Java || Snippet – How To Send Text Over A Network Using Socket

Print Friendly, PDF & Email

The following is sample code which demonstrates the use of the “socket” function call for interprocess communication over a network.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

The following is sample output.


Server started!

Waiting for someone to connect..


RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed Jan 08 18:07:15 PST 2014" to the client


SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed Jan 08 18:07:15 PST 2014" from the server

Was this article helpful?
👍 YesNo

Leave a Reply