Client Server Program in java |Java TCP Client – Server Program | Client Server Program in java |Socket
What is TCP Socket ?
- A socket 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
TCP socket Connection
- To make a connection request, the client makes a connection request to the server on the server’s machine and port
- The client identifies itself with a local port number (usually assigned by the system) during the connection
- If everything goes well, the server accepts the connection Upon acceptance, the server gets a new socket bound to the local endpoint bound to the same port and remote endpoint bound to the address and port of the client
- The original socket continues to listen for connection requests
- On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server
- The java.net package provides Server Socket and Socket classes to create server and client sockets
TCP Socket Connection in Figure
Server Side Program | Client Server Program in java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package server;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author Achyut
*/
public class Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
final int PORT = 1234;
System.out.println("Server started.");
System.out.println("Listening to client...");
try (
ServerSocket serverSocket = new ServerSocket(PORT);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
Scanner in = new Scanner(clientSocket.getInputStream());
) {
while (true) {
String input = in.nextLine();
if (input.equalsIgnoreCase("exit")) break;
System.out.println("Received from client: " + input);
double radius = Double.valueOf(input);
double area = Math.PI* radius *radius ;
out.println(area);
}
}
System.out.println("Server stopped");
}
}
Client Side Program | Client Server Program in java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package client;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author 5442
*/
public class Client {
public static void main(String[] args) throws IOException {
final String HOST = "127.0.0.1";
final int PORT = 1234;
System.out.println("Client started.");
try (
Socket socket = new Socket(HOST, PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner in = new Scanner(socket.getInputStream());
Scanner s = new Scanner(System.in);
) {
while (true) {
System.out.print("Input: ");
String input = s.nextLine();
out.println(input);
if (input.equalsIgnoreCase("exit")) break;
System.out.println("Echoed from server: " + in.nextLine());
}
}
}
}
Download the File of Client Server Program in java
Download Server side Program and client side program in Java
Also see :
Serving Multiple Clients program in Java | Multi Client -server Program in Java