Client Server System for Socket Programming | Desklib

Verified

Added on  2022/08/10

|18
|1451
|253
AI Summary
This article explains about Client Server System for Socket Programming. It includes program overview, program description, explanation with code, output, and figures.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running head: CLIENT SERVER SYSTEM
Client Server System
Name of the Author
Name of the University
Author Note

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
1CLIENT SERVER SYSTEM
Table of Contents
Program Overview:..........................................................................................................................2
Program Description:.......................................................................................................................2
Explanation with Code :..................................................................................................................2
Output:...........................................................................................................................................14
Figure 1......................................................................................................................................15
Figure 2......................................................................................................................................15
Figure 3......................................................................................................................................16
Figure 4......................................................................................................................................17
Document Page
2CLIENT SERVER SYSTEM
Program Overview:
This is a Client Server application. First it will get response from the server after that
client will reply the message based on the server response. During conversation there have
limited user so they can talk each other until the conversation is end. Finally it will encrypt or
decrypt data along with time and date constraints. This will work in such a cycle order.
Program Description:
There is two package. One package name is com.project for the getting client and server
information. First user have to run the KnockKnockServer.java this java file is for calling the
server side based on the localhost. Then user have to run the KnockKnockClient.java once the
server side is invoking the client side based on the knock the message. Based on the knock
knock message then user can interchange conversion between each other. There are different
user like Bob and Alice etc. Then user have to press. For date and time the package name is
com.assignment for the getting client and server information. Client can entered the date value
and it shows date and when tyoe the time current time is shown in the console.There is another
class RSA example which will use to encrypt and decrypt default message.
Explanation with Code :
Client server application program have two package com.project and com.assignment. In
the com.project package first run the knockknockserver.java file it is use for call the
server.
package com.project;
import java.net.*;
Document Page
3CLIENT SERVER SYSTEM
import java.io.*;
public class KnockKnockServer {
public static void main(String[] args) throws IOException {
/* if (args.length != 1) {
System.err.println("Usage: java KnockKnockServer <port number>");
System.exit(1);
}*/
int portNumber = 4444;
try (
ServerSocket serverSocket = new ServerSocket(4444);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
4CLIENT SERVER SYSTEM
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine, outputLine;
// Initiate conversation with client
Message kkp = new Message();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
Document Page
5CLIENT SERVER SYSTEM
out.close();
in.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
After that run knockknockclient.java file this is then response the server calling. First this
is show a encrypt decrypt message form RSAexamples.java file and so the message.
After that some some ques and reply is saved in the com.project message.java file which
will be called and user can type that manually and get response.
package com.project;
import java.io.BufferedReader;
Document Page
6CLIENT SERVER SYSTEM
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import com.assignment.RsaExample;
public class KnockKnockClient {
public static void main(String[] args) throws IOException {
/* if (args.length != 2) {
System.err.println(
"Usage: java EchoClient <host name> <port number>");
System.exit(1);
}*/
RsaExample rr=new RsaExample();

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7CLIENT SERVER SYSTEM
String msg="hello";
byte[] encryptedData=rr.encryptData(msg);
rr.decryptData(encryptedData);
String hostName = "localhost";
int portNumber = 4444;
/* String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);*/
try (
Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));
) {
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
Document Page
8CLIENT SERVER SYSTEM
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
kkSocket.close();
} catch (UnknownHostException e) {
Document Page
9CLIENT SERVER SYSTEM
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
Then com.assignment package server.java file will called and then client.java will be
response form the clienthandler.java file where date and time format is saved. So the date
and time question will be shown in the console. User can manually type date and time to
show the current date and time.
package com.assignment;
import java.io.*;
import java.net.*;

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
10CLIENT SERVER SYSTEM
import java.text.*;
import java.util.Date;
class ClientHandler extends Thread
{
DateFormat fordate = new SimpleDateFormat("yyyy/MM/dd");
DateFormat fortime = new SimpleDateFormat("hh:mm:ss");
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
// Constructor
public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)
{
this.s = s;
this.dis = dis;
this.dos = dos;
Document Page
11CLIENT SERVER SYSTEM
}
@Override
public void run()
{
String received;
String toreturn;
while (true)
{
try {
// Ask user what he wants
dos.writeUTF("What do you want?[Date | Time]..\n"+
"Type Exit to terminate connection.");
// receive the answer from client
received = dis.readUTF();
Document Page
12CLIENT SERVER SYSTEM
if(received.equals("Exit"))
{
System.out.println("Client " + this.s + " sends exit...");
System.out.println("Closing this connection.");
this.s.close();
System.out.println("Connection closed");
break;
}
// creating Date object
Date date = new Date();
// write on output stream based on the
// answer from the client
switch (received) {
case "Date" :
toreturn = fordate.format(date);

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
13CLIENT SERVER SYSTEM
dos.writeUTF(toreturn);
break;
case "Time" :
toreturn = fortime.format(date);
dos.writeUTF(toreturn);
break;
default:
dos.writeUTF("Invalid input");
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
try
Document Page
14CLIENT SERVER SYSTEM
{
// closing resources
this.dis.close();
this.dos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
Document Page
15CLIENT SERVER SYSTEM
Output:
Figure 1

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
16CLIENT SERVER SYSTEM
Figure 2
Figure 1 and Figure 2 will be shown the connection between server and client using
socket programming. First knockknockserver is called then knockknockclient will be called then
called RSA example file to encrypt and Decrypt the message. Then the conversation between
server and client will start.
Figure 3
First run the server.java file after that client.java file run and client.java file will be call
ClientHandler file to run this code. User can choose date or time and type manually then current
date and time will be shown.
Document Page
17CLIENT SERVER SYSTEM
Figure 4
After user manually type Exit then this message will be shown and connection between
server and client is closed as shown in the screenshot.
1 out of 18
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]