0

I started writing python servers this week, and I wrote just the simplest server I could make for an example. the server and client use sockets, the server listens to a socket, then sends back to the client what it got after saying "I got: ". I run both of those with CMD, and every time I connect to the server I need to turn it on again. I tried removing the sockets closing commands without success and many other methods with the same result, so if anyone has any idea how I can fix it I will be very grateful.

EDIT: the server's code: http://pastebin.com/X4sEwLPf the client's code: http://pastebin.com/uyDiUM4E

talbor49
  • 3
  • 4
  • 1
    Could you please include some example code with your question and any relevant error messages? – Kevin London Oct 15 '14 at 19:14
  • They both work completely fine, except to the fact that I have to reopen the server every time I want to connect to it again. – talbor49 Oct 15 '14 at 19:17

1 Answers1

1

In the Server you are only accepting once a connection form a client. You have to put a loop arround the server code.

import socket
server_socket = socket.socket()
server_socket.bind(('0.0.0.0',8820))
server_socket.listen(10)
while True:
    (client_socket,client_adress) = server_socket.accept()
    client_input = client_socket.recv(1024)
    client_socket.send("I got : " + client_input)
    client_socket.close()
server_socket.close()
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • This works! thank you very much! but is this the way big servers use? I mean does it have any disadvantages compared a different way and could be replaced with it? because a while True loop looks pretty dangerous to me for some reason. – talbor49 Oct 15 '14 at 19:35
  • loops are nothing dangerous. Big servers use some kind of concurrency. – Daniel Oct 15 '14 at 19:50
  • How about threading? – Scott Mar 05 '23 at 17:05