can anyone show me how to Use the java.net.ServerSocket class to open a port and listen for new connections ( Use the java.net.ServerSocket.accept() method to accept a new client connection. You can read from and write to the data streams in java.net.ServerSocket.getOutputStream() and java.net.ServerSocket.getInputStream() just as if they were files or standard input/outpu
public class Server {
private static final int port = 1313;
public static void main(String[] args) throws
IOException, AWTException {
ServerSocket server = null;
try {
//opeing the
port and listening connections
server = new
ServerSocket(port);
System.out.println("Server socket ready on port: " + port);
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.out.println(e);
System.exit(-1);
}
Socket socket =
null;
try {
//ready to accept the connections
socket = server.accept();
DataOutputStream dout = new
DataOutputStream(socket.getOutputStream());
//sending hello to the connected clients
dout.writeUTF("Hello..");
dout.flush();
} catch
(Exception e) {
server.close();
e.printStackTrace();
}
}
}
Note : If you like my answer please rate and help me it is very Imp for me
Get Answers For Free
Most questions answered within 1 hours.