Open links in new tab
  1. Socket programming in Java enables communication between programs over a network. Below is a simple implementation of a Client-Server model using Java sockets.

    Server Implementation

    The server listens for client connections on a specific port and processes incoming messages.

    import java.io.*;
    import java.net.*;

    public class Server {
    public static void main(String[] args) {
    try (ServerSocket serverSocket = new ServerSocket(5000)) {
    System.out.println("Server started. Waiting for a client...");

    // Accept client connection
    Socket socket = serverSocket.accept();
    System.out.println("Client connected.");

    // Input and output streams for communication
    DataInputStream input = new DataInputStream(socket.getInputStream());
    DataOutputStream output = new DataOutputStream(socket.getOutputStream());

    String message;
    while (!(message = input.readUTF()).equalsIgnoreCase("exit")) {
    System.out.println("Client: " + message);
    output.writeUTF("Server received: " + message);
    }

    System.out.println("Closing connection...");
    socket.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    Copied!
  1. Java Socket Programming: Server-Client Tutorial

    Aug 3, 2022 · Build client-server applications with Java Socket programming. Complete guide covering TCP sockets, ServerSocket, multithreading, and practical …

  2. A Guide to Java Sockets - Baeldung

    • In this article, we focused on an introduction to sockets programming over TCP/IP,and wrote a simple Client/Server application in Java. The full source code for this article can be found in the GitHubproject.
    See more on baeldung.com
    • Published: Aug 26, 2016
    • ServerSocket (Java Platform SE 8 ) - Oracle Help Center

      This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester. …

    • Java Socket Programming - Tpoint Tech

      Mar 19, 2026 · Java socket programming is used to establish communication between applications running on different systems or different Java Runtime …

    • Java - Socket Class with Examples - Online Tutorials Library

      The Java Socket class represents the socket that both the client and the server use to communicate with each other. The client obtains a Socket object by instantiating one, whereas the server obtains a …

    • People also ask
      Loading
      Unable to load answer
    • Java Socket Programming – Server and Client Example

      In this comprehensive guide, you’ll learn how to implement both server and client socket programs from scratch, explore real-world use cases, discover common …

    • Java Socket Programming Examples

      A trivial date server and client, illustrating simple one-way communication. The server sends data to the client only. A capitalize server and client, illustrating two …

    • Java Socket Client Examples (TCP/IP) - CodeJava.net

      Jul 18, 2019 · In this Java network programming tutorial, we’ll guide you how to write a client program that talks to a server using TCP/IP protocol. In the next few minutes, you will see that Java makes it …

    • How to Create a simple TCP Client-Server Connection in …

      Jul 23, 2025 · In Java, we can create TCP client-server connections using the Socket and ServerSocket classes from the java.net package. In this article, we will learn …