// author: frenchwhale (http://frenchwhales_site.tripod.com) // for the WinSock tutorial // // file name: wsclient.c // // description: A TCP client that uses Winsock // to connect to a server and // sends a message, then, it waits // for an answer from the server. // Copyright 2002 frenchwale #include #include #include #include #define MESSAGE "Hello, Server!\n" int main(int argc, int **argv) { WSADATA wsda; // Structure to store info // returned from WSAStartup struct hostent *host; // Used to store information // retreived about the server char szMessage[80]; int iMessageLen; int ret; char szInBuffer[128]; int iBufferLen; char szAddress[64]; int iPort; SOCKET s; // Our TCP socket handle SOCKADDR_IN addr; // The host's address // Check arguments if(argc != 3 || (argc==2 && strcmp((char *) &argv[1][0], "/?")==0)) { printf("wsclient server port\n"); printf(" server: the server to connect to\n"); printf(" port: the port on the remote server \n"); exit(1); } // Copy the IP address strcpy(szAddress, (char *) &argv[1][0]); // Get the remote port iPort = atoi((char *) &argv[2][0]); if(iPort<0 || iPort>65563) { printf("Invalid port number! (%s)\n", argv[2]); exit(1); } // Copy the pre-specified message into a buffer strcpy(szMessage, MESSAGE); iMessageLen = strlen(szMessage); // Load version 1.1 of Winsock WSAStartup(MAKEWORD(1,1), &wsda); // Create a TCP socket printf("Creating socket..."); s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); // Error? if(s == SOCKET_ERROR) { printf("Error\nCall to socket(AF_INET, SOCK_STREAM, IPPROTO_IP); failed with:\n%d\n", WSAGetLastError()); exit(1); } printf("OK\n"); // Fill in the host information addr.sin_family = AF_INET; addr.sin_port = htons(iPort); addr.sin_addr.s_addr = inet_addr(szAddress); if(addr.sin_addr.s_addr == INADDR_NONE) // The address wasn't in numeric // form, resolve it { host = NULL; printf("Resolving host..."); host = gethostbyname(szAddress); // Get the IP address of the server // and store it in host if(host == NULL) { printf("Error\nUnknown host: %s\n", szAddress); exit(1); } memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length); printf("OK\n"); } // Connect to the server printf("Connecting to %s:%d...",szAddress, iPort); ret = connect(s, (struct sockaddr *) &addr, sizeof(addr)); if(ret == SOCKET_ERROR) { printf("Error\nCall to connect(s, (SOCKADDR) addr, sizeof(addr)); failed with:\n%d\n", WSAGetLastError()); exit(1); } printf("OK\n"); // Ready to send data printf("Sending data..."); ret = send(s, szMessage, iMessageLen, 0); if(ret == SOCKET_ERROR) { printf("Error\nCall to send(s, szMessage, iMessageLen, 0) failed with:\n%d\n", WSAGetLastError()); exit(1); } printf("OK\n"); printf("\"%s\" sent to %s\n", szMessage, szAddress); // Receive data printf("Waiting for a response..."); ret = recv(s, szInBuffer, sizeof(szInBuffer), 0); if(ret == SOCKET_ERROR) { printf("Error\nCall to recv(s, szInBuffer, sizeof(szInBuffer), 0); failed with:\n%d\n", WSAGetLastError()); exit(1); } printf("Response recieved\n"); iBufferLen = ret; // recv() returns the number of bytes read szInBuffer[iBufferLen] = '\0'; // convert to cstring printf("Response recieved from %s:\n\"%s\"\n", szAddress, szInBuffer); closesocket(s); WSACleanup(); return 0; }