Python Forum

Full Version: Need some help with networking problems...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
so im trying to make a server that can communicate with the client, this is my code
server:
#!/usr/bin/python3           # This is server.py file
import socket                                         

# create a socket object
serversocket = socket.socket(
	        socket.AF_INET, socket.SOCK_STREAM) 

# get local machine name
host = socket.gethostname()                           

port = 9996                                         

# bind to the port
serversocket.bind((host, port))                                  

# queue up to 5 requests
serversocket.listen(5)                                           

while True:
   # establish a connection
   clientsocket, addr = serversocket.accept()      
   msg = input("what do you want to send?\n")
   clientsocket.send(msg.encode('ascii'))
   
client:
#!/usr/bin/python3 # This is client.py file 
import socket # create a socket object 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # get local machine name

host = socket.gethostname() 
port = 9996 # connection to hostname on the port. 

s.connect((host, port)) # Receive no more than 1024 bytes 
but the problem is i can send one message, and then it wont send more?
why?
help?
I do not see the client sending any message.
(Oct-17-2018, 02:44 PM)wavic Wrote: [ -> ]I do not see the client sending any message.
yeah but its the server thats suppose to send a message to the client, an custom message, allmost like a masseging app
Well, how the client receives the message?
(Oct-17-2018, 03:05 PM)wavic Wrote: [ -> ]Well, how the client receives the message?
it recieves it, and then if i send ANOTHER message it wont send it? or it will send but the client wont receiv it?
HEEEELP
The client closes and that closes the connection. Put a while loop in the client.
As your code is written, the client program exits after sending the first message, which closes the socket connection. The server complains about that. If you want to be able to send multiple messages from the client, you'll need a looping or event structure on both the client and server side.