Socket Programming: Sirak Kaewjamnong Computer Network Systems
Socket Programming: Sirak Kaewjamnong Computer Network Systems
Whats a Socket?
A socket is a communication mechanism that allows client/systems to be developed either locally or across networks.
C lie n t C lie n t S e rv e r
E th e r n e t
Connection-Oriented Protocol
socket() bind() listen() accept()
blocks until connection from client
server
client
connection establishment data (request) data(reply) socket() connect() write() read()
read() write()
Connectionless Protocol
socket() bind() recvfrom()
blocks until data received from a client
data(request)
sendto()
data(reply)
recvfrom()
Create a socket with the socket() system call Bind the socket to an address using the bind() system call. For a server socket, an address consists of a port number on the host machine Listen for connections with listen() system call Accept a connection with the 5
Create a socket with the socket() system call Connect the socket to the address of the server using the connect() system call Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() or send() and recv()system calls 6
Kinds of Socket
Traditional BSD families
TCP/IP (AF_INET,Internet) UNIX (AF_UNIX) XNS (AF_NS) APPLETALK,DECNET,IPX
Socket Address
A sockadd_in is a structure containing an internet address. This structure must defined in <netinet/in.h>
struct sockadd_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char }; sin_zero[8];
8
sockfd and newsockfd are file descriptors, i.e. array subscripts into the file descriptor table. These two variables store the values returned by the socket system call and the accept system call. portno stores the port number on which the server accepts connections. 11
The server reads characters from the socket connection into this buffer
struct sockaddr_in serv_addr, cli_addr;
The variable serv_addr will contain the address of server The variable cli_addr will contain the address of client To create socket and return a socket descriptor that can be used for accessing the socket
12 AF_INET is family of IPv4 Internet Protocol
sockfd = socket(AF_INET,SOCK_STREAM,0); if (sockfd < 0) error(Error opening Socket);
The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET.
serv_addr.sin_port = htons(portno);
The second field of serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port 13 number to this field, it is necessary
Byte Ordering
different byte ordering in varoius platform
little endian : Intel 80x86 , DEC VAX , DEC PDP-11 little endian high-order byte low-order byte big endian : IBM 370 , Motorola 68000 , Pyramid addr A+1 addr A
big endian high-order byte addr A low-order byte addr A+1
14
The third field of sockaddr_in is a structure of type struct in_addr which contains only a single field unsigned long s_addr. This field contains the IP address of the host. For server code, this will always be the IP address of the machine on which the server is running, and there is a symbolic constant 16
The bind() system call binds a socket to an address, in this case the address of the current host and port number on which the server will run. bind() takes three arguments,
the socket file descriptor the address to which is bound the size of the address to which it is bound.
The second argument is a pointer to a structure of type sockaddr, but what is passed in is a structure of17 type sockaddr_in, and so this must
The listen system call allows the process to listen on the socket for connections.
The first argument is the socket file descriptor The second is the size of the backlog queue: the number of connections that can be waiting while the process is handling a particular connection.The maximum size 18 permitted by most systems.
The accept() system call causes the process to block until a client connects to the server. It wakes up the process when a connection from a client has been successfully established. It returns a new file descriptor, and all communication on this connection should be done using 19 the new file descriptor.
This code reads from the socket,the read call uses the new file descriptor, the one returned by accept(), not the original file descriptor returned by socket().
20
Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the 21 client.
The close() system call closes a socket descriptor of sockfd and newsockfd and terminates program
22
23
The user needs to pass in the port number on which the server will accept connections as an 24
The socket() system call creates a new socket. It takes three arguments 1st argument is family:AF_INET is family of IPv4 Internet Protocol 2nd argument is type:SOCK_STREAM is type of connection-orient(TCP), SOCK_DGRAM is type of connectionless (UDP) 25
rd
The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET
serv_addr.sin_port = htons(portno);
serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port number to this field, it is 26
This function converts the character string from first argument, Server address into a network address structure in the AF address
if (connect(sockfd,&serv_addr,sizeof(serv_a ddr)) < 0) error("ERROR connecting");
The connect function is called by the client to establish a connection to the server. It takes three arguments
The socket file descriptor,
27
Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. 28
1. Put the accept statement and the following code in an infinite loop. 2. After a connection is established, call fork() to create a new process. 3. The child process will close sockfd and call dostuff(), passing the new socket file descriptor as an argument. When the two processes 30
31