0% found this document useful (0 votes)
35 views3 pages

1ATCP

The document contains code for a TCP client and server program written in Java. The client program takes in two numbers from the user, sends them to the server, and prints the response from the server. The server program receives the two numbers, calculates their sum, and sends the result back to the client.

Uploaded by

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

1ATCP

The document contains code for a TCP client and server program written in Java. The client program takes in two numbers from the user, sends them to the server, and prints the response from the server. The server program receives the two numbers, calculates their sum, and sends the result back to the client.

Uploaded by

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

TCPClient.

java
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class TCPClient
{
public static void main(String [] args)
{
Scanner userInput = new Scanner(System.in);
System.out.println("Enter Server Address: ");
String serverName;
serverName = userInput.next();
/* System.out.println("Enter Port Number: ");
String port;
port = userInput.next(); */
try
{
int serverPort=7889;
/*System.out.println("Connecting to " + serverName + " on port " +
port);*/
Socket client = new Socket(serverName, serverPort);
System.out.println("Just connected to " +
client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
System.out.println("Enter a first number: ");
//userInput.nextInt();
Integer x= userInput.nextInt();
System.out.println("Enter a second number: ");
// userInput.nextInt();
Integer y= userInput.nextInt();
// System.out.println("hello");
out.writeInt(x);
out.writeInt(y);
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server responds: " +in.readInt());
client.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

TCPServer.java
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class TCPServer extends Thread
{
private ServerSocket serverSocket;
public TCPServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(100000);
}
public void run()
{
while(true)
{
try
{
int serverPort = 7889;
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to " +
server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
Integer x=in.readInt();
System.out.println("First Number from Client" + " -- " + x);
Integer y=in.readInt();
System.out.println("Second Number from Client" + " -- " +y);

Integer sum = ( x + y );

DataOutputStream out = new


DataOutputStream(server.getOutputStream());
// System.out.println(sum);
out.writeInt(sum);
server.close();
}catch(SocketTimeoutException s){
System.out.println("Socket timed out!");
break;
}catch(IOException e){
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
//Scanner userInput = new Scanner(System.in);
//System.out.println("Please specify a port number (1~65535): ");
int serverPort = 7889;
//port = userInput.next();
try
{
Thread t = new TCPServer(serverPort);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
OUTPUT:

TCPClient
info-13@info13-OptiPlex-3020:~$ cd Desktop
info-13@info13-OptiPlex-3020:~/Desktop$ javac TCPClient.java
info-13@info13-OptiPlex-3020:~/Desktop$ java TCPClient
Enter Server Address
localhost
Just Connected to localhost/127.0.1.1:7889
Enter a first number:
15
Enter a second number:
10
Server responds: 25

TCPServer
info-13@info13-OptiPlex-3020:~$ cd Desktop
info-13@info13-OptiPlex-3020:~/Desktop$ javac TCPServer.java
info-13@info13-OptiPlex-3020:~/Desktop$ java TCPServer
Waiting for client on port 7889...
Just connected to /127.0.1.1:47632
First Number from client -- 15
Second Number from client -- 10
Waiting for client on port 7889...

You might also like