There are some other posts about this issue but none did help me with mine.
I'm trying to build a total simple server - client relationship in python
server.py
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1",8889))
s.listen(1)
try:
while True:
client, add = s.accept()
data = client.recv(1024)
if not data:
print 'No data'
print data
finally:
s.close()
client.py
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1",8889))
try:
while True:
message = 'Foo'
s.send(message)
ans = s.recv(1024)
print ans
finally:
s.close()
I start by running the server first, but when I try to run the client I'm getting this Errno 10054 --> An existing connection was forcibly closed by the remote host
While request the browser with the ip and the related port, I receive some data.
I'm quiet new to networking, so please explain what might be obvious wrong in my code.
EDIT* Main issue is, that the client is somehow wrong, because it returns also an empty string on recv
Thank you in advance
Main issue is, that the client is somehow wrong, because it returns also an empty string on recv
The client isn't receiving anything from the server because the server is not sending anything.
On the server side, after print data, adding client.send(data) will send the string back to the client.
I am guessing:
The server accepts one socket and then does
client, add = s.accept()
data = client.recv(1024)
...
client, add = s.accept()
The client does this in the mean time:
s.send(message)
ans = s.recv(1024) # blocks until timeout
If now an other client connects to the server then client is replaced, the socket garbage collected and closed. s.recv(1024) will then tell that the connection is reset.
Have a look at import select or twisted (google around) to handle multiple connections at once.
Related
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_server:
socket_server.connect(("77.222.42.207", 1337))
socket_server.send("get_flag".encode())
server_unswer = (socket_server.recv(1024)).decode()
print(server_unswer)
while (messsage := input("Я: ")) != "exit":
socket_server.send(messsage.encode())
messsage = socket_server.recv(1024)
messsage = messsage.decode()
print(messsage)
I'm trying to send a string to the server, but I can't send the data and get a response, what's the problem, please help.
When I connect to the server via netcat, everything is fine.
It looks like nothing is wrong with your code.Just create a simple server as shown below and connect your client socket program.
import socket
server = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP, fileno=None)
server.bind(('IP address', port))
server.listen(10)
client, _ = server.accept()
# Instead of a infinite while loop a for loop in given with a count of 10
for i in range(10):
data = client.recv(65535).decode()
print(data)
client.send(data.encode())
server.close()
Obviously you will have to update socket_server.connect(("77.222.42.207", 1337))
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_server:
socket_server.connect(("77.222.42.207", 1337))
server_unswer = (socket_server.recv(1024)).decode()
print(server_unswer)
socket_server.send("get_flag\n".encode())
server_unswer = (socket_server.recv(1024)).decode()
print(server_unswer)
I was finally able to get a response from the server, it was because NetCat automatically adds \n when sending text and the command on the server worked, but I did not add this character when sending text 🤦
I had to figure out how to use Wireshark to figure it out)
I am creating a game in Pygame that requires a client-server part for the multiplayer.
First, I am checking if there are less than two connections. If this is the case, the client will be shown a screen that says 'waiting for connections'.
I have got the client to successfully send a '1' message to the server, which will respond with a '1' if the server is not full. Therefore, if the server does not respond with a 1, the server is full, and the client can continue.
However, I am getting this error mentioned in the title.
Server code:
import socket
import sys
import threading
from _thread import *
import time
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
ip=socket.gethostbyname(host)
port=8000
connections=[]
print('Your local ip address is',ip)
s.bind((host,port))
s.listen(2)
def threaded_client(connection):
while True:
data=connection.recv(2048) #anything we receive
if not data:
break
connection.close()
def checkconnected(connections):
noofconn=len(connections)
while True:
print('Waiting for a connection...')
connection,address=s.accept()
print(address,'has connected to server hosted at port',address[1])
connections.append(address)
data=connection.recv(1024)
received=[]
counter=0
for letter in data:
received.append(data[counter])
counter+=1
received=(chr(received[0]))
if received=='1':#handling initial connections
if len(connections)!=2:
s.sendall(b'1')
if not data:
break
start_new_thread(threaded_client,(connection,))
s.close()
The client code that calls it:
host=socket.gethostname()
ip=socket.gethostbyname(host)
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
address=address
port=8000
if address==ip:
ishost=True
else:
ishost=False
try:
s.connect((address,port))
connectionwaitingmenu()
connected=False
while connected==False:
s.sendall(b'1')
data=s.recv(1024)
received=[]
counter=0
for letter in data:
received.append(data[counter])
counter+=1
received=(chr(received[0]))
if received=='1':
connected=False
elif received!='1':
connected=True
classselection()
The error occurs on the s.sendall(b'1') line in the server code.
There are a few other problems in your code, but the cause of the error in the title is that you're using the wrong socket to send and receive data on the server side.
When a server accepts a new client (conn, addr = server.accept()) this returns a new socket, which represents the channel through which you communicate with the client. All further communication with this client happens by reading and writing on conn. You should not be calling recv() or sendall() on s, which is the server socket.
The code should look something like this:
# Assuming server is a bound/listening socket
conn, addr = server.accept()
# Send to client
conn.sendall(b'hello client')
# Receive from client
response = conn.recv(1024)
# NO
server.send(b'I am not connected')
this_wont_work = server.recv(1024)
I am working on socket programming and I have to keep the track of all the clients that join the server.
So I am just keeping them an list:
client = []
and appending the client to it whenever they connect to the server.
Now, I have to remove the client from the list whenever the client disconnects.
The trouble is how can a server know if a client has disconnected from that server.
For connecting server, I am using:
s = socket.socket()
s.bind((host, port))
client = []
while True:
c, addr = s.accept()
client.append(addr)
s.close()
Normally, when a socket is disconnected, it raises a socket.error if written to, and also returns b"" when read from.
So what you can do is, you can catch that exception and remove the client if writing to, and check if the data is empty if reading from.
Btw, you need to use the socket as the client, not its addr.
Like this:
try:
sock.send(data)
except socket.error:
client.remove(sock)
data = sock.receive(1024)
if not data:
sock.close()
client.remove(sock)
I want to create a simple communication between a server and a client using sockets. The cliend is supposed to send a message and then the server sends a message to the client.
This is my Client code :
import socket
s = socket.socket()
HOST = '127.0.0.1'
s.connect((HOST, 1234))
s.send('Hi')
print ('Client send')
print s.recv(1024)
s.close
This is my Server's code :
import socket
s = socket.socket()
HOST = '127.0.0.1'
s.bind((HOST, 1234))
s.listen(5)
while True:
c, addr = s.accept()
c.send('Hi client')
c.close()
But it only prints "Client send " .
In your server, after having sent 'Hi client' you must wait for the client to have read the message.
You could do either of two things:
Use shutdown() on the socket in the server, see https://docs.python.org/2/library/socket.html#socket.socket.shutdown
Do a .recv(..) in the server, which will terminate after the client has close'ed
the socket after reading the reply the server sent.
Update: tried it on my system (MacOSX). Started two python interpreters. Pasted the server code verbatim in one; server is now up and running and accepting connections.
In the other python interpreter, the client shell, I did the following
>>> import socket
>>> HOST = '127.0.0.1'
>>> def test():
... s = socket.socket()
... s.connect((HOST, 1234))
... s.send('Hi')
... print s.recv(1024)
... s.close() # <== Note function call here!
...
>>> test()
Hi client
>>> test()
Hi client
>>> test()
Hi client
>>> test()
Hi client
This demonstrates that - at least on my system - the code works as anticipated.
I can't reprocude your problem and therefore vote to close your question.
Here's the code I used:
import socket
import threading
HOST = '127.0.0.1'
PORT = 12345
def client():
s = socket.socket()
s.connect((HOST, PORT))
s.send('Hi')
print ('Client send')
print s.recv(1024)
s.close()
def server():
s = socket.socket()
s.bind((HOST, PORT))
s.listen(5)
c, addr = s.accept()
c.send('Hi client')
c.close()
if __name__ == "__main__":
server = threading.Thread(target=server)
server.start()
client()
server.join()
Here's the output I got:
$ python test.py
Client send
Hi client
If the problem persists, please add additional details to your question about why and how your setup still not works. Maybe the problem is just about how you run the programs. Please add details about this as well.
The underlying problem is that you treat sockets as if they were message-busses (one message at a time, always received fully), where in fact they are streams of bytes.
Nobody guarantees you that sending X bytes of data will reach the opposite side as that. It could be X1, X2, X3, Y1 where X1+X2+X3=X, and Y1 being the beginning of the next message Y. And all other kinds of combinations and errors.
To remedy this, real-world client-server apps use protocols. Take HTPP for example: a request starts always with HTTP, and it ends with two newlines. Within this block, there is the Content-Length header telling the client how many bytes to read then - regardless of chunk sizes.
So to really solve your problem, you either have to write a full fledged protocol, or built upon libraries that do that for you - e.g. Twisted, Nanomsg or ZeroMQ
I have the following code for an echo client that sends data to an echo server using socket connection:
echo_client.py
import socket
host = '192.168.2.2'
port = 50000
size = 1024
def get_command():
#..Code for this here
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
while 1:
msg = get_command()
if msg == 'turn on':
s.send('Y')
elif msg == 'turn off':
s.send('N')
elif msg == 'bye bye':
break
else:
s.send('X')
data = s.recv(size)
print 'Received: ',data
s.close()
echo_server.py
import socket
host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
client, address = s.accept()
data = client.recv(size)
if data:
client.send(data)
client.close()
The problem im facing is that in the client s.send works only the first time even though its in an infinite loop. The client crashes with connection timed out, some time after the first send/receive has completed.
Why is s.send working only once ?. How can i fix this in my code ?
Please Help
Thank You
Your server code only calls recv once. You should call accept once if you only want to receive one connection, but then you need to loop calling recv and send.
Your problem is that you are blocking on the accept inside the server's loop.
This is expecting the server to accept connections from more than one client. If you want that, and for each client to send multiple commands, you would need to spawn a new thread (or process) after the accept, with a new while loop (for client communication) in that thread/process.
To fix your example to work with just one client, you need to move the accept outside the loop, like so:
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
client.send(data)