I have a little problem in my client chat app, basically the app crash if I try to do a function that receive data separately from the one that I use to send them. Basically, if I have only this function it works:
def send_message (ip_address, port, message):
#Connect to the server
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect((ip_address, port))
#Convert message in bytes-like object
message = message.encode("utf8")
c.send(message)
#Receive data
data = c.recv(88888888888888)
#Decode data from bytes-like object
data = data.decode("utf8")
return data
If I try to do two function, it doesn't work, like this:
def send_message (ip_address, port, message):
#Connect to the server
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect((ip_address, port))
#Convert message in bytes-like object
message = message.encode("utf8")
c.send(message)
def receive_message (ip_address, port, message):
#Connect to the server
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect((ip_address, port))
#Receive data
data = c.recv(88888888888888)
#Decode data from bytes-like object
data = data.decode("utf8")
return data
When I try to call the receive_message function from my GUI the app stop working. Hoping someone can help
EDIT: This is the server code:
import socket
import sys
from threading import Thread
def client_thread(conn, ip, port, MAX_BUFFER_SIZE = 4096):
# the input is in bytes, so decode it
input_from_client_bytes = conn.recv(MAX_BUFFER_SIZE)
# decode input and strip the end of line
input_from_client = input_from_client_bytes.decode("utf8").rstrip()
print("Result of processing is: {}".format(input_from_client))
vysl = res.encode("utf8") # encode the result string
conn.sendall(vysl) # send it to client
conn.close() # close connection
print('Connection ' + ip + ':' + port + " ended")
def start_server():
import socket
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this is for easy starting/killing the app
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print('Socket created')
try:
soc.bind(("127.0.0.1", 12345))
print('Socket bind complete')
except socket.error as msg:
import sys
print('Bind failed. Error : ' + str(sys.exc_info()))
sys.exit()
#Start listening on socket
soc.listen(10)
print('Socket now listening')
# for handling task in separate jobs we need threading
from threading import Thread
# this will make an infinite loop needed for
# not reseting server for every client
while True:
conn, addr = soc.accept()
ip, port = str(addr[0]), str(addr[1])
print('Accepting connection from ' + ip + ':' + port)
try:
Thread(target=client_thread, args=(conn, ip, port)).start()
except:
print("Terible error!")
import traceback
traceback.print_exc()
soc.close()
start_server()
You don't show what's on the other end of your connection. I guess it's something like an echo
which responds to received messages.
In the first case you open a single connection, first write to it, then read a response.
In the second case you open two separate connections. You write to the first one socket, read from the second one. Is anyone writing on the second socket? I guess not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.