Download presentation
Presentation is loading. Please wait.
Published byMarjory Johns Modified over 8 years ago
1
1 Socket Interface
2
2 Client-Server Architecture The client is the one who speaks first Typical client-server situations Client and server on the same host The output data is looped back internally and travels back up the stack as input Raw performance of the client and server application No network latency involved No dropped, delayed, or out of order Client and server on the same LAN still nearly ideal Packets are rarely lost and virtually never arrive out of order Client and server on different LANs Incur router routing and queuing problem
3
3 Client Server ClientServer (a) Client and server on the same host (b) Client and server on the same LAN Client router Server WAN (C) Client and server on different LAN
4
4 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
5
5 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
6
6 Interacting With Protocol Software Client or server uses transport protocols Protocol software inside OS Applications outside OS Mechanism needed to bridge the two Called Application Program Interface (API) Part of operating system Permits application to use protocols Defines Operations allowed Arguments for each operation
7
7 Socket API Originally designed For BSD UNIX To use with TCP/IP protocols Now Industry standard Available on many operating systems
8
8 Socket OS Abstraction (not hardware) Created dynamically Persists only while application runs Referenced by a descriptor Descriptor Small integer One per active socket Used in all operations on socket Generated by OS when socket created Only meaningful to application that owns socket In UNIX, integrated with file descriptors
9
9 Basic socket calls for a client Socket() bind() connect() recv()send() close() Local addr sockaddr_in{ } peer addr sockaddr_in{ }
10
10 #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
11
11 Demultiplexing Ethernet ARPIPRARP ICMPIGMPTCPUDP application incoming frame
12
12 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
13
13 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
14
14 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
15
15 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
16
16 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
17
17 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 ); }
18
18 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 ); }
19
19 Basic socket calls in a server Socket() bind() accept() recv()send() close() Local addr sockaddr_in{ } peer addr sockaddr_in{ } listen()
20
20 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
21
21 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
22
22 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 ); }
23
23 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 ); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.