Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views6 pages

Study of Socket Programming

Uploaded by

ramalingamt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Study of Socket Programming

Uploaded by

ramalingamt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

STUDY OF SOCKET PROGRAMMING

Aim:

To study about Socket programming with client – server model

The term network programming refers to writing programs that execute across multiple devices
(computers), in which the devices are all connected to each other using a network. Two
common network protocols are

1. TCP − TCP stands for Transmission Control Protocol, which allows for reliable
communication between two applications. TCP is typically used over the Internet
Protocol, which is referred to as TCP/IP.
2. UDP − UDP stands for User Datagram Protocol, a connection-less protocol that allows
for packets of data to be transmitted between applications.

• Socket Programming − This is the most widely used concept in Networking. Socket
programming is a way of connecting two nodes on a network to communicate with each
other. One socket (node) listens on a particular port at an IP, while other socket reaches
out to the other to form a connection. Server forms the listener socket while client
reaches out to the server.
• URL Processing − This would be covered separately. Click here to learn about URL
Processing.
Socket Programming

Sockets provide the communication mechanism between two computers using TCP. A client
program creates a socket on its end of the communication and attempts to connect that socket
to a server.When the connection is made, the server creates a socket object on its end of the
communication. The client and the server can now communicate by writing to and reading
from the socket.

The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a
mechanism for the server program to listen for clients and establish connections with them.

The following steps occur when establishing a TCP connection between two computers using
sockets −

1. The server instantiates a ServerSocket object, denoting which port number


communication is to occur on.
2. The server invokes the accept() method of the ServerSocket class. This method waits
until a client connects to the server on the given port.
3. After the server is waiting, a client instantiates a Socket object, specifying the server
name and the port number to connect to.
4. The constructor of the Socket class attempts to connect the client to the specified server
and the port number. If communication is established, the client now has a Socket object
capable of communicating with the server.
5. On the server side, the accept() method returns a reference to a new socket on the server
that is connected to the client's socket.

After the connections are established, communication can occur using I/O streams. Each socket
has both an OutputStream and an InputStream. The client's OutputStream is connected to the
server's InputStream, and the client's InputStream is connected to the server's OutputStream.

TCP is a two-way communication protocol, hence data can be sent across both streams at the
same time. Following are the useful classes providing complete set of methods to implement
sockets.

Stages for server:

1.Socket creation:
int sockfd = socket(domain, type, protocol)

sockfd: socket descriptor, an integer (like a file-handle)


integer, communication domain e.g., AF_INET (IPv4 protocol) ,
domain:
AF_INET6 (IPv6 protocol)
Communication type SOCK_STREAM: TCP(reliable, connection
type:
oriented) SOCK_DGRAM: UDP(unreliable, connectionless)
Protocol value for Internet Protocol(IP), which is 0. This is the same
protocol: number which appears on protocol field in the IP header of a
packet.(man protocols for more details)

Setsockopt:

int setsockopt(int sockfd, int level, int optname,


const void *optval, socklen_t optlen);

This helps in manipulating options for the socket referred by the file descriptor sockfd.
This is completely optional, but it helps in reuse of address and port. Prevents error
such as: “address already in use”.

2. Bind:

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

After creation of the socket, bind function binds the socket to the address and port
number specified in addr(custom data structure). In the example code, we bind the
server to the localhost, hence we use INADDR_ANY to specify the IP address.

3. Listen:

int listen(int sockfd, int backlog);

It puts the server socket in a passive mode, where it waits for the client to approach the
server to make a connection. The backlog, defines the maximum length to which the
queue of pending connections for sockfd may grow. If a connection request arrives
when the queue is full, the client may receive an error with an indication of
ECONNREFUSED.

4. Accept:

int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

It extracts the first connection request on the queue of pending connections for the
listening socket, sockfd, creates a new connected socket, and returns a new file
descriptor referring to that socket. At this point, connection is established between
client and server, and they are ready to transfer data.

Stages for Client

1. Socket connection: Exactly same as that of server’s socket creation


Connect:
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

The connect() system call connects the socket referred to by the file descriptor sockfd
to the address specified by addr. Server’s address and port is specified in addr.

Client Side Programming


1.Establish a Socket Connection

To connect to other machine we need a socket connection. A socket connection means


the two machines have information about each other’s network location (IP Address) and TCP
port.The java.net.Socket class represents a Socket. To open a socket:

Socket socket = new Socket(“127.0.0.1”, 5000);

• First argument – IP address of Server. ( 127.0.0.1 is the IP address of localhost, where


code will run on single stand-alone machine).
• Second argument – TCP Port. (Just a number representing which application to run on
a server. For example, HTTP runs on port 80. Port number can be from 0 to 65535)

2.Communication

To communicate over a socket connection, streams are used to both input and output
the data.

3.Closing the connection

The socket connection is closed explicitly once the message to server is sent.

Server Programming

1.Establish a Socket Connection

To write a server application two sockets are needed.


ServerSocket obj= new ServerSocket(int port);
Creates a server socket, bound to the specified port.
ServerSocket obj= new ServerSocket(int port, int backlog)
Creates a server socket and binds it to the specified local port number, with the specified
backlog.

ServerSocket obj= new ServerSocket(int port, int backlog, InetAddress bindAddr)


Create a server with the specified port, listen backlog, and local IP address to bind to.

• A ServerSocket which waits for the client requests (when a client makes a new
Socket())
• A plain old Socket socket to use for communication with the client.

2.Communication

Java DataOutputStream class allows an application to write primitive Java data types
to the output stream in a machine-independent way.Java application generally uses the data
output stream to write data that can later be read by a data input stream.

Syntax: DataOutputStream data = new DataOutputStream(Socketobject.getOu


tputStream());

getOutputStream() method is used to send the output through the socket.

The java.io.DataOuputStream.writeUTF(String str) method writes a string to the


underlying output stream using modified UTF-8 encoding.

Syntax: DataInputStream inst = new DataInputStream(socketobject. getInputStream());

getInputStream() method is used to receive the output through the socket.

3.Close the Connection

After finishing, it is important to close the connection by closing the socket as well as
input/output streams.

Important Points

• Server application makes a ServerSocket on a specific port which is 5000. This starts
our Server listening for client requests coming in for port 5000.
• Then Server makes a new Socket to communicate with the client.
socket = server.accept()

• The accept() method blocks(just sits there) until a client connects to the server.
• Then we take input from the socket using getInputStream() method. Our Server keeps
receiving messages until the Client sends “Over”.
• After we’re done we close the connection by closing the socket and the input stream.
• To run the Client and Server application on your machine, compile both of them. Then
first run the server application and then run the Client application.

Result:
Thus, the study of Socket Programming was done on client server model.

You might also like