I get this error when i connect on the client.
felix+
Traceback (most recent call last):
File "c:\Users\felix\Documents\CODE\Uno02\uno02_server.py", line 23, in <module>
server.send(join.encode('utf-8'))
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
The client simply receives some information from the user, and connects to the server. I am not sure what I typed wrong
Client code:
import socket
server_ip = input("Enter the server IP: ")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((server_ip,42069))
name = input("Please enter a username: ")
client.send(name.encode())
while True:
server_msg = client.recv(1024)
print(server_msg.decode())
and server code:
name_list = []
ip_list = []
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("127.0.0.1",42069))
server.listen()
while(True):
(clientConnected, clientAddress) = server.accept()
print("connection gained %s:%s"%(clientAddress[0], clientAddress[1]))
clientdata = clientConnected.recv(1024)
name = clientdata.decode()
join = name + " joined"
name_list.append(clientdata)
ip_list.append(clientAddress[0])
print(name+"+")
server.send(join.encode('utf-8'))
Replace
server.send(join.encode('utf-8'))
by
clientConnected.send(join.encode('utf-8'))
check Python TCP Communication for further details
Related
I am having a client-server program to send from the server to the client
the server in a different folder from the client (like there is a folder for the server code and another folder for the client code)
-when running the server and the client the server gives me an error that the socket disconnect and i don't know why:
Traceback (most recent call last):
File "server.py", line 30, in
s.send(ans.encode(FORMAT))
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 s
upplied
the server code:
import socket
import os
import json
SIZE = 1024
FORMAT = "utf-8"
# creating a TCP socket
print('\n','_'*50,"\n\nThe server started.. ")
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("127.0.0.1", 5100))
# converted into passive socket - queue up to 2 requests
print("\n\nThe server is listening.. ")
s.listen(5)
while True:
cli, sockname = s.accept()
print('Accepted request from', sockname[0], 'with port number', sockname[1])
listoffile= [x for x in os.listdir() if x.endswith(".txt")]
ans =json.dumps(listoffile)
thelist=ans.replace(',','\n').replace('[',"").replace('"',"").replace(']',"")+"\n exit"
print('\n The server will send the list to the client:\n',thelist)
s.sendall(ans.encode(FORMAT))
rec = cli.recv(SIZE).encode(FORMAT)
if rec != 'exit':
with open(rec,'rb') as f:
filetosend =f.read(SIZE)
s.sendall(filetosend.encode(FORMAT))
elif rec == 'exit':
cli.close()
s.close()
The client code:
import socket
host = '127.0.0.1'
port = 5100
SIZE = 1024
FORMAT = "utf-8"
# creating a TCP socket
print('\n','_'*50,"\n\nThe client side started.. ")
c = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
c.connect((host, port))
while True:
msg = c.recv(SIZE).decode(FORMAT)
msg=msg.replace(',','\n').replace('[',"").replace('"',"").replace(']',"")+"\n exit"
print(f"Choose one of the file to get a copy: {msg}")
filename = input("Enter the file name or exit to quit:")
if filename != 'exit':
c.send(filename.encode(FORMAT))
f=open(filename,'wb')
data=c.recv(SIZE)
f.write(data)
print('The file {filename} is copied.')
elif filename == 'exit':
c.close()
I have two pieces of code, a server and a client for a chat application using python 'socket' and 'selectors' modules. The 'service_connection' function should only do something when a socket is ready for reading. However as far as i'm aware, all the client does is send a message containing the client's username. This is dealt with in 'accept_connection' function. However I then receive an error resulting from the 'service_connection' function. Why is my program getting to this point?
Client:
import socket
import selectors
HEADER_LENGTH = 10
HOST = '127.0.0.1'
PORT = 54321
username = input('Enter username: ')
# Set up client socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.setblocking(False)
# Send username to server with a header
username_enc = username.encode("utf-8")
user_header = f'{len(username_enc):<{HEADER_LENGTH}}'.encode("utf-8")
sock.send(user_header + username_enc)
Server:
import socket
import selectors
HEADER_LENGTH = 10
HOST = '127.0.0.1'
PORT = 54321
clients = [] # List for storing client information
def accept_connection(sock):
"""Function to accept a new connection"""
conn, addr = sock.accept()
print(f'Connected to by {addr}')
conn.setblocking(False)
# Receive first message. This will contain username
user_header = conn.recv(HEADER_LENGTH)
username_length = int(user_header.decode("utf-8").strip())
username = conn.recv(username_length).decode("utf-8")
# Register socket with sel
events = selectors.EVENT_READ | selectors.EVENT_WRITE
data = {"addr": addr, "username": username, "socket": conn}
sel.register(conn, events, data=data)
clients.append(data)
def service_connection(key, mask):
"""Function to service an existing connection"""
sock = key.fileobj
data = key.data
# Check for read events
if mask & selectors.EVENT_READ:
# Receive message from socket
msg_header = sock.recv(HEADER_LENGTH)
if msg_header is None:
print(f'Closing connection to {data.addr}')
sel.unregister(sock)
sock.close()
clients = [client for client in clients if client.addr!=data.addr]
return None
msg_length = int(msg_header.decode("utf-8").strip())
msg = sock.recv(msg_length).decode("utf-8")
# Create username header
username_enc = data.username.encode("utf-8")
user_header = f'{len(username_enc):<{HEADER_LENGTH}}'.encode("utf-8")
# Distribute message to all other connected clients
for client in clients:
if client.addr == data.addr:
continue
client.socket.send(user_header + msg_header + msg.encode())
# Set up listening socket
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
lsock.bind((HOST, PORT))
lsock.listen()
lsock.setblocking(False)
# Set up selectors object
sel = selectors.DefaultSelector()
sel.register(lsock, selectors.EVENT_READ, data=None)
# Loop over blocking calls to select
while True:
events = sel.select()
for key, mask in events:
if key.data is None:
# Listening socket is ready. Accept new connection
accept_connection(key.fileobj)
else:
# Existing socket is ready. Service it
service_connection(key, mask)
Both server and client run as expected. However once I provide a username in the client script, I receive the following output server-side:
Connected to by ('127.0.0.1', 62636)
Traceback (most recent call last):
File "server.py", line 78, in <module>
service_connection(key, mask)
File "server.py", line 44, in service_connection
msg_length = int(msg_header.decode("utf-8").strip())
ValueError: invalid literal for int() with base 10: ''
This is because when the client script is finished it sends 0 bytes to the server and closes the socket connection. The server-side socket therefore has 0 bytes ready for reading. For service_connection() to work properly the line if msg_header is None: needs to be changed to if not msg_header:
What I'm trying to create are a set of server and client scripts; the server script prompts a user for raw input, stores that input in a dictionary and converts it to json with the json.dumps() function. The converted dictionary is then stored in the jasonFile variable which is then sent to the client. The json dictionary is working but I'm struggling with the networking side of things.
Here is my server code:
def Main():
host = '0.0.0.0'
port = 5000
s.bind((host, port))
s.listen(5)
print "Server Started"
while True:
addr = s.accept()
print "Client Connected from IP: " + str(addr)
serverMessage = "Connection Established: Would you like to download the Json dictionary?"
s.send(serverMessage)
clientReply = s.recv(1024)
if clientReply in ['Y', 'y', 'Yes', 'yes', 'YES']:
s.send(jasonFile)
s.close()
else:
print "Connection from " + addr + " closed!"
s.send("Connection Error!")
s.close()
And here is my client code:
def Main():
host = raw_input("Please enter the server IP you wish to connect to: ")
port = 5000
#define client to use socket module to connect via IPV4 and TCP only
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
serverMessage = client.recv(1024)
print serverMessage
clientReply = raw_input("Type 'Yes' To download dictionary")
if clientReply in ['Y', 'Yes', 'y', 'yes', 'YES']:
client.send(clientReply)
jasonRecv = client.recv(1024)
print jasonRecv
else:
client.close()
print "Disconnected from server!"
I haven't gotten as far as converting the json data back to a string on the client yet because the server throws me an error when the client tries to connect.
The error message I get from IDLE is:
Server Started
Client Connected from IP: (<socket._socketobject object at 0x000000000401E048>, ('127.0.0.1', 34375))
Traceback (most recent call last): File "D:/Server.py", line 105, in <module>
Main()
File "D:/Server.py", line 94, in Main
s.send(serverMessage)
error: [Errno 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
I thought I was defining the address to send data to in the addr variable, but apparently not?
Try:
conn, addr = s.accept()
...
conn.send(serverMessage)
i.e. replace s. calls with conn. which represents the accepted socket connection from the client.
From the Python socket API:
socket.accept()
Accept a connection. The socket must be bound to an address and listening for connections.
The return value is a pair (conn, address) where conn is a new socket
object usable to send and receive data on the connection, and address
is the address bound to the socket on the other end of the connection.
Examples are provided at the end of the page.
Also see the Python Socket Programming Howto
I have created two scripts which establish a client socket and server socket in localhost.
Server socket
import socket
from time import ctime
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpsersoc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcpsersoc.bind(ADDR)
tcpsersoc.listen(5)
while True:
print('waiting for connection .... ')
print(ADDR)
tcpClisoc,addr = tcpsersoc.accept()
print('......connected from', addr)
while True:
data = tcpClisoc.recv(BUFSIZ)
if not data:
break
tcpClisoc.send('[%s]%s'%(bytes(ctime(),'UTF-8'),data))
tcpClisoc.close()
tcpsersoc.close()
Client socket
from socket import *
HOST= '127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpCliSock = socket(AF_INET,SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
data = input('>')
if not data:
break
tcpCliSock.send(data.encode('utf-8'))
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print(data.decode('utf-8'))
tcpCliSock.close()
I'm still getting the below error despite converting the data into a bytes object. I'm using python 3.x
this is the error raised by the server socket
waiting for connection ....
('', 21567)
......connected from ('127.0.0.1', 52859)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
import exampletcpserver
File "C:/Python34\exampletcpserver.py", line 23, in <module>
tcpClisoc.send('[%s]%s'%(bytes(ctime(),'UTF-8'),data))
TypeError: 'str' does not support the buffer interface
Please let me know where i'm going wrong.
You are trying to send a string, but sockets require you to send bytes. Use
tcpClisoc.send(('[%s]%s' % (ctime(), data.decode("UTF-8"))).encode("UTF-8"))
Python 3.5 will support the alternative
tcpClisoc.send(b'[%s]%s' % (bytes(ctime(), 'UTF-8'), data))
I am making a chat program.
I have a (TCP) server which creates a new thread for every connection request it gets.
I'm having problems when the client quits/terminates connection. The server raises an error(below). How do I handle it?
And, the server has to send the 'data' it receives from one client to another (changeable) client.
How do I implement this??
I receive this error when the client quits :
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python2.7 For Chintoo\lib\threading.py", line 552, in __bootstrap_inner
self.run()
File "C:\Python2.7 For Chintoo\lib\threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\Users\karuna\Desktop\Jython\Python\My Modules\Network\Multi-server.py", line 23, in recv_loop
data = client.recv(1024)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
My scripts:
Multi-server.py
import os, socket, time, threading, random
class Server:
def __init__(self,host,port,user):
self.port = port
self.host = host
self.user = user
self.bufsize = 1024
self.addr = (host,port)
self.socket = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
self.socket.bind(self.addr)
print "Server running on",host,"at port",port
self.socket.listen(5)
def recv_loop(server,client,caddr):
print 'Connected To',caddr
while True:
global clients
name = clients[client]
data = client.recv(1024)
if not data:
break
print name + " said: " + data
client.close()
host = 'localhost'
port = random.randint(1025,60000)
user = 'No one'
server = Server(host, port, user)
clients = {}
threads = []
while True:
client, caddr = server.socket.accept()
# name extraction
name = client.recv(1024)
clients[client] = name
thread = threading.Thread(target=recv_loop, args=(server,client, caddr))
thread.start()
client.py
from socket import *
host = 'localhost'
name = raw_input('Enter name: ')
port = int(raw_input('Enter server port: '))
bufsiz = 1024
addr = (host, port)
tcpClient = socket(AF_INET , SOCK_STREAM)
tcpClient.connect(addr)
# sending name
tcpClient.send(name)
while True:
data = raw_input('> ')
if not data:
break
tcpClient.send(data)
raw_input('Enter to Quit')
Problem 1
Just close the socket connection at client side:
raw_input('Enter to Quit')
tcpClient.close()
Problem 2
You are looking at producer consumer problem here.
Basic solution:
Receiving loop should acquire a threading.Condition, update a global array and call notifyAll. Sending loops should acquire the condition, read data from the array and send to client.
I haven't done socket programming in Python, but you might want to cleanly close your socket connection before the client quits. I would use close method in the client.