- Timestamp:
- Nov 20, 2012, 1:19:44 AM (12 years ago)
- Branches:
- master
- Children:
- a845faf
- Parents:
- 7d7df47
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r7d7df47 raee34b9 3 3 4 4 #include <winsock2.h> 5 #include <WS2tcpip.h> 5 6 6 7 #include <stdio.h> … … 32 33 int main(int argc, char *argv[]) 33 34 { 34 int sock, n; 35 int length; 36 struct sockaddr_in server, from; 37 struct hostent *hp; 38 char buffer[256]; 39 35 int sock, n; 36 struct sockaddr_in server, from; 37 struct hostent *hp; 38 char buffer[256]; 39 40 40 if (argc != 3) { 41 printf("Usage: server port\n");41 cout << "Usage: server port" << endl; 42 42 exit(1); 43 43 } … … 51 51 52 52 if (wsaerr != 0) { 53 printf("The Winsock dll not found.\n");53 cout << "The Winsock dll not found." << endl; 54 54 exit(1); 55 55 }else 56 printf("The Winsock dll was found.\n");56 cout << "The Winsock dll was found." << endl; 57 57 58 58 sock= socket(AF_INET, SOCK_DGRAM, 0); 59 if (sock < 0) error("socket"); 59 if (sock < 0) 60 error("socket"); 60 61 61 62 server.sin_family = AF_INET; 62 63 hp = gethostbyname(argv[1]); 63 if (hp==0) error("Unknown host"); 64 if (hp==0) 65 error("Unknown host"); 64 66 65 memcpy((char *)&server.sin_addr, 66 (char *)hp->h_addr, 67 hp->h_length); 68 server.sin_port = htons(atoi(argv[2])); 69 length=sizeof(struct sockaddr_in); 70 printf("Please enter the message: "); 71 memset(buffer, 0, 256); 72 fgets(buffer,255,stdin); 73 n=sendto(sock,buffer, 74 strlen(buffer),0,(const struct sockaddr *)&server,length); 75 if (n < 0) error("Sendto"); 76 n = recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length); 77 if (n < 0) error("recvfrom"); 67 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); 68 server.sin_port = htons(atoi(argv[2])); 69 cout << "Please enter the message: "; 70 memset(buffer, 0, 256); 71 fgets(buffer,255,stdin); 78 72 73 socklen_t fromlen = sizeof(server); 74 75 n=sendto(sock,buffer, strlen(buffer), 0, (const struct sockaddr *)&server, fromlen); 76 if (n < 0) 77 error("Sendto"); 78 79 n = recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&from, &fromlen); 80 if (n < 0) 81 error("recvfrom"); 82 79 83 buffer[n] = '\0'; 80 84 cout << "Got an ack: " << endl;
Note:
See TracChangeset
for help on using the changeset viewer.