0% found this document useful (0 votes)
3 views

Server 2

The document contains Java code for a simple server application that listens for client connections on port 5000. Upon a client connection, it reads a message from the client and prints it to the console, then sends a response back to the client. The code includes error handling for potential input/output exceptions during the connection and communication process.

Uploaded by

Lina
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Server 2

The document contains Java code for a simple server application that listens for client connections on port 5000. Upon a client connection, it reads a message from the client and prints it to the console, then sends a response back to the client. The code includes error handling for potential input/output exceptions during the connection and communication process.

Uploaded by

Lina
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class server {

public static void main (String[] args) throws IOException{


try (ServerSocket ss = new ServerSocket(5000)) {
System.out.println("Server is running...");
try( Socket s = ss.accept()){
System.out.println("Client connected");

try (InputStreamReader in = new


InputStreamReader(s.getInputStream());
BufferedReader bf = new BufferedReader(in)) {
String str = bf.readLine();
System.out.println("Client: " + str);
} catch (IOException e) {
System.out.println("Error reading from client: " +
e.getMessage());
}
} catch (IOException e) {
System.out.println("Error starting server: " + e.getMessage());

}
try (Socket s = ss.accept();
PrintWriter pr = new
PrintWriter(s.getOutputStream(),true)){
pr.println("yes");
pr.flush();

}
}
}
}

You might also like