170 likes | 294 Views
This example demonstrates a simple client-server communication using TCP protocol with sequential execution. The client connects to the server, receives text, prints it, and closes the connection. The server accepts connections, sends text messages, and never exits. Socket calls for clients and servers are explained. Sample code for both client and server sides is provided.
E N D
An Example Service • Purpose • Count times invoked • Return printable ASCII message • Connection-oriented protocol • Sequential execution (not concurrent)
An Example Client • Open TCP connection to server • Iterate until end-of-file • Receive text • Print characters received • Close connection • Exit
Example Server • Create socket and put in passive mode • Iterate forever • Accept next connection, get new socket • Increment count and send text message • Close socket for connection • Notes • Main socket remains open • Server never exits
Socket Calls In Client And Server • Client closes socket after use • Server never closes original socket
Code For Client • Arguments to program • Host • Protocol port • Both optional • Many details • Minor incompatibilities among socket implementations • Unix • Microsoft • Use C #ifdef
Code For Server • Arguments to program • Protocol port • C language #ifdefs for socket variants
Stream Interface • Sender • Calls send repeatedly • Specifies number of octets per call • TCP • Divides stream into segments • Receiver • Calls recv repeatedly • Receives one or more octets per call • Count of zero means ‘‘end of file’’ • Size received unrelated to size sent