New version page

USC EE 450 - Overview of Network Programming: Sockets

This preview shows page 1-2-3-4-5 out of 15 pages.

View Full Document
View Full Document

End of preview. Want to read all 15 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

Overview of Network Programming: SocketsEE450: Introduction to Computer Networks Professor A. ZahidEE450, USC, Zahid 1Application Programming InterfaceEE450, USC, Zahid 2 The services provided by the operating system that provide the interface between application and the TCP/IP Protocol Suite.ApplicationNetwork APIProtocol AProtocol BProtocol CAPI: SocketsEE450, USC, Zahid 3 TCP/IP does not include an API definition. There are a variety of APIs for use with TCP/IP: UNIX Sockets Winsock (Windows Sockets) A socket is an abstract representation of a communication endpoint. A socket allows the application to “plug in” to the network and communicate with other applications A socket is uniquely identified by the IP address, Port number and the underlying transport layer protocolSocket TypesEE450, USC, Zahid 4Socket StructureEE450, USC, Zahid 5Socket Descriptor Data StructureDescriptor Table01234Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4Remote IP: 123.45.6.78Local Port: 2249Remote Port: 3726EE450, USC, Zahid 6Basic Sockets APIEE450, USC, Zahid 7 Clients and Servers differ in some aspects of their use of API and they are the same in other aspects Both client and server programs begin by askingthe NOS to “create” a socket. The function toaccomplish that is Socket ( ) int socket (int protocol family, int type, int protocol) Protocol family: PF_INET Type: SOCK_STREAM, SOCK_DGRAM Protocol: TCP, UDP Return value of Socket ( ) is a non-negative integer called Socket Descriptor (or –1 if errors)TCP ServerEE450, USC, Zahid 8 Create a TCP socket using Socket ( ) Assign a port number to the socket using Bind ( ) int bind (int socket, local address, address length) Local address is the IP address and the port number of theserver. It is this address that the client and the server need toagree on to communicate. Neither one need to know the clientaddress. Address length is length of address structure If successful, Bind ( ) returns a 0, otherwise it returns –1 If successful, the socket at server side is “associated ” to thelocal IP address and the local port number Listen to connections from clients using Listen ( ) int listen (int socket, int queue limit) Queue limit is an upper bound on # of clients that can be waiting If successful, Listen ( ) returns a 0, otherwise it returns –1TCP Server (Continued)EE450, USC, Zahid 9 The socket that has been bounded to a port and marked for listening is never actually used for sending and receiving. The socket (known as the welcoming socket or the “parent” socket. “Child” sockets are created for each client. It is this socket that is actually used for sending and receiving Server gets a socket for an incoming client connection by calling Accept ( ) int accept (int socket, client address, address length) If successful, accept ( ) returns a descriptor for the new socket, otherwise it returns -1TCP Server (Continued)EE450, USC, Zahid 10 Once the child socket is created, communications (send and receive) can take place int send (int socket, message, message length) int recv (int socket, recv buffer, buffer length) When the application is done with the socket, it needs to close it (The child socket, not the parent socket which is passively open to welcome new clients). This is done by calling Close ( ) int close (int socket)TCP ClientEE450, USC, Zahid 11 Create a TCP socket using Socket ( ) Establish a connection to the server using Connect ( ) int connect (int socket, foreign address, address length) Foreign address is the address of the server and the portnumber of the server (The well-known port number) Communications using Send and Recv int send (int socket, message, message length) int recv (int socket, recv buffer, buffer length) Close the socket using Close ( ) int close (int socket)Connection-less Iterative ServerEE450, USC, Zahid 12Flow Chart: Socket Interface for Connection-Less Iterative ServerEE450, USC, Zahid 13Connection-oriented Concurrent ServerEE450, USC, Zahid 14Flow Chart: Socket Interface for Connection-Oriented Concurrent ServerEE450, USC, Zahid


View Full Document
Loading Unlocking...
Login

Join to view Overview of Network Programming: Sockets and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Overview of Network Programming: Sockets and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?