Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package socketcomm;
- import java.net.*;
- import java.io.*;
- public class EchoServer2 extends Thread {
- protected Socket clientSocket;
- public static void main(String[] args) throws IOException {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(5000);
- System.out.println("Connection Socket Created");
- try {
- while (true) {
- System.out.println("Waiting for Connection");
- new EchoServer2(serverSocket.accept());
- }
- } catch (IOException e) {
- System.err.println("Accept failed.");
- System.exit(1);
- }
- } catch (IOException e) {
- System.err.println("Could not listen on port: 5000.");
- System.exit(1);
- } finally {
- try {
- serverSocket.close();
- } catch (IOException e) {
- System.err.println("Could not close port: 5000.");
- System.exit(1);
- }
- }
- }
- private EchoServer2(Socket clientSoc) {
- clientSocket = clientSoc;
- start();
- }
- public void run() {
- System.out.println("New Communication Thread Started");
- try {
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
- true);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(clientSocket.getInputStream()));
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- System.out.println("Server: " + inputLine);
- out.println(inputLine);
- if (inputLine.equals("Bye.")) {
- break;
- }
- }
- out.close();
- in.close();
- clientSocket.close();
- } catch (IOException e) {
- System.err.println("Problem with Communication Server");
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement