Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

NETWORKING - Unix Socket Programming

Loading...

Published in: Networking
1,190 Views

This document provides information about Unix Socket Programming in Networking.

Debarun S / Kolkata

10 years of teaching experience

Qualification: B.Tech

Teaches: Computer, Mathematics, Science, Chemistry, Computer Science, Physics, School Level Computer, Defence Exams, IBPS, Insurance Exams, SSC Exams

Contact this Tutor
  1. Unix Socket Programming Client Server Architecture In the client server architecture, a machine(refered as client) makes a request to connect to another machine (called as server) for providing some service. The services running on the server run on known ports(application identifiers) and the client needs to know the address of the server machine and this port in order to connect to the server. On the other hand, the server does not need to know about the address or the port of the client at the time of connection initiation. The first packet which the client sends as a request to the server contains these informations about the client which are further used by the server to send any information. Client acts as the active device which makes the first move to establish the connection whereas the server passively waits for such re uests from some client. response client server search data. Illustration of Client Server Model What is a Socket ? In unix, whenever there is a need for inter process communication within the same machine, we use mechanism like signals or pipes(named or unnamed). Similarly, when we desire a communication between two applications possibly running on different machines, we need sockets. Sockets are treated as another entry in the unix open file table. So all the system calls which can be used for any 10 in unix can be used on socket. The server and client applications use various system calls to conenct which use the basic construct called socket. A socket is one end of the communication channel between two applications running on different machines. Steps followed by client to establish the connection: 1. Create a socket 2. Connect the socket to the address of the server 3. Send/Receive data 4. Close the socket Steps followed by server to establish the connection:
  2. 1. 2. 3. 4. 5. Create a socket Bind the socket to the port number known to all clients Listen for the connection request Accept connection request Send/Receive data Basic data structures used in Socket programming Socket Descriptor: A simple file descriptor in Unix. int Socket Address: This construct holds the information for socket address struct sockaddrs { unsigned short char sa family; sa data [14] • // address family, AF x x x or PF x x x // 14 bytes of protocol address AF stands for Address Family and PF stands for Protocol Family. In most modern implementations only the AF is being used. The various kinds of AF are as follows: UNIX, AF LOCAL INET INET6 IPX NETLINK AX25 ATMPVC APPLETALK PACKET Purpose Local comrnunication IPv4 Internet protocols IPv6 Internet protocols Novell protocols IPX Kernel user interface device X. 25 / ISO—8208 protocol ITU-T Arnateur radio AX .25 protocol Access to raw ATM P VCs Applet alk Low level packet interface In all the sample programs given below, we will be using AF_INET. struct sockaddr_in: This construct holds the information about the address family, port number, Internet address,and the size of the struct sockaddr. struct sockaddr in short int unsigned short struct in addr unsigned char int sin sin sin sin family; port; addr ; zero [8] • Address family Port number Internet address Same size as struct sockaddr Some systems (like x8086) are Little Endian i-e. least signficant byte is stored in the higher address, whereas in Big endian systems most significant byte is stored in the higher address. Consider a situation where a Little Endian system wants to communicate with a Big Endian one, if there is no standard for data representation then the data sent by one machine is misinterpreted by the other. So standard has been defined for the data representation in the network (called Network Byte Order) which is the Big Endian. The system calls that help us to convert a short/long from Host Byte order to Network Byte Order and viceversa are
  3. htons() -- "Host to Network Short" htonl() -- "Host to Network Long" , ntohs() -- "Network to Host Short" ntohl() -- "Network to Host Long" IP addresses Assuming that we are dealing with IPv4 addresses, the address is a 32bit integer. Remembering a 32 bit number is not convenient for humans. So, the address is written as a set of four integers seperated by dots, where each integer is a representation of 8 bits. The representation is like a.b.c.d, where a is the representation of the most significant byte. The system call which converts this representation into Network Byte Order is: int i net aton (const char *cp, struct in addr *inp) inet_aton() converts the Internet host address cp from the standard numbers-and-dots notation into binary data and stores it in the structure that inp points to. inet_aton returns nonzero if the address is valid, zero if not. For example, if we want to initialize the sockaddr_in construct by the IP address and desired port number, it is done as follows: struct sockaddr in sockaddr; sockaddr. sin family = AF IN ET; sockaddr. sin port htons (21) i net a ton ("172.26.117.168" & (sockaddr. sin addr) ) memset ( & (sockaddr. sin zero) 8), Socket System Call A socket is created using the system call: int socket( domain protocol) t ype This system call returns a Socket Descriptor (like file descriptor) which is an integer value. Details about the Arguments: 1. 2. 3. Domain: It specifies the communication domain. It takes one of the predefined values described under the protocol family and address family above in this lecture. Type: It specifies the semantics of communication , or the type of service that is desired . It takes the following values: 0 SOCK STREAM : Stream Socket 0 SOCK_DGRAM : Datagram Socket 0 SOCK RAW : Raw Socket 0 SOCK_SEQPACKET : Sequenced Packet Socket 0 SOCK_RDM : Reliably Delivered Message Packet Protocol: This parameter identifies the protocol the socket is supposed to use . Some values are as follows: IPPROTO_TCP : For TCP (SOCK_STREAM) o
  4. 0 IPPROTO_UDP : For UDP (SOCK_DRAM) Since we have only one protocol for each kind of socket, it does not matter if we do not define any protocol at all. So for simplicity, we can put "0" (zero) in the protocol field. Bind System Call The system call bind associates an address to a socket descriptor created by socket. int addr len ) int bind ( int sockfd struct sockaddr *myaddr The second parameter myaddr specifies a pointer to a predefined address of the socket.lts structure is a general address structure so that the bind system call can be used by both Unix domain and Internet domain sockets. Other System Calls and their Functions LISTEN : Annoumce willingness to accept connections ; give queue size. ACCEPT : Block the caller until a commwction attempt arrives. CONNECT : Actively attempt to establish a connection. SEND : Send some data over the connection. RECIEVE : Recieve sme data from the connection. CLOSE : Release the connection.