Spread the love

How to Communicate Using TCP/IP In Java

Spread the love


TCP/Ip communication is based on client server model. It allows bi directional,point to point ,stream based connection between two machines one of which is termed as server and other of the two machine is termed as client. This is implemented using two core classes
1. Socket–read here
2. ServerSocket–read here
These two classes establishes socket connection between client and server. Sockets in java are end points of communication links between processes(Which are under execution in two machines).The socket class is to create a socket for the client and Socket server is for server.
Once the socket object is created , then it can gain access to the input and output streams associated with it.Similar to socket class ServerSocket class creates a Socket connection for the server.
The communication is simplified as:

Sl NoClientCommunicationServer
1Socket(host,port)————————>
Attempts to connect
ServerSocket(port,count)Register with this service and wait for connection
2ServerSocket.accept()
//Socket to initiate a commucation
3InputStream————————>OutputStream
4OutputStream<————————InputStream
5Socket.close()Socket.close()

Port number can be of range 0 to 65535 uniquely but 0-1023 is reserved for standard internet port.
The simple TCP/IP connection code is given as below:


import java.io.*;
import java.net.*;
class MysimpleServer{
public static void main(String args[])
{
ServerSocket server;
Socket socket;
String Message="Hello";
OutputStream oStream;
DataOutputStream dataOut;
try{
server=new ServerSocket(9876);
}
catch(IOException e){}
while(true)
{
try{
socket=server.accept();//waits here and listen for a connaction
oStream=socket.getOutputStream();
dataOut=new DataOutputStream();
dataOut.writeUTF(Message);
dataOut.close();//closes the data object when message is sent.
oStream.close();//closes the output stream on the current socket.
socket.close();//closes the current socket connection
}
catch(IOException e){}
//listen to the other clients
}
}
}

DataOutputStream is used to send message in binary bit String format to portability.The writeUTF(String) is a function defined in DataOutputStream class to write a String Object.
The client code is as follows:


import java.io.*;
import java.net.*;
class MySimpleClient{
public static void main(String args[])
{
int c;
Socket socket;
InputStream inputStream;
DataInputStreram dataIn;
socket=new Socket("New Server",9876);
//create a socket to connect the server "New server at port 9876"
inputStream=socket.getInputStream();
dataIn=new DataInputStream(inputStream);
//get an input stream in binary
String msg=new String(dataIn.readUTF());
//read the input stream.
System.out.println(msg);
dataIn.close();
inputStream.close();
socket.close();
}
}

client will connect the machine with a server called new Server having machine id 9876 as port no. If server sends some message,it will be displayed on client.

READ  Hire Java Builders With Working experience in Top 10 Java Frameworks

READ  Learn Package In Java Fast and Easy Manner




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis of instructions,...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...

EDITOR PICKS


Spread the love