Advertisement
Guest User

Untitled

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