I'm trying to complie server.c from Beej's tutorial:
I'm using win XP and Visual C++ .net.
Code is following:
I'm getting the following errors: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; }
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?



LinkBack URL
About LinkBacks


