Issue With a Simple Server - Client Program - python

I run the following script:
import socket, threading, time,Queue
if __name__ == '__main__':
pass
print("Starting...")
def server():
s = socket.socket()
host = socket.gethostname()
port = 1247
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print("Connection accepted from " + repr(addr[1]))
c.send("Server approved connection\n")
print (addr[1]) + ": " + c.recv(1026)
c.close()
def client ():
time.sleep(5)
print("Client Started")
s = socket.socket()
host = socket.gethostname()
port = 1247
s.connect((host, port))
print (s.recv(1024))
inpt = raw_input('type anything and click enter... ')
s.send(inpt)
print ("the message has been sent")
q = Queue.Queue()
t = threading.Thread(client(), args = (q))
t.daemon = True
t.start()
server()
I get this error:
Starting...
Client Started
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
Note that port 1247 is open in my device (Ubuntu OS).
Connection Refused means there is nothing to connect to. But What is wrong with the server, I cannot find the problem with it. Any help is very much appreciated after one week of tries!

When you create the thread, you are accidentally starting the client. The first argument of threading.Thread is client(), which executes the client including the initial sleep. Which blocks the main thread. You should change this to
t = threading.Thread(target=client, args = (q,))
The target argument expects a callable object, i.e. your client. Once you start the tread, it will execute the client on a different thread. Please note, the lack of () after client.

Related

Python Chat Room "ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host"

I'm trying to make a chat room that works on localhost with threading and socket. Here is the code:
# Server side
import threading
import socket
host = 'localhost'
port = 11298
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
clients = []
nicks = []
def broadcast(msg):
for client in clients:
client.send(msg)
def handle(client):
while True:
try:
msg = client.recv(1024)
broadcast(msg)
except:
index = clients.index(client)
clients.remove(client)
client.close()
nick = nicks[index]
broadcast(f'{nick} has left.'.encode('utf-8'))
nicks.remove(nick)
break
def recv():
while True:
print('The chat room is online ...')
client, address = server.accept()
print(f'You are connected to {str(address)}')
client.send('nick'.encode('utf-8'))
nick = client.recv(1024)
nicks.append(nick)
clients.append(client)
broadcast(f'{nick} has joined.'.encode('utf-8'))
client.send('You have been connected!'.encode('utf-8'))
thread = threading.Thread(target = handle(), args = (client))
thread.start()
if __name__ == "__main__":
recv()
# Client side
import threading
import socket
nick = input('Choose a nickname: ')
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('localhost', 11298))
def cl_recv():
while True:
try:
msg = client.recv(1024).decode('utf-8')
if msg == 'nick':
client.send(nick.encode('utf-8'))
else:
print(msg)
except:
print("Something went wrong! Bummer.")
client.close()
break
def cl_send():
while True:
msg = f'{nick}: {input("")}'
client.send(msg.encode('utf-8'))
receive_th = threading.Thread(target = cl_recv)
receive_th.start
Whenever I run the code (one cmd window for the server and two for the clients to test it out) it gives me this error:
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
I'm guessing it's a problem with the client closing the server, but I'm using client.connect. I've looked at the code multiple times and can't figure out how to fix it.
This can be caused by the two sides of the connection disagreeing over whether the connection timed out or not during a keepalive. (Your code tries to reused the connection just as the server is closing it because it has been idle for too long.) You should basically just retry the operation over a new connection.

How to keep code running on client when server is offline

So i have a server and client program where the two programs communicate with each other.
my problem is when the server disconnects/goes offline the clients program immediately stops running.
how can i make it so the client program keeps running after the server goes off and keeps trying to connect every 20 seconds lets say so when the server goes back online it reconnects. Edit: I forgot to mention I know how to reconnect the main issue is getting the code to keep running instead of stoping when socket disconnects. Also I know for a fact the whole code works. It's If I close the terminal of the server program, then the clients program stops too.
part of client code to reconnect:
import socket
import os
import sys
import subprocess
import time
from time import sleep
s = socket.socket()
host = '192.168.2.16'
port = 9999
connected = False
while connected == False:
try:
s.connect((host,port))
connected = True
except socket.error:
sleep(5)
part of the server code:
import socket
import sys
import os
import threading
import time
from queue import Queue
NUMBER_OF_THREADS = 2
JOB_NUMBER = [1,2]
queue = Queue()
all_connections = []
all_adresses = []
def socket_create():
try:
global s
global host
global port
host = '0.0.0.0'
port = 9999
s = socket.socket()
except socket.error as msg:
print("Error: " + str(msg))
def socket_bind():
try:
global host
global s
global port
print("Binding Socket To Port " + str(port))
s.bind((host, port))
s.listen(5)
except socket.error as msg:
print("Socket Binding Error: " + str(msg) + '/n' + "Trying Again...")
socket_bind()
def accept_connections():
for c in all_connections:
c.close()
del all_connections[:]
del all_adresses[:]
while 1:
try:
conn, address = s.accept()
conn.setblocking(1)
all_connections.append(conn)
all_adresses.append(address)
print("\nConnection has been established: " + address[0])
except:
print("Error accepting connections")
and error:
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
Simple answer i found.
use try and except. simple as that.

Python error: [Errno 111] Connection refused

I am trying to send and receive data using TCP connection using Python. My server and client are in the same file, defined and used as follows.
In the constructor, I define the server as:
self.sock_in = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock_in.bind((self.host_ip, self.host_port))
self.sock_in.listen(1)
Do not worry about the host_ip and host_port variables, they are all fine.
In a function, I am trying to send data as follows:
sock_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP Connection
sock_out.connect((self.remote_ip, self.remote_port))
sock_out.send(self.navigation_data.get_message())
sock_out.close()
And this is my main:
def main(self):
rospy.logwarn("Starting...")
while not rospy.is_shutdown():
conn = self.sock_in.accept()
try:
recv_buffer = conn.recv(BUFFERSIZE_IN)
if recv_buffer != "":
msg = recv_buffer.decode('utf-8')
msg_type = msg[:msg.find(',')]
if msg_type == self.pilot_control.MESSAGE_ID:
self.pilot_control_handler(msg, self.pilot_control_publisher)
else:
rospy.logwarn("Received an unimplemented message type '%s'", msg_type)
except socket.error as socket_error:
rospy.logerr("SocketError: %s", str(socket_error))
And the error I get is:
line 230, in send_83b_package
sock_out.connect((self.remote_ip, self.remote_port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 111] Connection refused
I put some print commands to see where it collapses, and apparently it does not run the accept command. Until there I can see the print commands working, but after the accept method nothing is printed, which means it collapses there.
I suspect the problem is about synchronization. That is, the server does not start fast enough.
Any thoughts?
EDIT:
One of the suggestions was to run the server on a separate thread, which I tried as follows:
def my_tcp_server(self):
# Establish a TCP Connection
self.sock_in = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock_in.bind((self.host_ip, self.host_port))
self.sock_in.listen(1)
rospy.logwarn("ready")
while not rospy.is_shutdown():
rospy.logwarn("before accept")
conn, address = self.sock_in.accept()
rospy.logwarn("after accept")
try:
recv_buffer = conn.recv(BUFFERSIZE_IN)
rospy.logwarn("recv works!")
if recv_buffer != "":
msg = recv_buffer.decode('utf-8')
msg_type = msg[:msg.find(',')]
if msg_type == self.pilot_control.MESSAGE_ID:
self.pilot_control_handler(msg, self.pilot_control_publisher)
else:
rospy.logwarn("Received an unimplemented message type '%s'", msg_type)
except socket.error as socket_error:
rospy.logerr("SocketError: %s", str(socket_error))
conn.close()
def main(self):
rospy.logwarn("Starting..")
threading.Thread(target=self.my_tcp_server).start()
And in my constructor, the order of calls are as follows:
self.main()
self.sendDataFunction()
Which should be okay. However, the accept function is still not working, hence there is no connection.
As you didn't provide a complete executable code example I took your snippet and removed the class declaration aspects, added definitions for ip/port etc. Also added socket timeout. Anywayt this code works for me on Windows 7x64 with 32-bit Python 2.7.8:
import threading
import socket
is_shutdown = False
BUFFERSIZE_IN = 32768
def my_tcp_server():
# Establish a TCP Connection
sock_in = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_in.bind((host_ip, host_port))
sock_in.settimeout(10000)
sock_in.listen(1)
print "ready"
while not is_shutdown:
print "before accept"
conn, address = sock_in.accept()
print "after accept"
try:
recv_buffer = conn.recv(BUFFERSIZE_IN)
print "recv works!"
if recv_buffer != "":
msg = recv_buffer.decode('utf-8')
print "Received",msg
except socket.error as socket_error:
print "SocketError: %s", str(socket_error)
conn.close()
print "Shutting down server"
sock_in.close()
def main():
print "Starting.."
threading.Thread(target=my_tcp_server).start()
def sendData():
sock_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP Connection
sock_out.connect((remote_ip, remote_port))
sock_out.send("ASD")
sock_out.close()
host_ip="127.0.0.1"
remote_ip = host_ip
host_port = 8073
remote_port = host_port
main()
print "Sending"
sendData()
print "Completed"
is_shutdown = True
The output is:
Starting..
Sending
ready
before accept
Completedafter accept
recv works!
Received ASD
Shutting down server
I guess there is something in your class/constructor/something I can't see which is making your code not work.
HTH
barny

Python socket allow only one connection, but disconnect on new, not refuse

In python, you can define maximum number of socket connections by parameter of listen() function... for example:
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), 80))
serversocket.listen(1) // allow only 1 connection
But the problem is that when second client wants to connect, connection is being refused. And I would like to disconnect the old user and connect the new one. Could anybody help me with that?
Probably an answer:
I am posting it in question as it is probable answer (I didn't have time to check it)
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), 80))
serversocket.listen(10) // allow 10 connections, but disconnect previous later
someone_connected = 0
while 1:
(clientsocket, address) = serversocket.accept()
if(someone_connected) someone_connected.close()
someone_connected = clientsocket
I am not sure that I fully understand you question, but I think the following example can meet your requirement. the server can disconnect the old user and serve the new one.
the sever side:
#!/usr/bin/env python
import socket
import multiprocessing
HOST = '127.0.0.1'
PORT = 50007
# you can do your real staff in handler
def handler(conn, addr):
try:
print 'processing...'
while 1:
data = conn.recv(1024)
if not data:
break
print data
conn.sendall(data)
conn.close()
print 'processing done'
except:
pass
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(5)
processes = []
while True:
conn, addr = s.accept()
print conn, addr
[p.terminate() for p in processes] # to disconnect the old connection
# start process newer connection and save it for next kill
p = multiprocessing.Process(target=handler, args=(conn, addr))
processes = [p]
p.start()
newest_conn = conn # this is the newest connection object, if you need it
For test, the client side:
#!/usr/bin/env python
import socket
import time
import multiprocessing
HOST = '127.0.0.1'
PORT = 50007
def client():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
time.sleep(0.1)
try:
for n in range(20):
s.send(str(n))
data = s.recv(1024)
print data
time.sleep(0.5)
s.send('')
s.close()
except:
pass
if __name__ == "__main__":
for i in range(5):
print 'user %i connect' %i
p = multiprocessing.Process(target=client)
p.start() # simulate a new user start connect
time.sleep(3)
Try it :-)
You have a wrong assumption built into your question - the single argument to socket listen() is not the "number of connections", but a backlog - number of pending, but not yet accepted client connections the kernel holds for you for a while.
Your problem then seems to be that you have accepted one connection, and reading/writing to it in a loop, and not calling accept() again. The kernel holds the request for any new client connection for some timeout, then notifies the client that the server is not accepting it.
You want to look into select() functionality, as suggested in the comments.

