Download presentation
Presentation is loading. Please wait.
Published byReynold Quinn Modified over 9 years ago
1
cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7
2
cs423-cotter2 Example Windows Client Develop a set of procedures that can be used by other programs to implement client / server. int connectUDP (host, service) int connectsock(host, service, “udp”) [identify service, host, port] [get a socket] [connect to service / host / port] [return socket number]
3
cs423-cotter3 Example Windows Client connectTCP.c #include SOCKET connectTCP (char *host, char *service) { return connectsock (host, service, “tcp”); } SOCKET connectUDP (char *host, char *service) { return connectsock (host, service, “udp”); }
4
cs423-cotter4 Example Windows Client connectsock.c #include,,, SOCKET connectsock (char *host, char *service, char *transport) { struct hostent*phe; struct servent*pse; struct protoent*ppe; struct sockaddr_in sin; int s, type;
5
cs423-cotter5 Example Windows Client connectsock.c memset(&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; if (pse = getservbyname (service, transport) ) sin.sin_port = pse ->s_port; else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0) errexit (“can’t get %s service\n”, service); if (phe = gethostbyname (host) ) memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE ) errexit (“can’t get %s host\n”, host);
6
cs423-cotter6 Example Windows Client connectsock.c if ( (ppe = getprotobyname (transport) == 0) errexit (“can’t get %s protocol entry\n”,transport); if (strcmp (transport, “tcp”) == 0) type = SOCK_STREAM; else type = SOCK_DGRAM; if( (s = socket (PF_INET, type, ppe->p_proto)) == INVALID_SOCKET) errexit (“Can’t create a socket: %d\n”, GetLastError()); if (connect (s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR) errexit (“can’t connect to remote socket\n”); return s; }
7
cs423-cotter7 Example client program Echo client (connection-oriented) Establish a connection to server while (user enters text) send text to server receive text back from server display received message on screen close connection & exit
8
cs423-cotter8 tcpecho ( ) #include,,, voidTCPecho(const char *, const char *); voiderrexit(const char *,...); SOCKETconnectTCP(const char *, const char *); #defineLINELEN 128 #defineWSVERSMAKEWORD(2, 0) void main(int argc, char *argv[]) { char*host = "localhost";/* host to use if none supplied */ char*service = "echo";/* default service name */ : WSADATAwsadata; if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); TCPecho(host, service); WSACleanup(); exit(0); }
9
9 tcpecho ( ) – argument Switch #include,,, voidTCPecho(), errexit(), connectTCP(); #defineLINELEN 128, WSVERSMAKEWORD(2, 0) void main(int argc, char *argv[]) { char*host = "localhost";/* host to use if none supplied */ char*service = "echo";/* default service name */ switch (argc) { case 1:break; case 3: service = argv[2]; //Fall Through!! case 2: host = argv[1]; break; default; printf (“Usage: %s [host[port]]\n”, argv[0]); exit(1); } WSADATAwsadata; if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); TCPecho(host, service); WSACleanup(); exit(0);
10
cs423-cotter10 tcpecho (cont) void TCPecho(const char *host, const char *service) { charbuf[LINELEN+1];/* buffer for one line of text */ SOCKETs;/* socket descriptor */ intcc, outchars, inchars;/* characters counts */ s = connectTCP(host, service); while (fgets(buf, sizeof(buf), stdin)) { buf[LINELEN] = '\0'; /* ensure line null-termination */ outchars = strlen(buf); (void) send(s, buf, outchars, 0); for (inchars = 0; inchars < outchars; inchars += cc) { cc = recv(s, &buf[inchars], outchars-inchars, 0); if (cc == SOCKET_ERROR) errexit("socket recv failed: %d\n", GetLastError()); }fputs(buf, stdout); }closesocket(s); }
11
cs423-cotter11 UDPecho.cpp #include,,, voidUDPecho(const char *, const char *); voiderrexit(const char *,...); SOCKETconnectUDP(const char *, const char *); #defineLINELEN128 #defineWSVERSMAKEWORD(2, 0) void main(int argc, char *argv[]) { char*host = "localhost", *service = "echo"; WSADATAwsadata; if (WSAStartup(WSVERS, &wsadata)) errexit("WSAStartup failed\n"); UDPecho(host, service); WSACleanup(); exit(0); }
12
cs423-cotter12 UDPecho.cpp void UDPecho(const char *host, const char *service) { charbuf[LINELEN+1];/* buffer for one line of text*/ SOCKETs;/* socket descriptor */ intnchars;/* read count*/ s = connectUDP(host, service); while (fgets(buf, sizeof(buf), stdin)) { buf[LINELEN] = '\0';/* ensure null-terminated */ nchars = strlen(buf); (void) send(s, buf, nchars, 0); if (recv(s, buf, nchars, 0) < 0) errexit("recv failed: error %d\n", GetLastError()); fputs(buf, stdout); }
13
cs423-cotter13 UDPecho_cnls.cpp #include,,, ….. // function prototypes, defines, etc. void main(int argc, char *argv[]) { char*host = "localhost", *service = "echo"; WSADATAwsadata; switch (argc) {… } // test for command line params if (WSAStartup(WSVERS, &wsadata)) errexit("WSAStartup failed\n"); UDPecho_cnls(host, service); WSACleanup(); exit(0); }
14
cs423-cotter14 UDPecho_cnls.cpp (cont) void UDPecho_cnls(const char *host, const char *service) { charbuf[LINELEN+1];/*buffer for text */ SOCKETs; /* socket descriptor */ intnchars; /* read count*/ struct hostent*phe; /* * host info entry*/ struct servent*pse;/* * service info entry */ struct protoent *ppe;/* * protocol info entry */ struct sockaddr_in sin; /* Internet endpoint addr*/ inttype, status; /* socket descriptor & type*/ char *transport = "udp";/*Transport service name*/
15
cs423-cotter15 UDPecho_cnls.cpp (cont) memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; /* Map service name to port number */ if ( pse = getservbyname(service, transport) ) sin.sin_port = pse->s_port; else if ( (sin.sin_port = htons((u_short)atoi(service)))== 0) errexit("can't get \"%s\" service entry\n", service); /*Map host name to IP address, dotted decimal(?)*/ if ( phe = gethostbyname(host) ) memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
16
cs423-cotter16 UDPecho_cnls.cpp (cont) else if( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) errexit("can't get \"%s\" host entry\n", host); printf("Server at %s\n",inet_ntoa(sin.sin_addr)); /* Map protocol name to protocol number */ if ( (ppe = getprotobyname(transport)) == 0) errexit("Can't get \"%s\" entry\n", transport); /* Use protocol to choose a socket type */ if (strcmp(transport, "udp") == 0) type = SOCK_DGRAM; else type = SOCK_STREAM;
17
cs423-cotter17 UDPecho_cnls.cpp (cont) /* Allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s == INVALID_SOCKET) errexit("Socket error: %d\n", GetLastError()); printf("Enter data to send...\n"); while (fgets(buf, sizeof(buf), stdin)) { buf[LINELEN] = '\0';/* ensure null-terminated */ nchars = strlen(buf); status = sendto(s, buf, nchars, 0, (struct sockaddr FAR *)&sin,sizeof(sin)); if (status == SOCKET_ERROR) errexit("Sendto failed: %d\n", GetLastError());
18
cs423-cotter18 UDPecho_cnls.cpp (cont) printf ("We just sent a datagram\n"); if (recvfrom(s,buf,LINELEN,0,NULL,NULL) < 0) errexit("recv failed: %d\n",GetLastError()); printf("We got back %d characters: ", nchars); fputs(buf, stdout); } /* end of while */ }/* end of UDPecho () */
19
cs423-cotter19 UDPecho_cnls.cpp – v2 #include using namespace std; voidUDPecho(const char *, const char *); #defineLINELEN128 #defineWSVERSMAKEWORD(2, 0) #define ARGSIZE 25 int main(int argc, char *argv[]) { charhost[ARGSIZE] = "localhost"; charservice[ARGSIZE] = "12345"; WSADATAwsadata;
20
cs423-cotter20 UDPecho_cnls.cpp – v2 switch (argc) { case 1: break; case 3: if (strlen(argv[2]) < ARGSIZE-1) strcpy_s (service,strlen(argv[2])+1,argv[2]); else { cout << "Service name too long!!" << endl; exit (1); } /* FALL THROUGH */ case 2: if (strlen(argv[1]) < ARGSIZE-1) strcpy_s (host,strlen(argv[1])+1,argv[1]); else { cout << "Service name too long!!" << endl; exit (1); } break;
21
cs423-cotter21 UDPecho_cnls.cpp – v2 default: cout << "Usage: UDPecho [host [port]]\n" << endl; exit(1); } if (WSAStartup(WSVERS, &wsadata)) { cout << "WSAStartup failed" << endl; exit(1); } UDPecho(host, service); WSACleanup(); return 0; }
22
cs423-cotter22 UDPecho_cnls.cpp – v2 void UDPecho(const char *host, const char *service) { charbuf[LINELEN+1];/* buffer for one line of text */ SOCKETs;/* socket descriptor */ intnchars;/* read count*/ struct hostent*phe;/* pointer to host information entry*/ struct servent*pse;/* pointer to service information entry */ struct protoent *ppe;/* pointer to protocol information entry */ struct sockaddr_in sin; /* an Internet endpoint address */ inttype, status; /* socket descriptor and socket type */ char *transport = "udp"; /* transport service name for port*/ memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET;
23
cs423-cotter23 UDPecho_cnls.cpp – v2 /* Map service name to port number */ if ( pse = getservbyname(service, transport) ) sin.sin_port = pse->s_port; else if ( (sin.sin_port = htons((u_short)atoi(service)))== 0) { cout << "Can't get \"" << service << "\" service entry" << endl; exit(1); } /* Map host name to IP address, allowing for dotted decimal */ if ( phe = gethostbyname(host) ) memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) { cout << "Can't get \"" << host << "\" IP address "<< endl; exit(1); }
24
cs423-cotter24 UDPecho_cnls.cpp – v2 cout <<"Our target server is at address " << inet_ntoa(sin.sin_addr) << endl; /* Map protocol name to protocol number */ if ( (ppe = getprotobyname(transport)) == 0) { cout << "Can't get \"" << transport << "\" protocol entry "<< endl; exit(1); } /* Socket type is UDP*/ type = SOCK_DGRAM; /* Allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s == INVALID_SOCKET) { cout << "Can't create socket" << endl; exit(1); }
25
cs423-cotter25 UDPecho_cnls.cpp – v2 /* begin the data input / output loop... */ cout << "Enter data to send..." << endl; while (cin.getline(buf,LINELEN)) { buf[LINELEN] = '\0';/* ensure null-terminated */ nchars = strlen(buf); status = sendto(s, buf, nchars, 0, (struct sockaddr FAR *)&sin, sizeof(sin)); if (status == SOCKET_ERROR) { cout << "Sendto failed...: error " << GetLastError() << endl; exit(1); } cout << "We just sent a datagram" << endl;
26
cs423-cotter26 UDPecho_cnls.cpp – v2 if (recvfrom(s, buf, LINELEN, 0, NULL, NULL) < 0) { cout << "recv failed: error" << GetLastError() << endl; exit(1); } if (strncmp(buf, "_quit", 5) == 0) { cout << "Goodbye......" << endl; break; } cout << "We got back the following " << nchars << " characters: " << endl; cout << buf << endl; } /* end of while */ }/* end of UDPecho () */
27
cs423-cotter27 Sample Output C: \udpecho_cnls\Debug>udpecho_cnls lab222c Our target server is at address 134.193.2.253 Enter data to send... This is my data We just sent a datagram We got back the following 16 characters: This is my data This is another message We just sent a datagram We got back the following 24 characters: This is another message ^Z C: \udpecho_cnls\Debug>
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.