Skip to content

grpanho/sockets_tcp_udp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Socket Programming Examples - TCP and UDP

A collection of simple client-server socket programming examples in Python demonstrating both TCP and UDP protocols.

Overview

This repository contains basic implementations of network communication using Python's socket library. It includes both TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) examples with client and server components.

Project Structure

sockets_tcp_udp/
├── tcp/
│   ├── tcp_server.py    # TCP server implementation
│   └── tcp_client.py    # TCP client implementation
└── udp/
    ├── udp_server.py    # UDP server implementation
    └── udp_client.py    # UDP client implementation

Features

  • TCP Socket Examples: Reliable, connection-oriented communication

    • Server accepts one client connection at a time
    • Client sends messages to server
    • Proper connection handling and cleanup
  • UDP Socket Examples: Fast, connectionless communication

    • Server receives messages from any client
    • Client sends messages without establishing connection
    • Lightweight message passing

Requirements

  • Python 3.x
  • No external dependencies (uses built-in socket library)

Usage

TCP Example

1. Start the TCP Server

python tcp/tcp_server.py

The server will listen on localhost:5000 and wait for client connections.

2. Connect with TCP Client

In a new terminal:

python tcp/tcp_client.py
  • Type messages and press Enter to send them to the server
  • Use CTRL+X to exit the client

UDP Example

1. Start the UDP Server

python udp/udp_server.py

The server will listen on localhost:5000 for UDP messages.

2. Send messages with UDP Client

In a new terminal:

python udp/udp_client.py
  • Type messages and press Enter to send them to the server
  • Use CTRL+X to exit the client

Configuration

Both implementations use the following default settings:

  • Host: 127.0.0.1 (localhost) for clients, empty string for servers (all interfaces)
  • Port: 5000

You can modify the HOST and PORT variables in each file to change these settings.

Key Differences: TCP vs UDP

Feature TCP UDP
Connection Connection-oriented Connectionless
Reliability Guaranteed delivery Best effort
Overhead Higher Lower
Use Case When data integrity is critical When speed is more important

Example Output

TCP Server Output:

Conectado por ('127.0.0.1', 54321)
('127.0.0.1', 54321) b'Hello TCP Server!'
Finalizando conexao do cliente ('127.0.0.1', 54321)

UDP Server Output:

('127.0.0.1', 54321) b'Hello UDP Server!'

Educational Purpose

These examples are designed for learning socket programming concepts:

  • Understanding the difference between TCP and UDP
  • Basic client-server architecture
  • Network programming in Python
  • Socket creation, binding, listening, and data transmission

License

This project is open source and available under the MIT License.

About

Simple client-server socket programming examples in Python for TCP and UDP.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages