I am writting a simple messaging program (for 2 users) that works on the terminal.
For its implementation, I decided to create 2 processes, one for the server (waits for the message to arrive from the other user), and another one for the client (just sends the message to the server process of the other user)
The fact is that when I run it, I get the following error:
C:\>python message.py
> Process Process-2:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 258, in_bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\message.py", line 12, in send_messages
message = raw_input('> ')
EOFError
Process Process-1:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 258, in
_bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\message.py", line 25, in receive_messages
message = sc.recv(1024)
error: [Errno 10054] An existing connection was forcibly closed by the
remote host
This is my Python code
from multiprocessing import Process
import socket
direction = "localhost"
global s
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def send_messages():
s.connect((direction, 5124))
while True:
message = raw_input('> ')
s.send(message)
if message == 'close':
break
print 'Bye'
s.close()
def receive_messages():
s.bind(("localhost",5124))
s.listen(2)
sc, addr = s.accept()
while True:
message = sc.recv(1024)
print message
sc.close()
s.close()
if __name__ == '__main__':
p1 = Process(target = receive_messages)
p1.start()
p2 = Process(target = send_messages)
p2.start()
p1.join()
p2.join()
NOTE 1: There can be some indentation errors due to cut and paste from my text editor to stackoverflow.
NOTE 2: I am working on Windows 10