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 [Link].*; import [Link].*;
import [Link].*; import [Link].*;
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 = [Link](); DataInputStream din = new
DataInputStream din = new DataInputStream([Link]());
DataInputStream([Link]()); DataOutputStream dout = new
DataOutputStream dout = new DataOutputStream([Link]())
DataOutputStream([Link]()) ;
; BufferedReader br = new
BufferedReader br = new BufferedReader(new
BufferedReader(new InputStreamReader([Link]));
InputStreamReader([Link])); String str = "", str2 = "";
String str = "", str2 = ""; while () {
while () { str = [Link]();
str = [Link](); [Link](str);
if ([Link]("Stop")) { [Link]();
str2 = str; str2 = [Link]();
} else { if ([Link]("Stop"))
char ch = break;
[Link](0); char ch = [Link](0);
ch += 1; ch += 1;
[Link](ch);
[Link](ch); }
str2 = [Link](); [Link]();
} [Link]();
[Link](str2); [Link]();
[Link](); }
} }
[Link]();
[Link]();
[Link]();
}
}
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.