Java > Java Networking > Socket Programming > Socket Input and Output Streams
Simple Socket Communication with Input and Output Streams
This example demonstrates basic socket communication using Java's Socket
class. It involves a server that listens on a specific port, and a client that connects to the server. The example showcases how to use input and output streams to send and receive data between the client and server.
Server Code (ServerSocket)
This server code creates a ServerSocket
to listen for incoming client connections on port 12345. When a client connects, it accepts the connection and obtains an input stream (InputStream
) to read data from the client and an output stream (OutputStream
) to send data back. A BufferedReader
is used to efficiently read lines from the input stream, and a PrintWriter
is used to easily send formatted output to the client.
import java.io.*;
import java.net.*;
public class SimpleServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(12345)) {
System.out.println("Server is listening on port 12345");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());
// Input Stream from Client
InputStream input = clientSocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
// Output Stream to Client
OutputStream output = clientSocket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
String message = reader.readLine();
System.out.println("Server received: " + message);
writer.println("Server received your message: " + message);
clientSocket.close();
} catch (IOException e) {
System.err.println("Server exception: " + e.getMessage());
e.printStackTrace();
}
}
}
Client Code (Socket)
The client code creates a Socket
to connect to the server at localhost
on port 12345. Similar to the server, it obtains an output stream to send data to the server and an input stream to receive data back. The client sends a message to the server and then waits for a response. PrintWriter
and BufferedReader
are used for efficient writing and reading respectively.
import java.io.*;
import java.net.*;
public class SimpleClient {
public static void main(String[] args) {
String serverAddress = "localhost"; // Or the IP address of the server
int serverPort = 12345;
try (Socket socket = new Socket(serverAddress, serverPort)) {
// Output Stream to Server
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
// Input Stream from Server
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
writer.println("Hello from client!");
String response = reader.readLine();
System.out.println("Client received: " + response);
} catch (UnknownHostException e) {
System.err.println("Unknown host: " + serverAddress);
} catch (IOException e) {
System.err.println("Client exception: " + e.getMessage());
e.printStackTrace();
}
}
}
Concepts Behind the Snippet
This code demonstrates the client-server model of networking. The server listens for connections, and the client initiates a connection. Sockets are the endpoints of the communication. Input streams are used to read data from the socket, and output streams are used to write data to the socket. ServerSocket
is responsible for listening and accepting connections, while Socket
is responsible for establishing a connection and sending/receiving data.
Real-Life Use Case
Socket programming with input and output streams is fundamental to many network applications. Examples include:
Best Practices
IOException
and UnknownHostException
.
Interview Tip
Be prepared to discuss the differences between TCP and UDP sockets, how to handle multiple client connections concurrently, and security considerations when using sockets. Understanding the TCP/IP model is also crucial.
When to use them
Use socket programming when you need low-level control over network communication, or when building custom network protocols. It's also suitable when you need persistent connections between a client and server.
Memory footprint
The memory footprint of socket programming depends on the number of connections and the size of the data being transmitted. Each socket consumes memory, and buffering data in streams also requires memory. Careful resource management and proper stream handling can help minimize the memory footprint.
Alternatives
Alternatives to raw socket programming include:
Pros
Cons
FAQ
-
What is a socket?
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. -
What is the difference between TCP and UDP sockets?
TCP (Transmission Control Protocol) is a connection-oriented protocol that provides reliable, ordered, and error-checked delivery of data. UDP (User Datagram Protocol) is a connectionless protocol that provides faster but unreliable delivery of data. -
How do I handle multiple client connections in a server?
Use multithreading. Each client connection should be handled in a separate thread to prevent blocking the main server thread. You can also use an ExecutorService to manage threads.