Last active
September 22, 2020 02:42
-
-
Save DhavalDalal/0a7bb28cd9819b3b805a5bb5907a18bc to your computer and use it in GitHub Desktop.
Echo TCP-Server (Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
import java.util.stream.Stream; | |
public class Client implements AutoCloseable { | |
private final Socket client; | |
private final DataOutputStream os; | |
private final BufferedReader is; | |
public Client(String host, int port) throws UnknownHostException, IOException { | |
client = new Socket(host, port); | |
os = new DataOutputStream(client.getOutputStream()); | |
is = new BufferedReader(new InputStreamReader(client.getInputStream())); | |
} | |
public void sendReceive(String message) { | |
try { | |
System.out.println("Thread = " + Thread.currentThread()); | |
System.out.println("Sending to Server: " + message); | |
os.writeBytes(message + "\n"); | |
os.flush(); | |
// keep on reading from/to the socket till we receive the "Ok" from Server, | |
// once we received that we break. | |
String responseLine = is.readLine(); | |
if (responseLine != null) { | |
System.out.println("Server Sent: " + responseLine); | |
} else { | |
System.out.println("Server Sent: No Response"); | |
} | |
} catch (UnknownHostException e) { | |
System.err.println("Don't know about host: hostname"); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
} | |
public void close() throws IOException { | |
sendReceive("QUIT"); | |
is.close(); | |
os.close(); | |
} | |
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException { | |
int port = 8080; | |
String host = "localhost"; | |
int totalClients = 4; | |
Stream.iterate(1, x -> x + 1).limit(totalClients).forEach(id -> { | |
new Thread(() -> { | |
try (Client client = new Client(host, port)) { | |
client.sendReceive("HELO" + id); | |
Thread.sleep(2000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
}).start(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintStream; | |
import java.net.InetAddress; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
public class Server implements AutoCloseable { | |
private final ServerSocket server; | |
public Server(String host, int port, int backlogConnectionQueueLength) throws UnknownHostException, IOException { | |
server = new ServerSocket(port, backlogConnectionQueueLength, InetAddress.getByName(host)); | |
System.out.println(Thread.currentThread() + " Created Server"); | |
} | |
public void start() { | |
System.out.println(Thread.currentThread() + " Server Ready: " + server); | |
while (true) { | |
acceptAndHandleClient(server); | |
} | |
} | |
private void acceptAndHandleClient(ServerSocket server) { | |
System.out.println(Thread.currentThread() + " Waiting for Incoming connections..."); | |
try (Socket clientSocket = server.accept()) { | |
handleNewClient(clientSocket); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void handleNewClient(Socket clientSocket) throws IOException { | |
System.out.println(Thread.currentThread() + " Received Connection from " + clientSocket); | |
BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
PrintStream os = new PrintStream(clientSocket.getOutputStream()); | |
// echo that data back to the client, except for QUIT. | |
String line = null; | |
while ((line = is.readLine()) != null) { | |
System.out.println(Thread.currentThread() + " Server Got => " + line); | |
if (line.equalsIgnoreCase("QUIT")) | |
break; | |
else { | |
System.out.println(Thread.currentThread() + " Server echoing line back => " + line); | |
os.println(line); | |
os.flush(); | |
} | |
} | |
System.out.println(Thread.currentThread() + " Server Closing Connection by Sending => Ok"); | |
os.println("Ok"); | |
os.flush(); | |
is.close(); | |
os.close(); | |
} | |
public void close() throws IOException { | |
server.close(); | |
} | |
public static void main(String[] args) { | |
try (Server server = new Server("localhost", 8080, 50)) { | |
server.start(); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment