0% found this document useful (0 votes)
2 views4 pages

CN 3

The document outlines the design and implementation of a Java client-server application using TCP socket programming for two-way communication. It details the algorithm for message exchange, including character manipulation, and provides sample code for both the server and client. The application demonstrates basic networking concepts and serves as a foundation for more complex network-based systems.

Uploaded by

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

CN 3

The document outlines the design and implementation of a Java client-server application using TCP socket programming for two-way communication. It details the algorithm for message exchange, including character manipulation, and provides sample code for both the server and client. The application demonstrates basic networking concepts and serves as a foundation for more complex network-based systems.

Uploaded by

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

Problem Definition

Design and implement a client-server application in Java using TCP socket


programming that enables two-way communication between a client and
a server, while performing simple character-based manipulations on the
exchanged messages.

Algorithm

Server Client

Close input stream​


Start server on port 1234​ Close socket​
Wait for client connection Close server socket

Set up:​ Connect to server at localhost on


- Input from client port 1234
(DataInputStream)​
- Output to client Set up:​
(DataOutputStream)​ - Input from server
- Input from server user (DataInputStream)​
(BufferedReader) - Output to server
(DataOutputStream)​
Initialize:​ - Input from client user
- str = ""​ (BufferedReader)
- str2 = ""
Initialize:​
WHILE str ≠ "Stop"​ - str = ""​
Read str from client - str2 = ""

IF str = "Stop"​ WHILE str ≠ "Stop"​


Set str2 = str​ Read str from user input​
ELSE​ Send str to server​
Extract first character from Flush output stream
str​
Increment character by 1​ Read str2 from server
Display incremented
character​ IF str2 = "Stop"​
Read str2 from server user Exit loop
(console)
Extract first character from str2​
Send str2 to client​ Increment character by 1​
Flush output stream Display incremented character

END WHILE END WHILE


Implementation/Code

Server Client

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


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

class Server { class Client {


public static void main(String public static void main(String
arg[]) throws Exception { arg[]) throws Exception {
ServerSocket ss = new Socket s = new
ServerSocket(1234); Socket("localhost", 1234);
Socket s = ss.accept(); DataInputStream din = new
DataInputStream din = new DataInputStream(s.getInputStream());
DataInputStream(s.getInputStream()); DataOutputStream dout = new
DataOutputStream dout = new DataOutputStream(s.getOutputStream())
DataOutputStream(s.getOutputStream()) ;
; BufferedReader br = new
BufferedReader br = new BufferedReader(new
BufferedReader(new InputStreamReader(System.in));
InputStreamReader(System.in)); String str = "", str2 = "";
String str = "", str2 = ""; while (!str.equals("Stop")) {
while (!str.equals("Stop")) { str = br.readLine();
str = din.readUTF(); dout.writeUTF(str);
if (str.equals("Stop")) { dout.flush();
str2 = str; str2 = din.readUTF();
} else { if (str2.equals("Stop"))
char ch = break;
str.charAt(0); char ch = str2.charAt(0);
ch += 1; ch += 1;
System.out.println(ch);
System.out.println(ch); }
str2 = br.readLine(); dout.flush();
} dout.close();
dout.writeUTF(str2); s.close();
dout.flush(); }
} }
din.close();
s.close();
ss.close();
}
}
Output

Server Client

C:\Users\student\Desktop\sayak>java C:\Users\student\Desktop\sayak>java
MyServer1 MyClient1
client says: Hello World Hello World

Conclusion
In this program, we developed a simple client-side socket application using Java. The
client successfully connected to the server over a TCP socket, took user input from the
console, reversed the string locally, and sent it to the server. It then received a response
from the server and displayed it to the user. This program demonstrated the use of key
Java networking and I/O classes such as Socket, DataInputStream,
DataOutputStream, and BufferedReader. Overall, the application showcases the
basics of client-server communication and forms a solid foundation for building more
interactive and advanced network-based systems.

You might also like