Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@seyedsahil
Created February 4, 2021 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seyedsahil/c18ecb901d65000a9844821aba3041f8 to your computer and use it in GitHub Desktop.
Save seyedsahil/c18ecb901d65000a9844821aba3041f8 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Date;
/**
* @author Seyed Sahil
*/
public class App {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("Server session started at " + new Date());
int connections = 0;
while (connections != 2) {
System.out.println("Server is listening on port 8080");
Socket clientSocket = serverSocket.accept();
connections++;
System.out.println("Server connected with Client-" + connections + " at " + new Date());
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println("Hello Client-" + connections + ", What is your name ?");
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.println("Hey " + in.readLine() + ", Nice to meet you!");
in.close();
out.close();
clientSocket.close();
}
serverSocket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment