Java Socket Programming Using TCP

We have discussed the fundamentals of networking recently.Now it is clear that the transport layer of TCP/IP contains TCP as well as UDP. Our Java application is in application layer. The Java application can use either TCP or UDP for communication. In the previous chapter we discussed the use of UDP with example.In this chapter , we are discussing Java socket programming using TCP with suitable example.

Java Socket Programming using TCP

TCP is the acronym for Transmission Control Protocol.As we discussed earlier it is connection oriented.The concept is explaining here with an example.

Our example has two java classes.One is acting as the server and the other is the  client.

The TCPSocketServer.java is the server in our case.Lets examine the code.

TCPSocketServer.java

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class TCPSocketServer {
private ServerSocket serverSocket = null;
private Socket socket = null;
private InputStream inStream = null;
private OutputStream ouStream = null;

public TCPSocketServer() {

}

public void communicate() {
try {
serverSocket = new ServerSocket(4545);
socket = serverSocket.accept();
inStream = socket.getInputStream();
ouStream = socket.getOutputStream();
while (socket.isConnected()) {
byte[] readBuffer = new byte[1024];
int numBytes = inStream.read(readBuffer);
byte[] tempBuffer = new byte[numBytes];
System.arraycopy(readBuffer, 0, tempBuffer, 0, numBytes);
String message = new String(tempBuffer, "UTF-8");
System.out.println("message from client = " + message);
String reply = "Thank you !!.This is from server";
byte[] replyBytes = reply.getBytes();
ouStream.write(replyBytes);
System.out.println("reply has written to socket");
Thread.sleep(2000);
socket.close();
}
} catch (SocketException se) {
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TCPSocketServer server = new TCPSocketServer();
server.communicate();
}
}

The server code first creates a ServerSocket object.It can accept incoming socket connections .Once a connection is received , server is waiting to read  the message string.After reading the message  it is sending a response message and then closes the socket connection.

Now lets examine the client code.

TCPSocketClient.java

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;

public class TCPSocketClient {
private Socket socket = null;
private InputStream inStream = null;
private OutputStream ouStream = null;

public TCPSocketClient() {

}

public void communicate() {
try {
socket = new Socket("localhost", 4545);
inStream = socket.getInputStream();
ouStream = socket.getOutputStream();
while (socket.isConnected()) {
String messageString = "Hello ...This is from client";
ouStream.write(messageString.getBytes());
System.out.println("Message has written to socket");
byte[] byteBuffer = new byte[1024];
int numBytes = inStream.read(byteBuffer);
byte[] tempBuffer = new byte[numBytes];
System.arraycopy(byteBuffer, 0, tempBuffer, 0, numBytes);
String message = new String(tempBuffer, "UTF-8");
System.out.println("Message from server = " + message);
Thread.sleep(2000);
socket.close();
}
} catch (SocketException se) {
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
TCPSocketClient client = new TCPSocketClient();
client.communicate();
}
}

The client application creates a socket connection to the specified address and port.(In the code shown above , we are using  local host .If our server is running in a different machine , we need to provide the ip address of that machine while creating socket connection from client. )  Then sending a message to server . After getting response from server it is closing the socket.

Output

Run  TCPSocketServer.java first  and then  TCPSocketClient.java.

Output of TCPSocketServer.java

message from client = Hello …This is from client

reply has written to socket

Output of  TCPSocketClient.java

Message has written to socket

Message from server = Thank you !!.This is from server

See also:

Networking with Java overview

Java socket communication with UDP

Leave a Reply

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