Guest User

Untitled

a guest
Oct 28th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. /***********************************************************
  2. * Ett enkelt exempel för att testa sockets i windowsmiljö *
  3. * med WSA (klientdel) *
  4. * *
  5. * Kurs: Nätverksprogrammering, DVA209 *
  6. * Kod av: Robert Suurna (lärare, mdh) *
  7. * Datum: 2011-10-07 *
  8. * *
  9. * struct sockaddr_in{ *
  10. * short sin_family; *
  11. * unsigned short sin_port; *
  12. * struct in_addr sin_addr; *
  13. * char sin_zero[8]; *
  14. * }; *
  15. ***********************************************************/
  16.  
  17. #include <windows.h>
  18. #include <winsock2.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #define NETWORK_ERROR -1
  23. #define NETWORK_OK 0
  24. #define CONN_PORT 8888
  25.  
  26. void sendmsg(SOCKET clientSocket);
  27. void report_error(const char *);
  28.  
  29. int main(void){
  30. WORD sockVersion;
  31. WSADATA wsaData;
  32. SOCKET clientSocket;
  33. struct sockaddr_in serverInfo;
  34. int sError;
  35. char ip_address[20];
  36.  
  37. sockVersion = MAKEWORD(2, 0);
  38.  
  39. printf("IP-Address:");
  40. gets(ip_address);
  41.  
  42. // Fill a socketaddr_in struct with address information
  43. serverInfo.sin_family = AF_INET;
  44. serverInfo.sin_addr.s_addr = inet_addr(ip_address);
  45. serverInfo.sin_port = htons(CONN_PORT); // Change to network-byte order and insert into port field
  46.  
  47. // Initialize Winsock
  48. WSAStartup(sockVersion, &wsaData);
  49.  
  50. // Create the socket
  51. clientSocket = socket(AF_INET, // Go over TCP/IP
  52. SOCK_STREAM, // A stream-oriented socket
  53. IPPROTO_TCP); // Use TCP protocol
  54.  
  55. if(clientSocket == INVALID_SOCKET) {
  56. report_error("socket()");
  57. WSACleanup();
  58. return NETWORK_ERROR;
  59. }
  60.  
  61. printf("\nCreate socket ok!\n");
  62. printf("\nTrying to connect to server (%s)...", ip_address);
  63.  
  64. // Connect to the server
  65. sError = connect(clientSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
  66.  
  67. if(sError == SOCKET_ERROR) {
  68. report_error("connect()");
  69. WSACleanup();
  70. return NETWORK_ERROR;
  71. }
  72.  
  73. // Successfully connected
  74. printf("\nConnected to server (%s)!\n", ip_address);
  75.  
  76.  
  77.  
  78. // Send to server (and receive from server)
  79. // ---> Put your new function call here! <---
  80. sendmsg(clientSocket);
  81. // system("PAUSE");
  82.  
  83.  
  84.  
  85. printf("\nDisconnected from server!\n\n");
  86.  
  87. // Clean up!
  88. closesocket(clientSocket);
  89. WSACleanup();
  90. system("PAUSE");
  91. return 0;
  92. }
  93.  
  94. /********************************************************************
  95. * Function grabs the last socket error number and displays an error *
  96. ********************************************************************/
  97. void report_error(const char *whichFunc)
  98. {
  99. int errorCode;
  100. char errorMsg[100]={};
  101.  
  102. errorCode = WSAGetLastError(); // Grab last error message
  103.  
  104. // Generate error string
  105. sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
  106.  
  107. // Print error message in console window
  108. printf("\n%s",errorMsg);
  109.  
  110. // Popup window with error message, just for fun
  111. MessageBox(NULL, errorMsg, "socket indication", MB_OK);
  112. }
  113.  
  114. void sendmsg(SOCKET clientSocket) {
  115.  
  116. char msg[100];
  117. int length;
  118.  
  119. while(1) {
  120. printf("Please type in a string\n");
  121. printf("Type %c#%c to disconnect.\n", 34, 34);
  122. fflush(stdin);
  123. gets(msg);
  124. length = strlen(msg);
  125.  
  126.  
  127. if(msg[0] == '#')
  128. break;
  129.  
  130. send(clientSocket, msg, length, 0);
  131. }
  132. }
  133.  
  134.  
  135.  
  136.  
  137. Serv
  138.  
  139.  
  140. /***********************************************************
  141. * Ett enkelt exempel för att testa sockets i windowsmiljö *
  142. * med WSA (serverdel) *
  143. * *
  144. * Kurs: Nätverksprogrammering, DVA209 *
  145. * Kod av: Robert Suurna (lärare, mdh) *
  146. * Datum: 2011-10-07 *
  147. * *
  148. * struct sockaddr_in{ *
  149. * short sin_family; *
  150. * unsigned short sin_port; *
  151. * struct in_addr sin_addr; *
  152. * char sin_zero[8]; *
  153. * }; *
  154. ***********************************************************/
  155.  
  156. #include <windows.h>
  157. #include <winsock2.h>
  158. #include <stdio.h>
  159.  
  160. #define NETWORK_ERROR -1
  161. #define NETWORK_OK 1
  162. #define CONN_PORT 8888
  163.  
  164. int recvmsg(SOCKET clientSocket);
  165. void report_error(const char *);
  166.  
  167. int main(void) {
  168. WORD sockVersion;
  169. WSADATA wsaData;
  170. SOCKET listeningSocket, clientSocket;
  171. struct sockaddr_in serverInfo, clientInfo;
  172. int sError, error, size, listening=1;
  173.  
  174. sockVersion = MAKEWORD(2, 0); // Use Winsock version 2.0
  175.  
  176. // Use sockaddr_in struct to fill in address information
  177. serverInfo.sin_family = AF_INET;
  178. serverInfo.sin_addr.s_addr = INADDR_ANY; // Since this socket is listening for connections, any local address will do
  179. serverInfo.sin_port = htons(CONN_PORT); // Convert integer to network-byte order and insert into the port field
  180.  
  181. // Initializing Winsock
  182. error = WSAStartup(sockVersion, &wsaData);
  183.  
  184. // Check for error
  185. if (error != 0) {
  186. report_error("WSAStartup()");
  187. WSACleanup();
  188. return NETWORK_ERROR;
  189. }
  190.  
  191. // Create the listening socket
  192. listeningSocket = socket(AF_INET, // Use TCP/IP
  193. SOCK_STREAM, // A stream-oriented socket
  194. IPPROTO_TCP); // Use TCP protocol
  195.  
  196. if (listeningSocket == INVALID_SOCKET) {
  197. report_error("socket()"); // Report the error with our custom function
  198. WSACleanup(); // Shutdown Winsock
  199. return NETWORK_ERROR;
  200. }
  201.  
  202. printf("\nCreate socket ok!");
  203.  
  204. // Bind the socket to our local server address
  205. sError = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr_in));
  206.  
  207. if (sError == SOCKET_ERROR) {
  208. report_error("bind()");
  209. WSACleanup();
  210. return NETWORK_ERROR;
  211. }
  212.  
  213. printf("\nBind ok!");
  214.  
  215. // Make the socket listen. Up to 10 connections may wait at any one time to be accepted
  216. sError = listen(listeningSocket, 10);
  217.  
  218. if (sError == SOCKET_ERROR) {
  219. report_error("listen()");
  220. WSACleanup();
  221. return NETWORK_ERROR;
  222. }
  223.  
  224. printf("\nSocket is now in listen mode!\n");
  225.  
  226. while(listening) {
  227. printf("\nWaiting for a client...");
  228.  
  229. // Wait for a client to connect, if accepted store info from connecting client in clientInfo
  230. size = sizeof(clientInfo);
  231. clientSocket = accept(listeningSocket, (struct sockaddr *)&clientInfo, &size);
  232.  
  233. if (clientSocket == INVALID_SOCKET) {
  234. report_error("accept()");
  235. WSACleanup();
  236. return NETWORK_ERROR;
  237. }
  238.  
  239. printf("\nConnect from client %s, port %d\n",inet_ntoa(clientInfo.sin_addr),
  240. ntohs(clientInfo.sin_port));
  241.  
  242.  
  243.  
  244.  
  245. // Receive from client (and send to client)
  246. // ---> Put your new function call here! <---
  247.  
  248. while(recvmsg(clientSocket) > 0);
  249.  
  250.  
  251. closesocket(clientSocket);
  252. printf("\nClient disconnected!\n\n");
  253. }
  254. closesocket(listeningSocket);
  255. printf("\nListening socket closed!\n\n");
  256.  
  257. // Shutdown Winsock
  258. WSACleanup();
  259.  
  260. return NETWORK_OK;
  261. }
  262.  
  263.  
  264. /********************************************************************
  265. * Function grabs the last socket error number and displays an error *
  266. ********************************************************************/
  267. void report_error(const char *whichFunc)
  268. {
  269. int errorCode;
  270. char errorMsg[100]={};
  271.  
  272. errorCode = WSAGetLastError(); // Grab last error message
  273.  
  274. // Generate error string
  275. sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
  276.  
  277. // Print error message in console window
  278. printf("\n%s",errorMsg);
  279.  
  280. // Popup window with error message, just for fun
  281. MessageBox(NULL, errorMsg, "socket indication", MB_OK);
  282. }
  283.  
  284. int recvmsg(SOCKET clientSocket) {
  285.  
  286. char msg[100];
  287. int recieve;
  288.  
  289. recieve = recv(clientSocket, msg, 20, 0);
  290.  
  291. if(recieve > 0) {
  292. if(msg[0] == '#')
  293. return 0;
  294.  
  295. printf("%s\n", msg);
  296. }
  297.  
  298.  
  299. return recieve;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment