Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.

Similar presentations


Presentation on theme: "1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport."— Presentation transcript:

1 1 Socket Interface

2 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport Layer 3 / Network Layer 2 / Data Link Layer 1 / Physical Application RARPICMP

3 3 Sockets process sends/receives messages to/from its socket socket analogous to door  sending process shoves message out door  sending process relies on transport infrastructure on other side of door which brings message to socket at receiving process process TCP with buffers, variables socket host or server process TCP with buffers, variables socket host or server Internet controlled by app developer

4 4 Basic socket calls for a client Socket() bind() connect() recv()send() close() Local addr sockaddr_in{ } peer addr sockaddr_in{ }

5 5 #include /*UNIX*/ #include /*Windows*/ SOCKET socket(int domain, int type, int protocol) Returns: socket descriptor on success, INVALID_SOCKET on failure Socket( ) system call The socket API is protocol independent It can support several different communication domains domain parameter  AF_INET (internet)  AF_LOCAL (or AF_UNIX) domain type parameter indicates the type of socket to be created  SOCK_STREAM  SOCK_DGRAM  SOCK_RAW (access IP packet) protocol field indicates which protocol should be used with the socket  In the TCP/IP, the parameter is set to zero

6 6 Demultiplexing Ethernet ARPIPRARP ICMPIGMPTCPUDP application incoming frame

7 7 bind( ) system call Specify local IP address and local port for a socket Can use INADDR_ANY for any IP address when the host is multi-home host s parameter  Socket descriptor *name and namelen parameters are used to supply the port and IP address of the local AP  *name points to the socket address data structure  namelen indicate the length of socket address data structure #include /*UNIX*/ #include /*Windows*/ int bind(SOCKET s, const struct sockaddr *name, int namelen); Return: 0 on success, SOCKET_ERROR on error

8 8 Sockaddr data abstraction Sa_family and sin_family are common between the two structures The domain type in the socket() function must be the same value as the family. Struct sockaddr{ unsigned short int sa_family; unsigned char sa_data[14]; }; Struct sockaddr_in{ sa_family_tsin_family; unsigned short int sin_port; structin_addrsin_addr; unsigned char_pad[ ] }; The sockaddr interface uses data abstraction. Thus, while the protocol domain may change, the interface remain the same

9 9 Connect( ) system call Used to establish the connection *peer parameter specifies server’s address and port number Used by client  Used in connection-oriented TCP: Forms a TCP connection, server uses accept to receive the call  Used in connectionless UDP: record the server’s address in the socket. #include /*UNIX*/ #include /*Windows*/ int connect(SOCKET s, const struct sockaddr *peer, int peer_len); Return: 0 on success, nonzero on failure

10 10 send(), sendto(), sendmsg() system call Send, sendto, and sendmsg  Transfer outgoing data from application  send () is used in the socket which is connected  sendto() and sendmsg() use to send a message using an unconnected socket Sendmsg (socket, msgstruct, flags)  Perform the same operation as sendto, but abbreviates the arguments by defining a structure  Flags MSG_OOB  Cause urgent data to be sent or read MSG_PEEK  Peek at incoming data without removing it from the receive buffer MSG_DONTROUTE  Cause the kernel to bypass the normal routing function #include /*UNIX*/ #include /*Windows*/ int send(SOCKET s, void *buf, size_t len, int flags); Int sendto (SOCKET s, const void *buf, size_t len, int flags, const struct sockaddr *to, int tolen) Return: 0 on success, nonzero on failure

11 11 recv(), recvfrom(), recvmsg() system call Recv, recvfrom, and recvmsg  Transfer incoming data to application  recv() uses to receive data from a connected socket  recvfrom() and recvmsg() is used to receive data from unconnected socket, receive data from arbitrary set of clients  Read and write with sockets s uch as read and write for I/O Used with connected sockets Read (descriptor, buffer, length)  Descriptor may correspond to a file or a socket (remote) #include /*UNIX*/ #include /*Windows*/ int recv (SOCKET s, void *buf, size_t len, int flags); Int recvfrom (SOCKET s, void *buf, size_t len, int flags, struct sockaddr *from, int fromlen) Return: number of bytes transferred on success, -1 on failure

12 12 Figure 13 a simple TCP client #include int main( void ) { struct sockaddr_in peer; int s; int rc; char buf[ 1 ]; peer.sin_family = AF_INET; peer.sin_port = htons( 7500 ); peer.sin_addr.s_addr = inet_addr( "127.0.0.1" ); s = socket( AF_INET, SOCK_STREAM, 0 ); if ( s < 0 ) { perror( "socket call failed" ); exit( 1 ); }

13 13 Figure 13 a simple TCP client (cont) rc = connect( s, ( struct sockaddr * )&peer, sizeof( peer ) ); if ( rc ) { perror( "connect call failed" ); exit( 1 ); } rc = send( s, "1", 1, 0 ); if ( rc <= 0 ) { perror( "send call failed" ); exit( 1 ); } rc = recv( s, buf, 1, 0 ); if ( rc <= 0 ) perror( "recv call failed" ); else printf( "%c\n", buf[ 0 ] ); exit( 0 ); }

14 14 Basic socket calls in a server Socket() bind() accept() recv()send() close() Local addr sockaddr_in{ } peer addr sockaddr_in{ } listen()

15 15 listen() system call  Used by server, TCP is in passive mode, UDP server don’t require  Prepares socket to accept incoming connections  backlog parameter is the length of the server request queue (connection request queue) #include /*UNIX*/ #include /*Windows*/ int listen(SOCKET s, int backlog); Return: 0 on success, SOCKET_ERROR on error

16 16 accept() system call Used by TCP server Waits for next connection establish and returns new socket Use the newsock to communication with this client Returns the address of the new connection’s peer in the sockaddr_in structure pointed to by *addr #include /*UNIX*/ #include /*Windows*/ SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen); Return: A connected socket if OK, INVALID_SOCKET on failure

17 17 A simpler TCP server #include int main( void ) { struct sockaddr_in local; int s; int s1; int rc; char buf[ 1 ]; local.sin_family = AF_INET; local.sin_port = htons( 7500 ); local.sin_addr.s_addr = htonl( INADDR_ANY ); s = socket( AF_INET, SOCK_STREAM, 0 ); if ( s < 0 ) { perror( "socket call failed" ); exit( 1 ); }

18 18 A simpler TCP server (cont) rc = bind( s, ( struct sockaddr * )&local, sizeof( local ) ); if ( rc < 0 ) {perror( "bind call failure" ); exit( 1 ); } rc = listen( s, 5 ); if ( rc ) {perror( "listen call failed" ); exit( 1 ); } s1 = accept( s, NULL, NULL ); if ( s1 < 0 ) {perror( "accept call failed" ); exit( 1 ); } rc = recv( s1, buf, 1, 0 ); if ( rc <= 0 ) {perror( "recv call failed" ); exit( 1 ); } printf( "%c\n", buf[ 0 ] ); rc = send( s1, "2", 1, 0 ); if ( rc <= 0 ) perror( "send call failed" ); exit( 0 ); }


Download ppt "1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport."

Similar presentations


Ads by Google