A basic TCP Client and Server implementation in Python.
- Simple TCP Communication: Establishes a fundamental client-server connection for data exchange.
- Easy to Understand: Clear and concise code, ideal for beginners learning network programming.
- Customizable Port: Allows users to configure the port for communication.
- Python 3.x: Ensure Python 3 or a later version is installed on your system.
- No external libraries required: This project only uses the built-in
socketmodule.
-
Clone the repository:
git clone https://github.com/Paddywelch117/TCP-Client-Server.git cd TCP-Client-Server -
No installation needed: The project consists of Python scripts that can be run directly.
-
Run the server:
python TCP\ Server.py -
Run the client:
python TCP\ Client.PyNote: Ensure the server is running before starting the client. The client will attempt to connect to the server at the specified host and port.
TCP Server.py:
import socket
#Creating the socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 4444
serversocket.bind((host, port))
#Starting TCP listener
serversocket.listen(3)
while True:
#Starting the connection
clientsocket,address = serversocket.accept()
print("received connection from: %s " % str(address))
#Message sent to client after successful connection
message = 'hello! Thank you for connecting to the server' + "\r\n"
clientsocket.send(message.encode('ascii'))
clientsocket.close()TCP Client.py:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 4444
s.connect((host, port))
tm = s.recv(1024)
s.close()
print("The time got from the server is %s" % tm.decode('ascii'))-
Port: The default port is
4444. You can modify theportvariable in bothTCP Server.pyandTCP Client.Pyto use a different port. Ensure that both the client and server use the same port.# Example: Changing the port to 12345 port = 12345
-
Host: The server uses
socket.gethostname()to determine the host. This is typically your local machine's hostname. You can change this to a specific IP address if needed, but ensure the client can reach that address.
Contributions are welcome! If you'd like to contribute to this project, please follow these guidelines:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them with clear, descriptive messages.
- Submit a pull request to the main branch.
This project has no specified license. All rights are reserved by the owner.
N/A