Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. //
  2. // Client.cpp
  3. //
  4. // Extremely simple, stream client example.
  5. // Works in conjunction with Server.cpp.
  6. //
  7. // The program attempts to connect to the server and port
  8. // specified on the command line. The Server program prints
  9. // the needed information when it is started. Once connected,
  10. // the program sends data to the server, waits for a response
  11. // and then exits.
  12. //
  13. // Compile and link with wsock32.lib
  14. //
  15. // Pass the server name and port number on the command line.
  16. //
  17. // Example: Client MyMachineName 2000
  18. //
  19. #include "stdafx.h"
  20. #include <stdio.h>
  21. #include <winsock2.h>
  22.  
  23. // Function prototype
  24. void StreamClient(char *szServer, short nPort);
  25.  
  26. // Helper macro for displaying errors
  27. #define PRINTERROR(s) \
  28. fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
  29.  
  30. ////////////////////////////////////////////////////////////
  31.  
  32. int main(int argc, char **argv)
  33. {
  34.  
  35.  
  36. WORD wVersionRequested = MAKEWORD(1,1);
  37. WSADATA wsaData;
  38. int nRet;
  39. short nPort;
  40. //
  41. // Check for the host and port arguments
  42. //
  43. if (argc != 3)
  44. {
  45. fprintf(stderr,"\nSyntax: client ServerName PortNumber\n");
  46. return -1;
  47. }
  48. nPort = atoi(argv[2]);
  49. //
  50. // Initialize WinSock and check the version
  51. //
  52. nRet = WSAStartup(wVersionRequested, &wsaData);
  53. if (wsaData.wVersion != wVersionRequested)
  54. {
  55. fprintf(stderr,"\n Wrong version\n");
  56. return -1;
  57. }
  58. //
  59. // Go do the stuff a stream client does
  60. //
  61. StreamClient(argv[1], nPort);
  62.  
  63.  
  64. //
  65. // Release WinSock
  66. //
  67. WSACleanup();
  68. system("pause");
  69. return 0;
  70. }
  71.  
  72. ////////////////////////////////////////////////////////////
  73.  
  74. void StreamClient(char *szServer, short nPort)
  75. {
  76. printf("\nStream Client connecting to server: %s on port: %d",
  77. szServer, nPort);
  78.  
  79. //
  80. // Find the server
  81. //
  82. LPHOSTENT lpHostEntry;
  83.  
  84. lpHostEntry = gethostbyname(szServer);
  85. if (lpHostEntry == NULL)
  86. {
  87. PRINTERROR("gethostbyname()");
  88. return;
  89. }
  90.  
  91. //
  92. // Create a TCP/IP stream socket
  93. //
  94. SOCKET theSocket;
  95.  
  96. theSocket = socket(AF_INET, // Address family
  97. SOCK_DGRAM, // Socket type
  98. IPPROTO_UDP); // Protocol
  99. if (theSocket == INVALID_SOCKET)
  100. {
  101. PRINTERROR("socket()");
  102. return;
  103. }
  104.  
  105. //
  106. // Fill in the address structure
  107. //
  108. SOCKADDR_IN saServer;
  109.  
  110. saServer.sin_family = AF_INET;
  111. saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  112. // ^ Server's address
  113. saServer.sin_port = htons(nPort); // Port number from command line
  114.  
  115. //
  116. // connect to the server
  117. //
  118. int nRet;
  119.  
  120.  
  121. //
  122. // Send data to the server
  123. //
  124. char szBuf[256];
  125. int a = sizeof(sockaddr);
  126. n strcpy(szBuf, "From the Client");
  127. nRet = sendto(theSocket, // Connected socket
  128. szBuf, // Data buffer
  129. strlen(szBuf), // Length of data
  130. 0,
  131. (LPSOCKADDR)&saServer,
  132. a); // Flags
  133. if (nRet == SOCKET_ERROR)
  134. {
  135. PRINTERROR("send()");
  136. closesocket(theSocket);
  137. return;
  138. }
  139. memset(szBuf, 0, sizeof(szBuf));
  140. //
  141. // Wait for a reply
  142. //
  143.  
  144. nRet = recvfrom(theSocket, // Connected socket
  145. szBuf, // Receive buffer
  146. sizeof(szBuf), // Size of receive buffer
  147. 0, (sockaddr*)&saServer, &a); // Flags
  148. if (nRet == SOCKET_ERROR)
  149. {
  150. PRINTERROR("recv()");
  151. closesocket(theSocket);
  152. return;
  153. }
  154. //
  155. // Display the received data
  156. //
  157. printf("\nData received: %s", szBuf);
  158. closesocket(theSocket);
  159. return;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement