Changeset 5e693e8 in network-game for client/Client
- Timestamp:
- Nov 24, 2012, 2:48:40 PM (12 years ago)
- Branches:
- master
- Children:
- 94ebbd9
- Parents:
- e08572c
- git-author:
- dportnoy <dmp1488@…> (11/24/12 14:11:41)
- git-committer:
- dportnoy <dmp1488@…> (11/24/12 14:48:40)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
re08572c r5e693e8 4 4 5 5 #if defined WINDOWS 6 7 6 #include <winsock2.h> 7 #include <WS2tcpip.h> 8 8 #elif defined LINUX 9 #include <sys/types.h> 10 #include <unistd.h> 11 #include <sys/socket.h> 12 #include <netinet/in.h> 13 #include <netdb.h> 9 #include <sys/types.h> 10 #include <unistd.h> 11 #include <sys/socket.h> 12 #include <netinet/in.h> 13 #include <netdb.h> 14 #include <cstring> 14 15 #endif 15 16 … … 30 31 using namespace std; 31 32 33 void initWinSock(); 34 void shutdownWinSock(); 32 35 void error(const char *); 33 36 34 37 int main(int argc, char *argv[]) 35 38 { 36 37 38 39 40 39 int sock, n; 40 struct sockaddr_in server, from; 41 struct hostent *hp; 42 char buffer[256]; 43 NETWORK_MSG msgTo, msgFrom; 41 44 42 43 44 45 45 if (argc != 3) { 46 cout << "Usage: server port" << endl; 47 exit(1); 48 } 46 49 47 WORD wVersionRequested; 48 WSADATA wsaData; 49 int wsaerr; 50 initWinSock(); 51 52 sock = socket(AF_INET, SOCK_DGRAM, 0); 53 if (sock < 0) 54 error("socket"); 50 55 51 wVersionRequested = MAKEWORD(2, 2); 52 wsaerr = WSAStartup(wVersionRequested, &wsaData); 56 server.sin_family = AF_INET; 57 hp = gethostbyname(argv[1]); 58 if (hp==0) 59 error("Unknown host"); 60 61 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); 62 server.sin_port = htons(atoi(argv[2])); 63 64 strcpy(msgTo.buffer, "Hello"); 65 n=sendMessage(&msgTo, sock, &server); 66 if (n < 0) 67 error("sendMessage"); 68 69 n = receiveMessage(&msgFrom, sock, &from); 70 if (n < 0) 71 error("receiveMessage"); 53 72 54 if (wsaerr != 0) { 55 cout << "The Winsock dll not found." << endl; 56 exit(1); 57 }else 58 cout << "The Winsock dll was found." << endl; 73 cout << msgFrom.buffer << endl; 74 75 while(true) { 76 cout << "Please enter a message (or q to quit): "; 77 cin.getline(msgTo.buffer, 256); 78 79 if (strcmp(msgTo.buffer, "q") == 0) { 80 break; 81 } 82 83 n=sendMessage(&msgTo, sock, &server); 84 if (n < 0) 85 error("sendMessage"); 86 87 n = receiveMessage(&msgFrom, sock, &from); 88 if (n < 0) 89 error("receiveMessage"); 90 91 cout << msgFrom.buffer << endl; 92 } 93 94 #if defined WINDOWS 95 closesocket(sock); 96 #elif defined LINUX 97 close(sock); 98 #endif 99 100 shutdownWinSock(); 101 102 cout << "Thank you for playing!" << endl; 103 getchar(); 104 105 return 0; 106 } 107 108 void initWinSock() 109 { 110 #if defined WINDOWS 111 WORD wVersionRequested; 112 WSADATA wsaData; 113 int wsaerr; 114 115 wVersionRequested = MAKEWORD(2, 2); 116 wsaerr = WSAStartup(wVersionRequested, &wsaData); 59 117 60 sock= socket(AF_INET, SOCK_DGRAM, 0); 61 if (sock < 0) 62 error("socket"); 118 if (wsaerr != 0) { 119 cout << "The Winsock dll not found." << endl; 120 exit(1); 121 }else 122 cout << "The Winsock dll was found." << endl; 123 #endif 124 } 63 125 64 server.sin_family = AF_INET; 65 hp = gethostbyname(argv[1]); 66 if (hp==0) 67 error("Unknown host"); 68 69 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); 70 server.sin_port = htons(atoi(argv[2])); 71 72 strcpy(msgTo.buffer, "Hello"); 73 n=sendMessage(&msgTo, sock, &server); 74 if (n < 0) 75 error("sendMessage"); 76 77 n = receiveMessage(&msgFrom, sock, &from); 78 if (n < 0) 79 error("receiveMessage"); 80 81 cout << msgFrom.buffer << endl; 82 83 while(true) { 84 cout << "Please enter a message (or q to quit): "; 85 cin.getline(msgTo.buffer, 256); 86 87 if (strcmp(msgTo.buffer, "q") == 0) { 88 break; 89 } 90 91 n=sendMessage(&msgTo, sock, &server); 92 if (n < 0) 93 error("sendMessage"); 94 95 n = receiveMessage(&msgFrom, sock, &from); 96 if (n < 0) 97 error("receiveMessage"); 98 99 cout << msgFrom.buffer << endl; 100 } 101 102 closesocket(sock); 103 104 WSACleanup(); 105 106 cout << "Thank you for playing!" << endl; 107 getchar(); 108 109 return 0; 126 void shutdownWinSock() 127 { 128 #if defined WINDOWS 129 WSACleanup(); 130 #endif 110 131 } 111 132 … … 113 134 void error(const char *msg) 114 135 { 115 116 WSACleanup();117 136 perror(msg); 137 shutdownWinSock(); 138 exit(1); 118 139 }
Note:
See TracChangeset
for help on using the changeset viewer.