

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


#include #include #include #include
#define PORT 13490 // the port client will be connecting to #define MAXDATASIZE 100 // max number of bytes we can get at once int main(int argc, char argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct sockaddr_in their_addr; // connector’s address information WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); exit(1); } if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { / Tell the user that we could not find a usable / / WinSock DLL. */ WSACleanup( ); exit(0); } if (argc != 2) { fprintf(stderr,"usage: client serverIP\n"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); WSACleanup( ); exit(1); }
their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // network byte order their_addr.sin_addr.s_addr = inet_addr(argv[1]); memset(&(their_addr.sin_zero), 0, 8); // zero rest of the struct if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr_in)) == -1) { perror("connect"); WSACleanup( ); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); WSACleanup( ); exit(1); } buf[numbytes] = 0; printf("Received: %s",buf); closesocket(sockfd); WSACleanup( ); return 0; }