Thread: Socket compiler errors

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    Socket compiler errors

    I'm trying to complie server.c from Beej's tutorial:
    I'm using win XP and Visual C++ .net.
    Code is following:

    Code:
    #include <winsock.h>
    	#include <stdio.h>
        #define MYPORT 3490    
        #define BACKLOG 10     
        void sigchld_handler(int s)
        {
            while(wait(NULL) > 0);
        }
        int main(void)
        {
    		WSADATA wsaData;
            SOCKET sockfd, new_fd;  
            struct sockaddr_in my_addr;    
            struct sockaddr_in their_addr;
            int sin_size;
            struct sigaction sa;
            int yes=1;
    
    		if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
                fprintf(stderr, "WSAStartup failed.\n");
                exit(1);
            } 
    
    
            if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("socket");
                exit(1);
            }
            if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
                perror("setsockopt");
                exit(1);
            }
            
            my_addr.sin_family = AF_INET;         
            my_addr.sin_port = htons(MYPORT);     
            my_addr.sin_addr.s_addr = INADDR_ANY; 
            memset(&(my_addr.sin_zero), '\0', 8); 
            if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
                                                                           == -1) {
                perror("bind");
                exit(1);
            }
            if (listen(sockfd, BACKLOG) == -1) {
                perror("listen");
                exit(1);
            }
            sa.sa_handler = sigchld_handler; 
            sigemptyset(&sa.sa_mask);
            sa.sa_flags = SA_RESTART;
            if (sigaction(SIGCHLD, &sa, NULL) == -1) {
                perror("sigaction");
                exit(1);
            }
            while(1) {  
                sin_size = sizeof(struct sockaddr_in);
                if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
                                                               &sin_size)) == -1) {
                    perror("accept");
                    continue;
                }
                printf("server: got connection from %s\n",
                                                   inet_ntoa(their_addr.sin_addr));
                if (!fork()) { 
                    closesocket(sockfd); 
                    if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                        perror("send");
                    closesocket(new_fd);
                    exit(0);
                }
                closesocket(new_fd); 
            }
            return 0;
        }
    I'm getting the following errors:

    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(66): error C2065: 'fork' : undeclared identifier
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(52): error C2065: 'SA_RESTART' : undeclared identifier
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(53): error C2065: 'SIGCHLD' : undeclared identifier
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(32): error C2664: 'setsockopt' : cannot convert parameter 4 from 'int *__w64 ' to 'const char *'
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(51): error C2228: left of '.sa_mask' must have class/struct/union type
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(50): error C2228: left of '.sa_handler' must have class/struct/union type
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(52): error C2228: left of '.sa_flags' must have class/struct/union type
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(66): error C2171: '!' : illegal on operands of type ''unknown-type''
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(19): error C2079: 'sa' uses undefined struct '(void)main::sigaction'
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(10): error C2065: 'wait' : undeclared identifier
    c:\Documents and Settings\Mitjko\My Documents\Visual Studio Projects\Tmp\Socketi.cpp(51): error C2065: 'sigemptyset' : undeclared identifier


    I don't know how to change fork() and similar, because I don't have any experience using *nix.

    What are Windows equivalents to these?
    Can you help me correcting this code?

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
        #include <winsock.h>
        #include <stdio.h>
        #define MYPORT 3490    
        #define BACKLOG 10     
    
        int main(void)
        {
            WSADATA wsaData;
            SOCKET sockfd, new_fd;  
            struct sockaddr_in my_addr;    
            struct sockaddr_in their_addr;
            int sin_size;
    
            int yes=1;
    
            if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
                fprintf(stderr, "WSAStartup failed.\n");
                exit(1);
            } 
    
            if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("socket");
                exit(1);
            }
            if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&yes,sizeof(yes)) == -1) {
                perror("setsockopt");
                exit(1);
            }
            
            my_addr.sin_family = AF_INET;         
            my_addr.sin_port = htons(MYPORT);     
            my_addr.sin_addr.s_addr = INADDR_ANY; 
            memset(&(my_addr.sin_zero), '\0', 8); 
            if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
                                                                           == -1) {
                perror("bind");
                exit(1);
            }
            if (listen(sockfd, BACKLOG) == -1) {
                perror("listen");
                exit(1);
            }
    
            while(1) {  
                sin_size = sizeof(struct sockaddr_in);
                if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
                                                               &sin_size)) == -1) {
                    perror("accept");
                    continue;
                }
                printf("server: got connection from %s\n",
                                                   inet_ntoa(their_addr.sin_addr));
                closesocket(sockfd); 
                if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                    perror("send");
                closesocket(new_fd); 
            }
            return 0;
        }
    There is no "equivalent" function of fork() in any Windows OS, per se. The best you can do if you want to do some multithreaded socket programming is use CreateThead(), _beginthead(), _spawnl(), etc. Otherwise just use asynchronous sockets.

    [edit]
    Corrected/modified the call to setsockopt()
    [/edit]
    Last edited by LuckY; 07-08-2004 at 01:56 PM.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I receive only one error in line

    if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,si zeof(int)) == -1)

    error C2664: 'setsockopt' : cannot convert parameter 4 from 'int *__w64 ' to 'const char *'

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    hmmm... try turning int yes=1 into: char yes[]="1"
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by Devil Panther
    hmmm... try turning int yes=1 into: char yes[]="1"
    No, no. Don't do that. Just cast the &yes to a const char*:
    Code:
    if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&yes,sizeof(yes)) == -1)
    // notice I also changed the sizeof() parameter to "yes" instead of "int" just because I find it
    // useful to have as a reminder, but either is fine.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    oh well... my bad...
    Last edited by Devil Panther; 07-08-2004 at 03:45 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks for help it's now working.
    I do have another question.
    When I execute this code and in Start\Run type:
    telnet hostname 3490 I get message Hello world and after that Connection to host lost. At the same time in first window (server) I see infinte loop with text: "accept: No error", I assume it is perror("accept"). Now, when I try once more to start telnet I get message:
    Could not open connection to the host, on port 3490. No connection could be made because the target machine actively refused it.
    I don't think this program is supposed to do this. If someone can explain me why it is happenning and how to fix it.
    Thanks

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Is it possible that is happening because of thet system call fork()?
    When talking about fork() that exists in *nix some people say use CreateProcess or CreateThread, but it is not equivalent because CreateThred uses couple of parameters and fork () use non!

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    By removing fork() from the code, the server can only deal with one connection at a time. So in other words, you don't have multi-tasking.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 01:52 PM
  3. Strange errors.
    By emonk in forum C Programming
    Replies: 2
    Last Post: 01-22-2003, 12:49 PM
  4. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM