How to Use a Socket? | Network Communication Basics

A socket is an endpoint for communication between two machines, and using one in code means creating it, connecting or binding it, then sending and receiving data.

Whether you’re wiring up a Python script or a Java application, the socket concept is the same: it’s the software endpoint where network communication happens. Most programmers encounter sockets through Python’s socket module or Java’s java.net.Socket class. Both give you access to the same underlying BSD socket interface that Unix systems have used for decades. The steps are straightforward once you understand the flow, and the differences between languages are smaller than you’d expect.

What Exactly Is a Socket?

A socket is a software endpoint for network communication between two machines. Think of it as a doorway: one program opens a socket, another connects to it, and data flows through. The concept comes from the BSD socket interface, standardized by the Open Group as int socket(int domain, int type, int protocol); — a call that creates an unbound socket or file descriptor.

Two transport types matter most:

  • TCP (stream sockets) — connection-oriented, reliable, ordered data delivery. Used for web traffic, email, file transfers.
  • UDP (datagram sockets) — connectionless, faster, but with no delivery guarantees. Used for streaming, gaming, DNS lookups.

Addresses follow a simple format: an IP address plus a port number, written as a tuple like ("192.168.1.10", 8080).

Using Sockets in Python

Python’s socket module provides the BSD socket interface on modern Unix, Windows, and macOS. It’s a low-level API — not a high-level HTTP client — so you handle the connection details yourself. The core flow is: create, connect or bind, then send and receive.

For a client that connects to a server, the pattern is short. Here’s a minimal example that connects and sends data:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("example.com", 80))
s.send(b"GET / HTTP/1.0\r\n\r\n")
data = s.recv(1024)
s.close()

Python’s Socket Programming HOWTO documents the same sequence and also includes socket.setblocking(False) for non-blocking mode — useful when you don’t want your program to pause waiting for data. For a server, you’d call bind() to attach to an address, listen() to accept connections, then loop on accept().

Using Sockets in Java

Java’s java.net.Socket is a client socket class for connection-oriented stream communication. The usage flow mirrors Python’s: create the socket, connect to a server, read from its input stream, and close when done.

Socket socket = new Socket();
socket.connect(new InetSocketAddress("example.com", 80));
socket.getInputStream().read();
socket.close();

Key methods to know: bind(SocketAddress) binds locally, connect(SocketAddress) connects to a server, getInputStream() reads incoming data, and close() ends the connection. Oracle’s Java 8 docs mark the constructor Socket(InetAddress host, int port, boolean stream) as deprecated — for UDP transport, use DatagramSocket instead. Java’s Socket class is strictly for TCP-style stream sockets, not datagrams.

Common Mistakes and Caveats

Three mistakes trip up most beginners. First, using TCP sockets when you need UDP — Java’s Socket won’t do datagrams, and forcing it causes confusing failures. Second, forgetting that Python’s socket module is low-level: it won’t parse HTTP responses or manage connections for you. Third, assuming socket behavior is identical across platforms — the BSD-style interface is common on Unix-like systems and Windows for Python, but details like non-blocking behavior can differ.

MicroPython users should know that its socket module is a subset of CPython’s; some features may not exist in released versions. Address formats there follow the same (ipv4_address, port) tuple pattern.

If you’re building something that needs sockets, the right tools matter. We’ve rounded up solid options in our budget socket set recommendations — useful for hands-on network hardware work.

References & Sources

Please use a real email you check. If it's fake or mistyped, your message won't reach us and we can't reply — wrong addresses are rejected automatically.