Thread: socket programming for windows in C

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Salem
    > the code that is given in this tutorial is supposed to be written for Visual C++ 6.0 standard edition
    It pretends to be C, but it takes C++ to compile it

    It's also crap code.
    Not only does it lack formatting, 3 out of the first 4 lines are sloppy and or wrong.
    Assuming the following is the code he is trying to compile:

    Code:
    #include "stdio.h" 
    #include "winsock.h" 
    
    #define PORT 1200 
    
    void main(int argc,char *argv[]){ 
    
    WSADATA wsda; 
    WSAStartup(0x0101,&wsda); 
    
    struct sockaddr_in server; 
    int sockfd; 
    struct hostent *h; 
    char *message="Hello Server"; 
    
    
    if(argc!=2){ 
    printf("Usage : client "); 
    exit(1); 
    } 
    
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){ 
    printf("Socket Error..."); 
    exit(1); 
    } 
    
    if((h=gethostbyname(argv[1]))==NULL){ 
    
    fprintf(stderr,"Host Name Error..."); 
    exit(1); 
    } 
    
    server.sin_addr=*((struct in_addr*)h->h_addr); 
    server.sin_port=htons(PORT); 
    server.sin_family=AF_INET; 
    
    if(connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1){ 
    
    fprintf(stderr,"Connection out..."); 
    exit(1); 
    } 
    
    send(sockfd,message,strlen(message),0); 
    
    
    WSACleanup(); 
    closesocket(sockfd); 
    }
    Where do you see C++ in this? Also where do you see wrong code here? (Not saying you are wrong in your statement that 3 out of the first 3 lines are sloppy/wrong, but at a glance, I dont see any wrong code here).

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by bithub
    Code:
    void main(int argc,char *argv[]){ 
    
    WSADATA wsda; 
    WSAStartup(0x0101,&wsda); 
    
    struct sockaddr_in server; 
    int sockfd; 
    struct hostent *h; 
    char *message="Hello Server";
    Where do you see C++ in this? Also where do you see wrong code here? (Not saying you are wrong in your statement that 3 out of the first 3 lines are sloppy/wrong, but at a glance, I dont see any wrong code here).

    Lots of C compilers (like Microsoft Visual C++ when compiling a C program) do not allow any declarations after the first executable code. After I changed the position of the WSAStartup() line, it compiles OK as C or C++.

    On the other hand many, if not most, C compilers accept "void main()" and most other C++ compilers reject "void main()". It's sure to get lots of flames on this board, but...

    My question is: can you learn anything about windows sockets programming from this? Are there other, better tutorials that accomplish this with 48 lines of C (or C++, depending on how you look at)? Point me to it.

    I changed #include "stdio.h" to #include <stdio.h> just out of general principles. (Also winsock.h.)

    I timed myself at 42 seconds to re-format the code with vi. (That included about 5 seconds that it took me to figure out how to use the stopwatch function on my old Casio.)

    Looking at the error message, i changed the position of the WSAStartup(). (Didn't time myself to see how long it took to soak in, but wasn't much.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 07:40 PM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM