Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

FNL Socket

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

 Socket programming in Java is used for communication between

the applications that are running on different JRE. It can be


either connection-oriented or connectionless. On the whole, a
socket is a way to establish a connection between a client and a
server.

What is Socket Programming in Java?

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
in order to form a connection.

The server forms the listener socket while the client reaches out to
the server. Socket and Server Socket classes are used for connection-
oriented socket programming.

Now let’s understand the core concept of Socket Programming i.e. a


socket.
What is a Socket in Java?

A socket in Java is one endpoint of a two-way communication link


between two programs running on the network. A socket is bound
to a port number so that the TCP layer can identify the application
that data is destined to be sent to.

An endpoint is a combination of an IP address and a port number.


The package in the Java platform provides a class, Socket that
implements one side of a two-way connection between your Java
program and another program on the network. The class sits on top
of a platform-dependent implementation, hiding the details of any
particular system from your Java program. By using the class
instead of relying on native code, your Java programs can
communicate over the network in a platform-independent fashion.

Now that you know, what is Socket in Java, let’s move further and
understand how does client communicates with the server and how
the server responds back.

Client-Side Programming

In the case of client-side programming, the client will first wait for
the server to start. Once the server is up and running, it will send the
requests to the server. After that, the client will wait for the response
from the server. So, this is the whole logic of client and server
communication. Now let’s understand the client-side and server-
side programming in detail.

In order to initiate a clients request, you need to follow the below-


mentioned steps:

1. Establish a Connection

The very first step is to establish a socket connection. A socket


connection implies that the two machines have information about
each other’s network location (IP Address) and TCP port.

You can create a Socket with the help of a below statement:

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

 Here, the first argument represents the IP address of Server.

 The second argument represents the TCP Port. (It is a number


that represents which application should run on a server.)

2. Communication

In order to communicate over a socket connection, streams are used


for both input and output the data. After establishing a connection
and sending the requests, you need to close the connection.

3. Closing the connection


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

Now let’s see how to write a Java program to implement socket


connection at the client-side.
import java.net.*;

import java.io.*;

class MyClient{

public static void main(String args[])throws Exception{

Socket s=new Socket("localhost",3333);

DataInputStream din=new DataInputStream(s.getInputStream());

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str="",str2="";

while(!str.equals("stop")){

str=br.readLine();

dout.writeUTF(str);

dout.flush();

str2=din.readUTF();

System.out.println("Server says: "+str2);

dout.close();

s.close();

}}
Now, let’s implement server-side programming and then arrive at
the output.

Server-Side Programming

Basically, the server will instantiate its object and wait for the client
request. Once the client sends the request, the server will
communicate back with the response.

In order to code the server-side application, you need two sockets


and they are as follows:

 A ServerSocket which waits for the client requests (when a


client makes a new Socket())

 A plain old socket for communication with the client.

After this, you need to communicate with the client with the
response.

Communication

getOutputStream() method is used to send the output through


the socket.

Close the Connection

It is important to close the connection by closing the socket as well


as input/output streams once everything is done.
Now let’s see how to write a Java program to implement socket
connection at server side.
import java.net.*;

import java.io.*;

class MyServer{

public static void main(String args[])throws Exception{

ServerSocket ss=new ServerSocket(3333);

Socket s=ss.accept();

DataInputStream din=new DataInputStream(s.getInputStream());

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str="",str2="";

while(!str.equals("stop")){

str=din.readUTF();

System.out.println("client says: "+str);

str2=br.readLine();

dout.writeUTF(str2);

dout.flush();

din.close();

s.close();

ss.close();

}}

After configuring both client and server end, you can execute the
server-side program first. After that, you need to run the client side
program and send the request. As soon as the request is sent from
the client end, the server will respond back. Below snapshot
represents the same.

1. When you run the server-side script, it will start and wait for the
client to get started.

2. Next, the client will get connected and inputs the request in the
form of a string.

3. When the client sends the request, the server will respond back.

That’s how you need to execute a socket program in Java. You can
also execute these programs on a terminal window or a command
prompt. But, as Eclipse is well advanced with its features, you can
simply execute both the programs on a console.

This brings us to the end of the article on Socket Programming in


Java. I hope I have thrown some light on to your knowledge
on Socket Programming.

You might also like