EmbLogic's Blog

article on socket programming

ARTICLE ON SOCKET PROGRAMMING [LINUX]

A LINUX socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes WHICH allow one process to communicate with another whether it is local on the same computer system or remote over the network. Many other higher level protocols are built upon sockets technology.
Sockets utilize the following standard protocols:

Protocol Description
IP Internet Protocol provides network routing using IP addressing eg 192.168.1.204
UDP User Datagram Protocol – IP with ports to distinguish among processes running on same host. No data verification.
TCP Transmission Control Protocol – IP with ports to distinguish among processes running on same host.Connection oriented, stream
transfer, full duplex, reliable with data verification.

Typically one configures a socket server to which a socket client may attach and communicate.
Create the socket instance.
IN THIS THE STEPS ARE DESCRIBED HOW WE CREATE SOCKET & HOW SERVER AND CLINT INTERACT WITH EACH OTHER.
1-Socket function prototype:
int sockfd = socket(int socket_family, int socket_type, int protocol)
discription: Choose socket communications family/domain
according to the communication within same host or over the network we choose the family as AF_UNIX,OR AF_INET ACCORDINGLY.

Choose socket type:

TCP: SOCK_STREAM for connection oriented services using tcp protocol.
UDP: SOCK_DGRAM for connectionless services using UDP protocol.

Choose socket protocol: (See /etc/protocols)
for AF_UNIX & AF_INET or Internet Protocol (IP): 0 or IPPROTO_IP
for ICMP: 1
2-Configure the socket as a client or server:
Socket Server            Socket Client
socket()                      socket()
bind()
listen()
accept()                        connect()
recv()/send()            recv()/send()
close() close()
This is specific to whether the application is a socket client or a socket server.
above all calls are listed which we use in socket as server & clint configuration.
the detail discription of calls are as…
Socket server:
bind(): bind the socket to a local socket address. This assigns a name to the socket.Function prototype:
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
Bind arguments:
int sockfd: Socket file descriptor. Returned by call to “socket”.
struct sockaddr: Socket information structure
socklen_t addrlen: Size of structure
Returns 0: Sucess, -1: Failure and errno may be set.
listen(): listen for connections on a socket created with “socket()” and “bind()” and accept incoming connections. This is used for TCP and not UDP. Zero is returned on success.Function prototype:
int listen(int s, int backlog);
Listen arguments:
int s: Socket file descriptor. Returned by call to “socket”. Identifies a bound but unconnected socket.
int backlog: Set maximum length of the queue of pending connections for the listening socket. A reasonable value is 10. Actual maximum permissible: SOMAXCONN .
accept(): accept a connection on a socket. Accept the first connection request on the queue of pending connections, create a new connected socket with mostly the same properties as defined by the call to “socket()”, and allocate a new file descriptor for the socket, which is returned. The newly created socket is no longer in the listening state. Note this call blocks until a client connects.
Function prototype:
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
Accept arguments:
int s: Socket file descriptor. Returned by call to “socket”.
struct sockaddr *addr: Pointer to a sockaddr structure. This structure is filled in with the address of the connecting entity.
socklen_t *addrlen: initially contains the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. When addr is NULL nothing is filled in.
Returns:
Success: non-negative integer, which is a descriptor of the accepted socket. Argument “addrlen” will have a return value.
Fail: -1, errno may be set

Socket client:
connect(): initiate a connection with a remote entity on a socket. Zero is returned on success. Support both TCP (SOCK_STREAM) and UDP (SOCK_DGRAM). For SOCK_STREAM, an actual connection is made. For SOCK_DGRAM the address is the address to which datagrams are sent and received.
Connect function prototype:
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
Connect arguments: (Same as server’s bind() arguments)
int sockfd: Socket file descriptor. Returned by call to “socket”.
struct sockaddr: Socket information structure
socklen_t addrlen: Size of structure
Returns 0: Sucess, -1: Failure and errno may be set.
Zero is returned upon success and on error, -1 and errno is set appropriately.

here description of sockaddr_in data structure that how we name & provide address to socket.

socketInfo.sin_family = AF_INET;
// Use any address available to the system. This is a typical configuration for a server.
// Note that this is where the socket client and socket server differ.
// A socket client will specify the server address to connect to.
socketInfo.sin_addr.s_addr = htonl(“ip addd”); // Translate long integer to network byte order.
socketInfo.sin_port = htons(portNumber); // Set port number

so this is all about socket programming in brief….

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>