Threading - How to recieve and reply at the same time using the socket module?

I am trying to make a simple LAN instant messager where many clients connect to the server and the server replies back and can see what the client is saying. I have tried but my lack of knowledge using the threading module has limited me. At the moment, however, the server only gets a message and has to reply to get the next one. I am trying to make it so the server can see all the messages it receives instantly and can reply whenever he need to. How?
Server Code:
from threading import *
import socket
s = socket.socket()
host = socket.gethostbyname(socket.gethostname())
port = 1337
s.bind((host, port))
s.listen(5)
def getMainThread():
for thread in enumerate(): # Imported from threading
if thread.name == 'MainThread':
return thread
if thread.name == 'Thread':
return thread
return None
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.socket = socket
self.address = address
self.start() # Initated the thread, this calls run()
def reply(self):
reply = getThread()
while reply and reply.isAlive():
sent = input("Enter Message: ")
self.socket.send(bytes(sent, 'UTF-8'))
def run(self):
main = getMainThread()
while main and main.isAlive():
message = self.socket.recv(8192).decode('utf-8')
self.socket.send(b"Got your message.. send another one!")
print('Someone:',message)
sent = input("Enter Message: ")
self.socket.send(bytes(sent, 'UTF-8'))
self.socket.close()
while True:
c, addr = s.accept()
client(c, addr)
Client Code:
import socket
host = socket.gethostbyname(socket.gethostname())
print("""
================================================================================
Welcome to Coder77's local internet message for avoiding surveillance by the NSA
================================================================================
The current soon to be encrypted server is {0}
""".format(host))
#host = input("Please select the IP you would like to communicate to: ")
print("Now connecting to {0}....".format(host))
sock = socket.socket()
try:
sock.connect((host, 1337))
while True:
message = input("Enter Message: ")
if message == 'quit':
break
sock.send(bytes(message, 'UTF-8'))
recieved = sock.recv(8192).decode('utf-8')
print('Server responded with:', recieved)
except socket.error:
print ("Host is unreachable")
sock.close()
Also, is it possible using Threading so that 2 while statements can run at the same time? If so, can someone give me an example?
Boosting this to try and get an answer. Anyone?

Categories

Resources