Skip to content

Rahuldrabit/socketprogramming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Socket Programming in Python

This repository demonstrates basic TCP socket programming in Python through a simple client-server model. It contains two scripts:

  • TCPServer.py: A TCP server that listens for incoming client connections, processes messages, and sends responses.
  • TCPClient.py: A TCP client that connects to the server, sends user input, and displays the server's response.

Features

  • TCP Server: Listens on a configurable port, accepts incoming client connections, receives data, processes it (converts it to uppercase), and sends a response back to the client.
  • TCP Client: Connects to the TCP server, sends a message (entered by the user), receives the server's response, and prints it.

File Overview

TCPServer.py

This script implements a simple TCP server. Its key steps:

  1. Create a TCP Socket:
    Uses socket(AF_INET, SOCK_STREAM) for IPv4 and TCP.

  2. Bind to a Port:
    Binds the server to all interfaces ('') and port 12332.

  3. Listen for Connections:
    Listens for up to 5 simultaneous connection requests.

  4. Accept Connections:
    Waits in an infinite loop to accept new connections.

  5. Process Client Data:

    • Receives a sentence from the client.
    • Converts it to uppercase.
    • Sends the capitalized sentence back to the client.
    • Closes the client connection.
from socket import *

serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', 12332))
serverSocket.listen(5)
print("server is ready to receive")
while True:
    connectionSocket, addr = serverSocket.accept()
    sentence = connectionSocket.recv(1024).decode()
    captalizedSentence = sentence.upper()
    connectionSocket.send(captalizedSentence.encode())
    connectionSocket.close()

TCPClient.py

This script acts as a client that connects to the above server. Its key steps:

  1. Create a TCP Socket:
    Uses socket(AF_INET, SOCK_STREAM).

  2. Connect to the Server:
    Connects to the server using the specified IP address and port (192.168.0.103, 12332).
    Note: Change the IP address to match your server's address.

  3. Send Data:
    Prompts the user for input, encodes it, and sends it to the server.

  4. Receive and Display the Response:
    Receives the server's reply and prints it.

  5. Close the Connection:
    Closes the socket.

from socket import *

clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(('192.168.0.103', 12332))
sentence = input('Input lowercase sentence: ')
clientSocket.send(sentence.encode())
ackSentence = clientSocket.recv(1024)
print("From server: ", ackSentence.decode())
clientSocket.close()

Usage

Requirements

  • Python 3.x

No external dependencies are required; only the Python standard library is used.


Setup & Running

1. Start the Server

On the server machine (or terminal window), run:

python TCPServer.py

You should see:

server is ready to receive

2. Start the Client

On the client machine (or a different terminal window on the same machine), edit the TCPClient.py file to set the correct server IP address:

clientSocket.connect(('SERVER_IP_ADDRESS', 12332))

Replace SERVER_IP_ADDRESS with the actual IP address of the server.

Then run:

python TCPClient.py

You will be prompted:

Input lowercase sentence:

Type a sentence (e.g., hello world) and press Enter.

The client will send this to the server, and print the response:

From server:  HELLO WORLD

Notes

  • Make sure the server and client are on the same network or the server's port is accessible from the client.
  • The server will process one connection at a time and then wait for the next client.
  • You can extend this example to handle multiple clients concurrently using Python threading or asynchronous sockets.

License

This project is for educational purposes and does not include a specific license. Feel free to use, modify, and share.


Author

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages