Socket Programming-> It helps us to connect a client to a server. Client is message sender and receiver and server is just a listener that works on data sent by client.
What is a Thread?
A thread is a light-weight process that does not require much memory overhead, they are cheaper than processes.
What is Multi-threading Socket Programming?
Multithreading is a process of executing multiple threads simultaneously in a single process.
Multi-threading Modules :
A _thread module & threading module is used for multi-threading in python, these modules help in synchronization and provide a lock to a thread in use.
from _thread import * import threading
A lock object is created by->
print_lock = threading.Lock()
A lock has two states, “locked” or “unlocked”. It has two basic methods acquire() and release(). When the state is unlocked print_lock.acquire() is used to change state to locked and print_lock.release() is used to change state to unlock.
The function thread.start_new_thread() is used to start a new thread and return its identifier. The first argument is the function to call and its second argument is a tuple containing the positional list of arguments.
Let’s study client-server multithreading socket programming by code-
Note:-The code works with python3.
Multi-threaded Server Code
Console Window: socket binded to port 12345 socket is listening Connected to : 127.0.0.1 : 11600 Bye
Console Window: Received from the server : skeegrofskeeg syas ayruahs Do you want to continue(y/n) :y Received from the server : skeegrofskeeg syas ayruahs Do you want to continue(y/n) :n Process finished with exit code 0
This article is contributed by SHAURYA UPPAL. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Socket Programming in Python
- Socket Programming in Java
- Multithreading in Python | Set 1
- Multithreading in Python | Set 2 (Synchronization)
- Difference Between Multithreading vs Multiprocessing in Python
- Explicitly assigning port number to client in Socket
- Difference between Secure Socket Layer (SSL) and Transport Layer Security (TLS)
- Secure Socket Layer (SSL)
- Difference between Secure Socket Layer (SSL) and Secure Electronic Transaction (SET)
- Socket in Computer Network
- A basic Python Programming Challenge
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Command Line Interface Programming in Python
- Turtle Programming in Python
- CGI Programming in Python
- Python in Competitive Programming
- Python - Fastest Growing Programming Language
- Why is Python the Best-Suited Programming Language for Machine Learning?
- Difference Between Go and Python Programming Language