I have tried looking at this question but didn't help: Python Server Client WinError 10057
My code is this:
import socket
def actual_work():
return 'dummy_reply'
def main():
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
try:
sock.bind( ('127.0.0.1', 6666) )
while True:
data, addr = sock.recvfrom( 4096 )
reply = actual_work()
sock.sendto(reply, addr)
except KeyboardInterrupt:
pass
finally:
sock.close()
if __name__ == '__main__':
main()
Got the following error:
Traceback (most recent call last):
File "testserver.py", line 22, in <module>
main()
File "testserver.py", line 14, in main
data, addr = sock.recvfrom( 4096 )
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
-
1You can't recvfrom anyone unless you have accepted a socket connection first. Seeing as this is a TCP socket... So the answer on the other question should solve your problemAndre Motta– Andre Motta2018年08月13日 12:51:08 +00:00Commented Aug 13, 2018 at 12:51
-
I tried. But it didn't worked. Can you modify the code that I have mentioned in the question and show what exact you mean. I am getting many error after the modification. please let me know.Jaffer Wilson– Jaffer Wilson2018年08月13日 12:59:48 +00:00Commented Aug 13, 2018 at 12:59
-
@AndreMotta Can you help me with that?Jaffer Wilson– Jaffer Wilson2018年08月13日 13:02:26 +00:00Commented Aug 13, 2018 at 13:02
-
I will add an example code as I'm in my cellphone right now from github. But this what you have is a server. You need some other code to connect to it so that you can accept the connection... Like this: gist.github.com/Integralist/3f004c3594bbf8431c15ed6db15809aeAndre Motta– Andre Motta2018年08月13日 13:04:44 +00:00Commented Aug 13, 2018 at 13:04
-
Think of it this way, this is a TCP socket. So a connection needs to be estabilished prior to any data transference. Which means a client program needs to request a connection to the address of the server and the server must accept it. Only after the acceptance is completed the client can send data to the server and the server can recvfrom() the sent dataAndre Motta– Andre Motta2018年08月13日 13:06:10 +00:00Commented Aug 13, 2018 at 13:06
1 Answer 1
First of all you need a client code to connect with your server. This is a TCP socket, therefore it is connection oriented.
This means that prior to any data transference (socket.recv(), socket.send() ) you need to request a connection from the client to the server, and the server must accept the connection.
After the connection is estabilished you will be able to freely send data between the sockets.
this is an example of this simple socket design that can be applied genericaly to your program:
Client Example
import socket
# create an ipv4 (AF_INET) socket object using the tcp protocol (SOCK_STREAM)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect the client
# client.connect((target, port))
client.connect(('0.0.0.0', 9999))
# send some data (in this case a String)
client.send('test data')
# receive the response data (4096 is recommended buffer size)
response = client.recv(4096)
print(response)
Server Example
import socket
import threading
bind_ip = '0.0.0.0'
bind_port = 9999
max_connections = 5 #edit this to whatever number of connections you need
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(max_connections) # max backlog of connections
print (('Listening on {}:{}').format(bind_ip, bind_port))
def handle_client_connection(client_socket):
request = client_socket.recv(4096 )
print (str(request))
client_socket.send('ACK!')
client_socket.close()
while True:
client_sock, address = server.accept()
print (('Accepted connection from {}:{}').format(address[0], address[1]))
client_handler = threading.Thread(
target=handle_client_connection,
args=(client_sock,) # without comma you'd get a... TypeError: handle_client_connection() argument after * must be a sequence, not _socketobject
)
client_handler.start()
This example should print test data in the server console and ACK! in the client console.
edit: Not sure about python3 prints working as i wrote them here... but it's just a small detail. The general idea is what matters in this case. When i get to a pc I will try to run this and correct the prints if there is any syntax mistake