Adv Java Experiment 04
Adv Java Experiment 04
Experiment – 04
Advance Java Lab (5CS4-24)
Class – B.Tech III Year, V Sem.
Objective:
Write a java program for two way TCP communication for server and client. It should
look like a simple chat application.
Code:
GossipClient.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
} catch (IOException e) {
e.printStackTrace();
}
}
}
GossipServer.java
//Server program to handle multiple
// Clients with socket connections
import java.io.*;
import java.net.*;
import java.util.concurrent.CopyOnWriteArrayList;
// Constructor
public ClientHandler(Socket socket) {
this.clientSocket = socket;
try {
// Create input and output streams for communication
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
// Close the input and output streams and the client socket
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}