Skip to content

Instantly share code, notes, and snippets.

@greut
Created May 29, 2017 07:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greut/78df81b6b197189e36c0e7b0815099b6 to your computer and use it in GitHub Desktop.
Save greut/78df81b6b197189e36c0e7b0815099b6 to your computer and use it in GitHub Desktop.
TCP + UDP Java Echo Server.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
// TCP
new Thread(new Runnable() {
@Override
public void run() {
ExecutorService executor = null;
try (ServerSocket server = new ServerSocket(1234)) {
executor = Executors.newFixedThreadPool(5);
System.out.println("Listening on TCP port 1234, Say hi!");
while (true) {
final Socket socket = server.accept();
executor.execute(new Runnable() {
@Override
public void run() {
String inputLine = "";
System.err.println(
socket.toString() + " ~> connected");
try (PrintWriter out = new PrintWriter(
socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket
.getInputStream()))) {
while (!inputLine.equals("!quit")
&& (inputLine = in
.readLine()) != null) {
System.out.println(socket.toString()
+ ": " + inputLine);
// Echo server...
out.println(inputLine);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
System.err.println(socket.toString()
+ " ~> closing");
socket.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
});
}
} catch (IOException ioe) {
System.err.println("Cannot open the port on TCP");
ioe.printStackTrace();
} finally {
System.out.println("Closing TCP server");
if (executor != null) {
executor.shutdown();
}
}
}
}).start();
// UDP
new Thread(new Runnable() {
@Override
public void run() {
try (DatagramSocket socket = new DatagramSocket(1234)) {
byte[] buf = new byte[socket.getReceiveBufferSize()];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
System.out.println("Listening on UDP port 1234, Say hi!");
while (true) {
socket.receive(packet);
System.out.println(packet.getSocketAddress().toString()
+ ": " + new String(buf, "UTF-8"));
// Echo server
socket.send(packet);
}
} catch (IOException ioe) {
System.err.println("Cannot open the port on UDP");
ioe.printStackTrace();
} finally {
System.out.println("Closing UDP server");
}
}
}).start();
}
}
@et92
Copy link

et92 commented Apr 26, 2021

Can u share client socket or full project please?
my email is: erikrozay@gmail.com

@CodeWizardGenius
Copy link

Can u share client socket or full project please?
my email is: bkarapelit48@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment