Socketsand Socket Address Structure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/283284085

Sockets and Socket Address Structure

Article · October 2012

CITATIONS READS

0 4,632

2 authors, including:

Mohit Mittal
Knowtion GmbH
75 PUBLICATIONS   708 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Cyber Security View project

software product line View project

All content following this page was uploaded by Mohit Mittal on 28 October 2015.

The user has requested enhancement of the downloaded file.


International Journal of Engineering Research & Technology (IJERT)
ISSN: 2278-0181
Vol. 1 Issue 8, October - 2012

Sockets and Socket Address Structure

Mohit Mittal1
1
Assistant Professor,
Anand College of Engineering & Management, Kapurthala.

Tarun Bhalla2
2
Assistant Professor,
Anand College of Engineering & Management, Kapurthala.

Abstract:-In this paper specifies the concept of The parameters have different formats for
socket and socket address structures. In this, we have different protocols.
discussed how communication has been performed  The network interface must support different
between two hosts and discussed the role of the sockets. protocols. These protocols may use different-
Sockets address of IPv4 and IPv6 is defined. The socket size variable for addresses and other fields. [3]
function call is also included which consists of various
main functions like socket, connect, listen, bind, accept
and close. It consists of two types of servers which uses
socket function.

Keywords:Introduction, socket and its address,


socket function calls, TCP socket call, and servers.

I. Introduction: Java's socket model is derived from


BSD (UNIX) sockets, introduced in the early 1980s for
RT

inter-process communication using IP, the Internet


Protocol. The Internet Protocol breaks all
communications into packets, finite-sized chunks of
IJE

data which are separately and individually routed from


source to destination. IP allows routers, bridges, etc. to
drop packets--there is no delivery guarantee. Packet size
is limited by the IP protocol to 65535 bytes. Of this, a
minimum of 20 bytes is needed for the IP packet
header, so there is a maximum of 65515 bytes available
for user data in each packet.
Sockets are a means of using IP to communicate
between machines, so sockets are one major feature that
allows Java to interoperate with legacy systems by
simply talking to existing servers using their pre-
defined protocol. [1]

2. API: The application interface is the interface


available to the programmer for using the
communication protocols. The API is depends to the OS “Figure 1: Socket Interface.” [2]
the programming language.
We discuss the socket API. With sockets, the network 3. Socket Address Structure:A Character
connection can be used as a file. Network I/O is, Recognition deal with the problem of reading offline
however, more complicated than file I/O because: handwritten character i.e. at some point in time (in
 Asymmetric. The connection requires the mins, sec, hrs.) after it has been written. However
program to know which process it is, the client recognition of unconstrained handwritten text can be
or the server. very difficult because characters cannot be reliably
 A network connection that is connection- isolated especially when the text is cursive handwriting.
oriented is somewhat like opening a file. A [2]
connectionless protocol doesn’t have anything
like an open.
/* Generic Socket Address Structure, length=16*/
 A network application needs additional
information to maintain protections, for <sys/socket.h>
example, of the other process. Structsocketaddr
 There are more parameters required to specify {
network connection than the file Input/Output. unit8_t sa_len;
www.ijert.org 1
International Journal of Engineering Research & Technology (IJERT)
ISSN: 2278-0181
sa_family_tsa_family; /* address family: Vol. 1 Issue 8, October - 2012
AF_XXX value */

char sa_data[14] /*up to 14 types of protocol-


specific address */
}; [3]

/* Ipv4 Socket Address Structure, length=16*/


<netinet/in.h>
structin_addr
{
in_addr_ts_addr; /* 32-bit IPv4 address,
network byte ordered */
};

structsockaddr_in
{
unit8_t sin_len; /* length of structure
(16 byte) */
sa_family_tsin_family; /*AF_INET*/
in_port_tsin_port; /* 16-bit TCP or UDP
port number, network byte ordered */

structin_addrsin_addr; /*32-bit Ipv4 address,


network byte ordered */
charsin_zero[8]; /* unused – initialize to all
zeroes */
};
RT

/* Ipv6 Socket Address Structure, length=24*/


<netinet/in.h>
IJE

struct in6_addr {
unit8_t s6_addr[16]; /* 128-bit Ipv6
address, network byte ordered */
};

#define SIN6_LEN /* required for compile-


time tests */

struct sockaddr_in6
{
unit8_t sin6_len; /* length of this structure
(24byte) */
sa_family_t sin6_family; /*AF_INET6*/
in_port_t sin6_port; /* 16-bit TCP or
UDP port number, network byte ordered */
}; [3]

4. Procedure of Socket Programming


In order to communicate between two processes, the
two processes must provide the formation used by
ICP/IP (or UDP/IP) to exchange data. This information
is the 5-tupe: {protocol, local-addr, local-process,
foreign-addr and foreign-process}.
Several network systems calls are used to specify this
information and use the socket. [3]

“Figure 2: Difference Between TCP and UDP” [3]


www.ijert.org 2
International Journal of Engineering Research & Technology (IJERT)
ISSN: 2278-0181
Vol. 1 Issue 8, October - 2012
5. SOCKET FUNCTION CALLING Bind assigns a local protocol address to a socket.

#include <sys/types.h> Protocol address: a 32 bit IPv4 address and a 16 bit


TCP or UDP port number.
#include <sys/socket.h>
sockfd: a socket descriptor returned by the socket
Socket Function function.
*myaddr: a pointer to a protocol-specific address.[2]
int socket (int family, int type, int protocol); addrlen: the size of the socket address structure.
Family: specifies the protocol family {AF_INET for Servers bind their “well-known port” when they start.
TCP/IP}
Returns on success: 0
Type: indicates communications semantics
On error : -1
SOCK_STREAM stream socket TCP
Example:
SOCK_DGRAM datagram socket UDP If (bind (sd, (structsockaddr *) &servaddr ,sizeof
(servaddr)) != 0)
SOCK_RAW raw socket errsys(“bind call error”);

Protocol: set to 0 except for raw sockets

Returns on success: socket descriptor {a small


nonnegative integer}

On error: -1

if (( sd= socket (AF_INET, SOCK_STREAM, 0)) < 0)

err_sys(“socket call error”);


RT

Connect Function

intconnect(intsockfd,conststructsockaddr*servaddr,
IJE

socklen_taddrlen);

sockfd: a socket descriptor returned by the socket


function.

*servaddr: a pointer to a socket address structure


addrlen: the size of the socket address structure

The socket address structure must contain the IP


address and the port number for the connection wanted. “Figure 3: TCP socket calls” [2]
In TCP connect initiates a three-way handshake.
Connect returns only when the connection is
Listen Function
established or when an error occurs.
intlisten ( intsockfd, intbacklog );[2]
Returns on success: 0
on error: -1 Listen is called only by a TCP server and performs two
actions:
Example:
if ( connect (sd, (structsockaddr*) &servaddr, 1. Converts an unconnected socket (sockfd) into
sizeof(servaddr)) != 0) a passive socket.
2. Specifies the maximum number of
err_sys(“connectcall error”); connections (backlog) that the kernel should
queue for this socket.

Listen is normally called before the accept function.


6. TCP SOCKET CALLS:
Returns on success: 0
Bind Function on error: -1
Example:
intbind (intsockfd, conststructsockaddr* myaddr,
socklen_taddrlen); if (listen (sd, 2) != 0)
www.ijert.org 3
International Journal of Engineering Research & Technology (IJERT)
ISSN: 2278-0181
errsys(“listen call error”); 7. TWO TYPES OF SERVER Vol. 1 Issue 8, October - 2012
Accept Function
Concurrent server – forks a new process, so multiple
intaccept ( intsockfd , structsockaddr*cliaddr, clients can be handled at the same time.
socklen_t*addrlen); Iterative server – the server processes one request
before accepting the next.

