by
354 16
5
7
3,276
5
Top 1% !
Popular
Famous
Nice
Tagged
Specified
OpenSource
Popularity: 4029th place

Published on:

LanguageJava
LicenseMIT_X11

Server socket in Java

This is a simple server socket in java. The server will accept the client request and return the date string.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/33143/Server-socket-in-Java?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; /** * A TCP server that runs on port 9090. When a client connects, it * sends the client the current date and time, then closes the * connection with that client. Arguably just about the simplest * server you can write. */ public class DateServer { /** * Runs the server. */ public static void main(String[] args) throws IOException { ServerSocket listener = new ServerSocket(9090); try { while (true) { Socket socket = listener.accept(); try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println(new Date().toString()); } finally { socket.close(); } } } finally { listener.close(); } } }

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus