TCP socket programming

TASK :
Write a client/server program whereby the server accepts commands from the client and does the computation as requested.
Specifically, the client will send to the server two integers along with one of the following operations: +, -, *, and /.
The server will response with the result and the client will print the result on client’s screen. In the meantime, the server will also print the same result on its screen.
You can use either TCP or UDP to exchange the messages. Of course your client program should prompt the user for entering the two operands, an operator, and the IP address of the server.

Available programming languages and operating systems: C, C++ or Java language over Windows OS or Linux/UNIX OS.

RESULT

TCPClient.java

import java.io.*;
import java.net.*;

//TCPClient.java
class TCPClient {
public static void main (String argv[]) throws Exception
{
String input1;
String input2;
String operator;
String result;
String server;

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

System .out.println(“Enter IP Server : \n”);
server = inFromUser.readLine();

Socket clientSocket = new Socket(server,6769);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

System.out.println(“Enter Input 1 : \n”);
input1 = inFromUser.readLine();
outToServer.writeBytes(input1 + ‘\n’);
System.out.println(“Enter Input 2 : \n”);
input2= inFromUser.readLine();
outToServer.writeBytes(input2 + ‘\n’);
//Choose Operator
System.out.println(“Choose Operator : \n”);
System.out.println(“1. Addition”);
System.out.println(“2. Substraction”);
System.out.println(“3. Multiplication”);
System.out.println(“4. Division \n”);

operator = inFromUser.readLine();
outToServer.writeBytes(operator + ‘\n’);

result = inFromServer.readLine();
System.out.println(“FROM SERVER : ” + result);
clientSocket.close();
}
}

TCPServer.java

import java.io.*;
import java.net.*;
import javax.xml.bind.ParseConversionEvent;

//TCPServer.java
class TCPServer {
public static void main (String argv[]) throws Exception
{

String clientInput1;
String clientInput2;
String clientOperator;
String serverResult;
ServerSocket welcomeSocket = new ServerSocket(6769);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader (new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientInput1 = inFromClient.readLine();
int a = Integer.parseInt(clientInput1);

clientInput2 = inFromClient.readLine();
int b = Integer.parseInt(clientInput2);

clientOperator = inFromClient.readLine();
int choice = Integer.parseInt(clientOperator);

switch( choice ) //switch to the value of choice
{
case 1: //if choice is 1 then:
serverResult = a +” + “+ b + ” = ” + (a+b) + ‘\n’;
outToClient.writeBytes(serverResult);
System.out.println( “Result in Server : ” + a +” + “+ b + ” = ” + (a+b) );
break;

case 2: //if choice is 2
serverResult = a +” – “+ b + ” = ” + (a-b) + ‘\n’;
outToClient.writeBytes(serverResult);
System.out.println( “Result in Server : ” + a + ” – ” + b +” = ” + (a-b) );
break;

case 3: //if choice is 3
serverResult = a +” * “+ b + ” = ” + (a*b) + ‘\n’;
outToClient.writeBytes(serverResult);
System.out.println( “Result in Server : ” + a + ” * ” + b +” = ” + (a*b) );
break;

case 4: //if choice is 4
serverResult = a +” / “+ b + ” = ” + (float)(a/b) + ‘\n’;
outToClient.writeBytes(serverResult);
System.out.println( “Result in Server : ” + a + ” / ” + b +” = ” + (float)(a/b) );
break;

default: //if the user enters any other value
System.out.println( “Enter a valid choice…” );

}
}
}
}

run server first, then run client.

Tutorial Java Socket Programming : http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

Tutorial Netbeans : http://www.netbeans.org/ 

Comments are closed.

%d bloggers like this: