Thread: socket programming for windows in C

  1. #1
    noob lepricaun's Avatar
    Join Date
    Jul 2004
    Posts
    26

    socket programming for windows in C

    hi guys,

    I'm still learning C, and although socket programming for linux works like a charm, i can't get it to work for windows....

    the code that is given in this tutorial is supposed to be written for Visual C++ 6.0 standard edition, well i have it too...

    if i try to compile this code i get the following errors:

    --------------------Configuration: winsock - Win32 Debug--------------------
    Compiling...
    winsock.c
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(11) : error C2143: syntax error : missing ';' before 'type'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(12) : error C2143: syntax error : missing ';' before 'type'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(13) : error C2143: syntax error : missing ';' before 'type'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(14) : error C2143: syntax error : missing ';' before 'type'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(22) : error C2065: 'sockfd' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(27) : error C2065: 'h' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(27) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct hostent *'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(27) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(33) : error C2065: 'server' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(33) : error C2224: left of '.sin_addr' must have struct/union type
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(33) : error C2223: left of '->h_addr_list' must point to struct/union
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(34) : error C2224: left of '.sin_port' must have struct/union type
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(35) : error C2224: left of '.sin_family' must have struct/union type
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(43) : error C2065: 'message' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(43) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(43) : warning C4024: 'send' : different types for formal and actual parameter 2
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(43) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(43) : warning C4024: 'strlen' : different types for formal and actual parameter 1
    Error executing cl.exe.

    winsock.exe - 12 error(s), 6 warning(s)
    i've tried several other tutorials about sockets in windows, but none of them will compile, most give errors about the functions used in winsock.h.

    i've tried different compilers too:
    Dev-C++
    Pelles C
    Borland C++

    but i can't compile any sources which contain sockets in windows.

    how is this possible, and better, how should i solve this, it's getting really irritating, i've been strugling with this problem for a couple of weeks now, have read all i could about sockets in windows, added WSock32.lib to the project, but that still gives errors


    any help is greatly appreciated!
    The path of access leads to the tower of wisdom...
    __________________________________________________ ___________________

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
            const char buf[17]="Hello everybody!";
    	printf("%s\n",buf);
            return 0;
    }
    

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Looks like you have a simple syntax error somewhere in your code - post it.

    A nice tut: http://www.ecst.csuchico.edu/~beej/g...tml/index.html

    gg

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(11) : error C2143: syntax error : missing ';' before 'type'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(12) : error C2143: syntax error : missing ';' before 'type'
    C:\Program Files\Microsoft Visual Studio\MyProjects\winsock\winsock.c(13) : error C2143: syntax error : missing ';' before 'type'
    I believe these particular syntax errors are caused by having variable definitions after executable code in a Visual C++ C program.

    Move the
    Code:
    WSAStartup(0x0101,&wsda);
    to a place after the char *message statement.

    If there are any error messages may (or may not) be a little more meaningful once you get these messages out of the way.

    Remember to link with wsock32.lib.

    Something like

    cl winsock.c /link wsock32.lib

    Good Luck!

    Dave
    Last edited by Dave Evans; 09-08-2004 at 01:31 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you including winsock.h or winsock2.h ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    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).

  7. #7
    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.)

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Where do you see C++ in this?
    You mean the statements mixed up with declarations.
    You can tell it's a C program because the OP called it winsock.c
    The use of 'C' header files is also a big clue.

    void main is wrong - period.
    > if not most, C compilers accept "void main()"
    gcc is widely used, and doesn't accept this in any language mode

    Header files as commented on already - use <> not "" for system header files

    > exit(1);
    No prototype for this function

    > send(sockfd,message,strlen(message),0);
    No error checking

    > I timed myself at 42 seconds to re-format the code with vi.
    I find
    indent -kr -ts4 -nut prog.c
    does a rather nice job in a fraction of a second
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Ahh, yes. I should have looked a little bit longer at the code to see those problems

    Anyways, I agree that this is a very bad tutorial to learn network programming. Not only because it ignores return values, but also because of this line in the tutorial:

    Socket programming is very necessary if you want to be a good hacker ...Since a a hacker knows how to break into a computer remotely
    <sigh> Everyone wants to be a hacker

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Salem
    > Where do you see C++ in this?
    You mean the statements mixed up with declarations.
    You can tell it's a C program because the OP called it winsock.c
    The use of 'C' header files is also a big clue.

    void main is wrong - period.
    > if not most, C compilers accept "void main()"
    gcc is widely used, and doesn't accept this in any language mode

    Header files as commented on already - use <> not "" for system header files

    > exit(1);
    No prototype for this function

    > send(sockfd,message,strlen(message),0);
    No error checking

    > I timed myself at 42 seconds to re-format the code with vi.
    I find
    indent -kr -ts4 -nut prog.c
    does a rather nice job in a fraction of a second
    Not to nitpick: but gcc accepts void main() with a warning. g++ rejects it with an error. The other C compilers I tried didn't complain, and the other C++ compilers rejected it with an error. Of course everyone here knows how evil this is, so it requires no further comment by me.

    gcc (as well as borland and microsoft) accept and link exit() with the headers given. I never noticed this before, since I rarely use exit(), and when I do, I always include <stdlib.h> I wonder why he used "exit(1)" for error conditions, but didn't use "exit(0)" for non-error conditions. Is zero the default exit/return value or what? Of course he couldn't use return 1 and return 0, since he used void main(). Anyhow, I agree that not including <stdlib> is a bad thing for people using exit().


    It took me more that 30 seconds to find the command line for indent (since I always format my programs the way I like, I rarely have use for indent, but I really like it for what it does).

    I absolutely agree with sentiments expressed here that tutorial programs should be exemplary examples (did I really say "exemplary examples"?) of good programming practice and good style. Even though opinions of "good" style vary, almost everyone agrees that the things you pointed out (no error checking, for example) are "bad" practice. No source-code formatting is, obviously, "bad" style. Use of #include "stdio.h" looks amateurish to me, and therefore would be placed in the "not good" category, but is not "wrong". Etc. etc., etc.

    I would like to see a link to a good tutorial on socket programming with good example code. Portable if possible (or, at least, as portable as possible).
    Last edited by Dave Evans; 09-08-2004 at 05:24 PM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well socket programming tutorials will never be 100% portable, but they can be written very close. In fact, the only differences I can think of off the top of my head are:
    1) Different include files are needed.
    2) Windows needs WSAStartup()
    3) gethostbyname() is now depreciated on windows, and getaddrinfo() should be used instead.
    4) error codes are a little different.

    The best tutorial I've seen regarding socket programming is at http://www.ecst.csuchico.edu/~beej/guide/net/
    That tutorial is for linux, but 98% of it is valid on windows as well.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by bithub
    Well socket programming tutorials will never be 100% portable, but they can be written very close. In fact, the only differences I can think of off the top of my head are:
    1) Different include files are needed.
    2) Windows needs WSAStartup()
    3) gethostbyname() is now depreciated on windows, and getaddrinfo() should be used instead.
    4) error codes are a little different.

    The best tutorial I've seen regarding socket programming is at http://www.ecst.csuchico.edu/~beej/guide/net/
    That tutorial is for linux, but 98% of it is valid on windows as well.
    A real quick glance makes me think that this is what I am looking for! I really appreciate the link. I will check it out more carefully in the near future, I hope.

    I didn't mean to hijack the thread from the original poster, but I am happy to get the information.

    Thanks a lot.

    Dave

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Most welcome

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by bithub
    Anyways, I agree that this is a very bad tutorial to learn network programming. Not only because it ignores return values, but also because of this line in the tutorial:

    Socket programming is very necessary if you want to be a good hacker ...Since a a hacker knows how to break into a computer remotely
    But even the good tutorial you referred us to was written by someone with an e-mail address @piratehaven.org (!)

    You really can't tell who you are dealing with online. Some very good people have very bad opinions. Some very bad people have something worthwhile to say. Your Mileage May Vary.

    Thanks, again,

    Dave

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by Codeplug
    Looks like you have a simple syntax error somewhere in your code - post it.

    A nice tut: http://www.ecst.csuchico.edu/~beej/g...tml/index.html

    gg
    See, I told ya it was nice

    gg

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