Accept is called by the TCP server to return the next Concurrent Server
completed connection from the front of the completed listenfd = socket(…);
connection queue. bind(listenfd,…);
listen(listenfd,…)
sockfd: This is the same socket descriptor as in for ( ; ; ) {
listencall. connfd = accept(listenfd, …);
If (( pid = fork()) == 0) { /* child*/
*cliaddr: used to return the protocol address of the close(listenfd);
connected peer process (i.e., the client process).[2] /* process the request */
close(connfd);
*addrlen: {this is a value-result argument} exit(0);
}
Before the accept call:We set the integer value pointed close(connfd); /* parent*/
to by *addrlento the size of the socket address structure }[3]
pointed to by *cliaddr;
Iterative Server
listenfd = socket(…);
on return from the accept call: This integer value bind(listenfd,…);
contains the actual number of bytes stored in the socket listen(listenfd,…)
address structure. for ( ; ; ) {
connfd = accept(listenfd, …);
Returns on success: a new socket descriptor /* process the request */
on error : -1 close(connfd);
}[3]
RT

For accept the first argument sockfdis the listening


socketand the returned value is the connected socket. Client
sockfd = socket(…);
IJE

The server will have one connected socket for each connect(sockfd, …)
client connection accepted. /* process the request */
When the server is finished with a client, the connected close(sockfd);[3]
socket must be closed.

Example: CONCLUSION
sfd= accept (sd, NULL, NULL); In this paper ,we have concluded that the socket
interface generally holds the communication between
the user and the kernel. Also the two different
if (sfd== -1) err_sys (“accept error”);[2] applications can be interfaced through the
communication network. In TCP socket calls, the client
Close Function sends the request to the server and the server performs
all the functions i.e. socket(), bind(), listen() and
intclose (intsockfd); accept(). In concurrent servers, multiple clients can be
handledat the same time, whereas in iterative server, the
server processes only one request before accepting the
Close marks the socket as closed and returns to the
next one. The Internet Protocol breaks all
process immediately.
communications into packets, finite-sized chunks of
data which are separately and individually routed from
sockfd: This socket descriptor is no longer useable.
source to destination.
Note – TCP will try to send any data already queued to
REFERENCES
the other end before the normal connection termination
sequence. [1] Elementary TCP Sockets UNIX Network
Programming Vol. 1, Second Ed.
Returns on success: 0 Stevens Chapter 4
on error : -1
http://www.cs.usfca.edu/~parrt/doc/java/Sockets-
Example: notes.pdf
close (sd); [2]
[2] UNIX Network Programming by W. Richard
Stevens, Prentice Hall,
www.ijert.org 4
International Journal of Engineering Research & Technology (IJERT)
ISSN: 2278-0181
Englewood Cliffs, NJ, 1997.
Vol. 1 Issue 8, October - 2012
http://web.cs.wpi.edu/~rek/Undergrad_Nets/B
06/TCP_Sockets.pdf

[3] Unix Network Programming, W.R. Stevens,


1990,Prentice-Hall, Chapter 6.

[4] Unix Network Programming, W.R. Stevens,


1998,Prentice-Hall, Volume 1,
Chapter 3-
4.http://www.ece.eng.wayne.edu/~gchen/ece56
50/lecture7.pdf

Mohit Mittal received his B.Tech


and M.Tech degree in Computer
Science from Guru Nanak Dev
University, in 2011. He is
working as Assistant Professor in Anand
college of Engineering and Management,
Kapurthala. His research areas include image
processing, computer networks and Network
Security.

Tarun Bhalla received his B.Tech


degree in Computer Science from
Punjab Technical University. He is
RT

currently working as a Assistant


Professor in Anand College of
IJE

Engineering and Management, Kapurthala.


His research interest area includes Database,
Network Security, Mobile Computing and
adhoc network .

www.ijert.org 5
View publication stats

You might also like