From: Gatis <ga...@mt...> - 2002-05-13 16:38:44
|
I guess the problem occurs becouse you do not call WSAStartup to initialize windows sockets before any other socket releated call. Try something like this before call to socket(): WSADATA wsadata; if (WSAStartup(MAKEWORD(2, 1), &wsadata) != 0) { printf("unable to initialice windows sockets\n"); exit(-1); } Gatis > The recent posts about winsock have reminded me of > a problem I had with winsock and mingw. > > I tried to make a simple echo server in mingw just > to play around with network apps and such, but I > always got an error trying to get a socket. The program > compiled fine, but just never got a socket. I tried > this with firewall on and with firewall off, and got > the same results both times. > > Is this a common problem? What am I doing wrong? > Here is the code: > > #include <winsock.h> > > > #define ECHO_PORT 7 > > > void main() > { > int sockfd, clisock; > > struct sockaddr_in server, client; > int addrlen; > char buffer[1024]; > int bytes_recv, msglen; > > > > if (( sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) > exit(-1); // HERE is where it always exits > > > server.sin_family = AF_INET; > server.sin_addr.s_addr = INADDR_ANY; > > server.sin_port = htons((u_short) ECHO_PORT); > > if (bind(sockfd, (struct sockaddr *) &server,sizeof(server))) > > exit(-1); > > listen(sockfd, 5); > > > > addrlen = sizeof(client); > clisock = accept(sockfd,(struct sockaddr *) &client,&addrlen); > > > for(;;) { > memset(buffer, '\0', 1024); > > bytes_recv = recv(clisock, buffer, 1024, 0); > if (bytes_recv < 1){ > close(clifd); > exit(0); > > } > > if (bytes_recv > 0) > > { > printf("\nrecv:%s", buffer); > msglen = bytes_recv; > send(clisock, buffer, msglen, 0); > > } > > } > > > } > _________________________________________________________________ > http://fastmail.ca/ - Fast Secure Web Email for Canadians |