Can a socket send and receive at the same time 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]

asked Oct 30, 2015 at 10:39

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[]

Adrian Mole

46.1k124 gold badges45 silver badges73 bronze badges

answered Oct 30, 2015 at 10:54

SugamSugam

5873 silver badges12 bronze badges

One socket may send and receive at the same time but one thread has to only send data and other thread has to only receive data. This way send[] doesn't have to wait for the end of recv[] and recv[] doesn't have to wait for the end of send[] so they not block each other.

Without threads it would be harder to do this.

client.py

import socket
import threading
import sys

# --- functions ---

def recv_msg[]:
    while True:
        recv_msg = conn.recv[1024]
        if not recv_msg:
            sys.exit[0]
        recv_msg = recv_msg.decode[]
        print[recv_msg]

def send_msg[]:
    while True:
        send_msg = input[str["Enter message: "]]
        send_msg = send_msg.encode[]
        conn.send[send_msg]
        print["message sent"]

# --- main ---

host = socket.gethostname[]
port = 8080

s = socket.socket[]
s.bind[[host, port]]
s.listen[1]

print["Waiting for connections"]
conn, addr = s.accept[]

print["Client has connected"]
conn.send["Welcome to the server".encode[]]

# thread has to start before other loop
t = threading.Thread[target=recv_msg]
t.start[]

send_msg[]

server.py

import socket
import threading
import sys

# --- functions ---

def recv_msg[]:
    while True:
        recv_msg = conn.recv[1024]
        if not recv_msg:
            sys.exit[0]
        recv_msg = recv_msg.decode[]
        print[recv_msg]

def send_msg[]:
    while True:
        send_msg = input[str["Enter message: "]]
        send_msg = send_msg.encode[]
        conn.send[send_msg]
        print["message sent"]

# --- main ---

host = socket.gethostname[]
port = 8080

s = socket.socket[]
s.bind[[host, port]]
s.listen[1]

print["Waiting for connections"]
conn, addr = s.accept[]

print["Client has connected"]
conn.send["Welcome to the server".encode[]]

# thread has to start before other loop
t = threading.Thread[target=recv_msg]
t.start[]

send_msg[]

Can you send and receive on a socket at the same time?

Once connected, a TCP socket can only send and receive to/from the remote machine. This means that you'll need one TCP socket for each client in your application. UDP is not connection-based, you can send and receive to/from anyone at any time with the same socket.

How do you send and receive data using socket in python?

Overview:.
The send[]method of Python's socket class is used to send data from one socket to another socket..
The send[]method can only be used with a connected socket. ... .
The send[] method can be used to send data from a TCP based client socket to a TCP based client-connected socket at the server side and vice versa..

How does python handle multiple socket connections?

How to Create Socket Server with Multiple Clients in Python.
import socket import os from _thread import * ... .
ServerSideSocket = socket. ... .
try: ServerSideSocket. ... .
def multi_threaded_client[connection]: connection. ... .
while True: Client, address = ServerSideSocket. ... .
import socket ClientMultiSocket = socket..

Chủ Đề