You should keep 2 threads: one for listening and the other for receiving. In your while
loop, you should remove the listener part, and keep the code in a different thread. This way you can receive and type on the console at the same time.
def recv():
while True:
data = s.recv(1024).decode()
if not data: sys.exit(0)
print data
Thread(target = recv).start()
You should keep 2 threads: one for listening and the other for receiving. In your while loop, you should remove the listener part, and keep the code in a different thread. This way you can receive and type on the console at the same time.,Basically I have been working on a simple chat room using socket and thread. In my client I can receive and send messages, my issue is that one comes before another in a loop, so if I am sending a message I will only receive data once I have sent a message. I want it to work like any other chat room, where I could receive a message when I am sending a message, any help will help greatly. This is my basic client:,Python – Referring to the null object in Python,Python – What’s the canonical way to check for type in Python
Basically I have been working on a simple chat room using socket and thread. In my client I can receive and send messages, my issue is that one comes before another in a loop, so if I am sending a message I will only receive data once I have sent a message. I want it to work like any other chat room, where I could receive a message when I am sending a message, any help will help greatly. This is my basic client:
import socket import sys # # # # # # # # # # # HOST = '25.0.18.52' PORT = 9999 # # # # # # # # # # # name = input("Enter your name: ") s = socket.socket() s.connect((HOST, PORT)) while 1: message = input("Message: ") s.send("{}: {}".format(name, message).encode('utf-8')) data = s.recv(1024) a = data.decode("utf-8") print(a)
You should keep 2 threads: one for listening and the other for receiving. In your while
loop, you should remove the listener part, and keep the code in a different thread. This way you can receive and type on the console at the same time.
def recv():
while True:
data = s.recv(1024).decode()
if not data: sys.exit(0)
print data
Thread(target = recv).start()
Last Updated : 17 Jun, 2021,GATE CS 2021 Syllabus
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
last modified July 29, 2022
#!/usr/bin/python import socket ip = socket.gethostbyname('example.com') print(ip)
The example prints the IP address of example.com
.
$. / get_ip.py
93.184 .216 .34
#!/usr/bin/python
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
message = b ''
addr = ("djxmmx.net", 17)
s.sendto(message, addr)
data, address = s.recvfrom(1024)
print(data.decode())
We import the socket
module.
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
A datagram socket for IPv4 is created.
message = b ''
14 January 2022 , 3 May 2022 , 12 February 2022 , 5 June 2022
Go ahead and open your Python REPL. In it, type this:
# REPL 1 >>> import socket >>> server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> server.bind(("localhost", 7342)) >>> server.listen() >>> (client_socket, address) = server.accept()
You should notice that your REPL is hanging – that's good news!
Here is what is going on:
First, we import the built-in module socket
, which provides sockets-related functionality.
Then, the line
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
By writing
server.bind(("localhost", 7342))
does. Finally, we want to accept connections and use them for something, so we write
(client_socket, address) = server.accept()
After creating the server, I had a glimpse of inspiration!
I opened my browser and I typed localhost:7342
; I suggest you do the same.
When you do that, your server code is no longer hanging, and you get to inspect the client_socket
:
# (client_socket, address) = server.accept() <- this is no longer hanging.>>> client_socket
<socket.socket fd=580, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 7342), raddr=('127.0.0.1', 61896)>