Socket program in C cannot compile -


hi guys reading book socket programming , there 2 codes client , server.

here server code

#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(char *msg) {     perror(msg);     exit(1); } int main(int argc, char *argv[]) {     int sockfd, newsockfd, portno, clilen;     char buffer[256];     struct sockaddr_in serv_addr, cli_addr;     int n;     if (argc < 2) {         fprintf(stderr,"error, no port provided/n");         exit(1);     }     sockfd = socket(af_inet, sock_stream, 0);     if(sockfd < 0)         error("error opening socket");     bzero((char *) &serv_addr, sizeof(serv_addr));     portno = atoi(argv[1]);     serv_addr.sin_family = af_inet;     serv_addr.sin_addr.s_addr = inaddr_any;     serv_addr.sin_port = htons(portno);     if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <0)         error("error on binding");     listen(sockfd,5);     clilen = sizeof(cli_addr);     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client);     if(newsockfd < 0)         error("error on accept");     bzero(buffer,256);     n = read(newsockfd,buffer,255);     if(n < 0)         error("error reading socket");         printf("here message: %s\n",buffer);         n = write(newsockfd,"i got message",18);         if(n < 0)             error("error writing socket");         return 0; } 

and here client code

#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h>  void error(char *msg) {     perror(msg);     exit(1); } int main(int argc, char *argv[]) {     int sockfd, portno, n;     struct sockaddr_in serv_addr;     struct hostent *server;      char buffer[256];     if(argc < 3) {         fprintf(stderr,"usage %s hostname port\n", argv[0]);     }     portno = atoi(argv[2]);     sockfd = socket(af_inet, sock_stream, 0);     if(sockfd < 0)         error("error opening socket");     server = gethostbyname(argv[1]);     if(server == null) {         fprintf(stderr,"error no such host\n");         exit(0);     }     bzero((char *) &serv_addr, sizeof(serv_addr));     serv_addr.sin_family = af_inet;     bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);     serv_addr.sin_port = htons(portno);     if(connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)         error("error conecting");     printf("please enter message: ");     bzero(buffer,256);     fgets(buffer,255,stdin);     n = write(sockfd,buffer,strlen(buffer));     if(n < 0)     error("error reading socket");     printf("%s\n",buffer);     return 0; } 

but when compile visual studio or turbo c++ or borland c++ gives me error have downloaded required headers problem still there.

the problem reading examples standard "bsd" socket library. windows uses different socket library.

for more general approach, can port standard bsd sockets programs (what reading in book) winsock. advice here.

or, if want socket compatible system in windows, can download cygwin http://cygwin.com/ , run program there. (be sure download gcc compiler etc in setup.exe program.)


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -