This project demonstrates inter-process communication using network sockets in .NET. It consists of a socket server and client application that communicate over TCP/IP, implementing core socket programming concepts.
├── SocketServer/
│ └── Program.cs // Server implementation
└── SocketClient/
└── Program.cs // Client implementation
- TCP/IP socket communication
- Client-server architecture
- Bi-directional message exchange
- Multi-client support with threaded connections
- Connection management and cleanup
- .NET Core/6.0+
- System.Net.Sockets namespace
- TCP/IP networking
Feature | .NET Implementation | C Implementation |
---|---|---|
Socket Creation | new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) |
socket(AF_INET, SOCK_STREAM, 0) |
Binding | socket.Bind(ipEndPoint) |
bind(sockfd, (struct sockaddr*)&address, sizeof(address)) |
Listening | socket.Listen(backlog) |
listen(sockfd, backlog) |
Connection | socket.Connect(endPoint) |
connect(sockfd, (struct sockaddr*)&address, sizeof(address)) |
Accepting | Socket client = serverSocket.Accept() |
clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addr_len) |
Sending | socket.Send(buffer) |
send(sockfd, buffer, length, flags) |
Receiving | socket.Receive(buffer) |
recv(sockfd, buffer, length, flags) |
Threading | Task or Thread |
pthread_create() |
Error Handling | Exceptions | Error codes and errno |
Address Structure | IPEndPoint |
struct sockaddr_in |
Memory Management | Automatic | Manual |
- Socket lifecycle (create, bind, listen, accept, receive/send, close)
- Difference between Unix domain sockets and internetwork sockets
- Port usage and management (server fixed ports vs client ephemeral ports)
- TCP connection identification via 4-tuple (source IP, source port, destination IP, destination port)
- Resource limitations in socket connections
- Thread management for multiple client connections
cd SocketServer
dotnet run
cd SocketClient
dotnet run
Sockets are communication endpoints that enable processes to exchange data, whether on the same machine or across a network. They provide a standardized API for network and inter-process communication in operating systems.
Sockets follow a client-server model where:
- A server creates a socket, binds it to an address, and listens for connections
- A client creates a socket and connects to the server's address
- Once connected, both sides can send and receive data bidirectionally
- When communication is complete, either side can close the connection
Feature | Unix Domain Sockets | TCP/IP Sockets |
---|---|---|
Communication Scope | Same machine only | Local or across networks |
Addressing Mechanism | File system paths | IP address + port number |
Address Format | /path/to/socket |
192.168.1.100:8080 |
Performance | Higher (avoids network stack) | Lower (requires protocol overhead) |
Address Family | AF_UNIX / AddressFamily.Unix |
AF_INET / AddressFamily.InterNetwork |
Connection Identification | File descriptor | 4-tuple (source IP, source port, destination IP, destination port) |
Security | File system permissions | Network-level security |
Use Cases | Fast IPC between local processes | Communication between processes on different machines |
Port Usage | No concept of ports | Requires port allocation |
Durability | Socket file remains until explicitly removed | Terminates when process ends |
- Use file paths as addresses
- Don't require network protocol overhead
- Provide higher performance for local communication
- Limited to processes on the same machine
- File permissions control access
- Use IP addresses and ports
- Work across network boundaries
- Client connections use ephemeral ports assigned by OS
- Support thousands of concurrent connections via unique 4-tuple identifiers
- Require more system resources and overhead
- Unix Sockets: Database engines (PostgreSQL, MySQL), X Window System, Container communications
- TCP Sockets: Web servers/browsers, email clients/servers, remote login, file transfers
Both socket types follow similar programming patterns but differ in how they're addressed and the scope of their communication capabilities.
- Implement asynchronous socket operations
- Add protocol definition for structured messages
- Improve error handling and recovery
- Implement connection pooling
- Add configuration options for IP/port
- Create performance benchmarks
This project was created as a learning exercise to understand socket communication in .NET.