I can't find the error in my socket python program - python

I was following a socket tutorial and suddenly there was a bug in my code that I couldn't understand or find
Can anyone help me find the bug? I referenced with the tutorial code and it doesn't work.
The only way is to copy/paste and I don't like it
this is my server.py file
import socket
import threading
HEADER = 64
FORMAT = 'utf-8'
PORT = 5050
DISCONNECTED_ADDR = "!disCONNECTED"
SERVER = socket.gethostbyname(socket.gethostname())
print(SERVER)
ADDR = (SERVER, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECTED_ADDR:
connected = False
print(f"[{addr}] {msg}")
conn.send("Msg received".encode(FORMAT))
conn.close()
def main():
print(f"[LISTENING] Server listening on {SERVER}")
server.listen()
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print("STARTED")
print("APP STARTED")
main()
And this is my client.py file
import socket
HEADER = 64
FORMAT = 'utf-8'
PORT = 5050
DISCONNECTED_ADDR = "!disCONNECTED"
SERVER = "#myipaddress"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length)-1)
client.send(send_length)
client.send(message)
print(client.recv(2048).decode(FORMAT))
send("Hello World!")
input()
send("Hello Everyone!")
input()
send("Hello Tim!")
send(DISCONNECTED_ADDR)

Can anyone help me find the bug?
send_length += b' ' * (HEADER - len(send_length)-1)
You miscalculate the padding length of the header in the client. I don't know why you thought the decrement of 1 were needed - it's out of place, correct:
send_length += b' ' * (HEADER - len(send_length))

Related

Sockets Python | chatting server multiple clients

i am trying to implement a TLS secured socket server in python, and i want multiple clients to connect to the server and talk to each other,
what i would like to do is that each client connecting can receive messages sent by other clients also,
Server code:
import socket
import threading
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected = False
print(f"[{addr}] {msg}")
conn.send("Msg received".encode(FORMAT))
conn.close()
def start():
server.listen()
print(f"[LISTENING] Server is listening on {SERVER}")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
print("[STARTING] server is starting ...")
start()
Client Code:
import socket
HEADER = 64
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = "192.168.1.141"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
print(client.recv(2048).decode(FORMAT))
send(input('[Send a Message]: '))
send(DISCONNECT_MESSAGE)
where can i add the TLS in this?:/
if something isn't right please inform me :)
---------------------EDIT------------------------------
SERVER:
import socket
import select
HEADER_LENGTH = 10
IP = "127.0.0.1"
PORT = 1234
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((IP, PORT))
server_socket.listen(5)
sockets_list = [server_socket]
clients = {}
def receive_message(client_socket):
try:
message_header = client_socket.recv(HEADER_LENGTH)
if not len(message_header):
return False
message_length = int(message_header.decode('utf-8').strip())
return {"header": message_header, "data": client_socket.recv(message_length)}
except:
return False
while True:
read_sockets, _, exception_sockets = select.select(
sockets_list, [], sockets_list)
for notified_socket in read_sockets:
if notified_socket == server_socket:
client_socket, client_address = server_socket.accept()
user = receive_message(client_socket)
if user is False:
continue
sockets_list.append(client_socket)
clients[client_socket] = user
print(
f"Accepted new connection from {client_address[0]}:{client_address[1]} username: {user['data'].decode('utf-8')}")
else:
message = receive_message(notified_socket)
if message is False:
print(
f"closed connection from {clients[notified_socket]['data'].decode('utf-8')}")
sockets_list.remove(notified_socket)
del clients[notified_socket]
continue
user = clients[notified_socket]
print(
f"Received message from {user['data'].decode('utf-8')}: {message['data'].decode('utf-8')}")
for client_socket in clients:
if client_socket != notified_socket:
client_socket.send(
user['header'] + user['data'] + message['header'] + message['data'])
for notified_socket in exception_sockets:
sockets_list.remove(notified_socket)
del clients[notified_socket]
CLIENT:
from http import client
import socket
import select
import errno
import sys
import threading
HEADER_LENGTH = 10
IP = '127.0.0.1'
PORT = 1234
my_username = input("Username: ")
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP, PORT))
client_socket.setblocking(False)
username = my_username.encode('utf-8')
username_header = f"{len(username):<{HEADER_LENGTH}}".encode('utf-8')
client_socket.send(username_header + username)
def send_message():
message = input(f"{my_username} > ")
if message:
message = message.encode('utf-8')
message_header = f"{len(message):<{HEADER_LENGTH}}".encode('utf-8')
client_socket.send(message_header + message)
while True:
thread = threading.Thread(target=send_message)
thread.start()
try:
while True:
# receive things
username_header = client_socket.recv(HEADER_LENGTH)
if not len(username_header):
print("connection closed by the server")
sys.exit()
username_length = int(username_header.decode('utf-8').strip())
username = client_socket.recv(username_length).decode('utf-8')
message_header = client_socket.recv(HEADER_LENGTH)
message_length = int(message_header.decode('utf-8').strip())
message = client_socket.recv(message_length).decode('utf-8')
print(f"{username} > {message}")
except IOError as e:
if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK:
print('Reading error', str(e))
sys.exit()
continue
except Exception as e:
print('General error', str(e))
sys.exit()
unfortunately, i cant seem to get the buffer to work right , other clients only receive other clients input when they send messages, so the output is always unordered correctly.

How to connect to Python socket server from another network

I'm new to socket programming and I'm trying to make a server with socket module. When working on a local network it works perfectly fine, but the problem comes when I'm trying to run it and connect from two different networks. I've tried many things that I saw on SO and the internet in general.
Server code:
import socket
import threading
SERVER = socket.gethostbyname('')
PORT = 12345
ADDR = (SERVER, PORT)
HEADER = 64
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = '!DISCONNECT'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f'[NEW CONNECTION] {addr} connected.')
print(f'[ACTIVE CONNECTIONS] {threading.active_count() - 1}')
connected = True
while connected:
msg_lenght = conn.recv(HEADER).decode(FORMAT)
if msg_lenght:
msg_lenght = int(msg_lenght)
msg = conn.recv(msg_lenght).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected = False
print(f'[MESSAGE RECIEVED] FROM: {addr}; Message: {msg}')
conn.send('Message received!'.encode(FORMAT))
conn.close()
def start():
server.listen()
print(f'[LISTENING] Server is listening on {SERVER}')
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f'IP Address: {SERVER}')
print('[STARTING] Server is starting...')
start()
Client code:
import socket
SERVER = input('IP Address: ')
PORT = 5050
ADDR = (SERVER, PORT)
HEADER = 64
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = '!DISCONNECT'
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_lenght = len(message)
send_lenght = str(msg_lenght).encode(FORMAT)
send_lenght += b' ' * (HEADER - len(send_lenght))
client.send(send_lenght)
client.send(message)
while True:
user_Input = input('Message: ')
send(user_Input)
if user_Input == DISCONNECT_MESSAGE:
break
I tried making "rules" on the firewall that should make my port public or something. I have also tried different addresses because I was not sure which one to use.
Thanks in advance!

Things to consider while failproofing socket communciation

I have a simple client/server socket program written in python. So far I think I have managed to deal with two types of certain socket errors.
server is not alive when the client is open (Connectionrejected)
server shuts down while the client is open (Connectionreset)
I am wondering what other errors I should be accounting for. When the client sends a message and the server is closed before the send happens, the client waits until it can reconnect and sends the message however I am not sure if that covers all the cases where the message can be lost or not transmitted completely.
server.py
import socket
import threading
HEADER = 8
PORT = 5050
SERVER = "127.0.0.1" #socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "exit"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(ADDR)
def close():
server.shutdown(socket.SHUT_RDWR)
server.close()
print ("closed")
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected = False
print(f"[{addr}] {msg}")
conn.send("Barcode received".encode(FORMAT))
conn.shutdown(socket.SHUT_RDWR)
conn.close()
def start():
server.listen()
print(f"[LISTENING] Server is listening on {SERVER}")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")
print("[STARTING] server is starting...")
start()
client.py
import socket
import datetime
import time
HEADER = 8
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "exit"
SERVER = "127.0.0.1"
ADDR = (SERVER, PORT)
def check_connection(ADDR):
print("Trying to connect to the server...")
connected = False
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while not connected:
try:
client.connect(ADDR)
except socket.error as error:
connected = False
time.sleep(0.01)
print("Can't connect.Retrying...")
#print(connected)
else:
connected = True
print("Connected to the server")
return client
def send(msg,client,ADDR):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
sending = True
while sending :
try:
cl_ls[0].send(send_length)
cl_ls[0].send(message)
print(cl_ls[0].recv(2048).decode(FORMAT))
sending = False
except socket.error as error:
cl_ls[0] = check_connection(ADDR)
cl_ls[0].send(send_length)
cl_ls[0].send(message)
sending = False
print(cl_ls[0].recv(2048).decode(FORMAT))
cl_ls = ["client"]
client = check_connection(ADDR)
cl_ls[0] = client
accept_more_barcodes = True
while accept_more_barcodes:
input_value = input("Type 'exit' to close input box ")
today = datetime.datetime.now()
date_time = today.strftime("%m/%d/%Y, %H:%M:%S")
input_value += ' *** ' + date_time
if (input_value).startswith("exit"):
accept_more_barcodes = False
else:
with open("barcodes.txt", "a") as f:
f.write(input_value + "\n")
send(input_value,cl_ls[0],ADDR)

how can I send a message to all of my clients connect to my server (socket)

My code is below and I just can't seem to figure out how to send messages to my clients at once
Even when I try asking for help or anything like that they say Store each client and idk how to do that. also I did try it and look it up I did it but I want it to update instantly like discord
import socket
import threading
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!stop"
NAME_MESSAGE = "!name"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(client, addr):
print(f'[INFO] {addr} connected.')
while True:
msg_length = client.recv(HEADER).decode(FORMAT)
if not msg_length:
msg_length = int(msg_length)
msg = client.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
print(f'[DISCONNECT] {addr} Disconnected')
break
elif msg == NAME_MESSAGE:
name_length = client.recv(HEADER).decode(FORMAT)
if name_length:
name_length = int(name_length)
name = client.recv(name_length).decode(FORMAT)
addr = name
else:
<Idk what to put here>
else:
break
def start():
server.listen()
print(f'[INFO] Server is listening on {SERVER}')
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f'\n[INFO] Active connections: {threading.activeCount() - 1}')
print('[INFO] Server is starting...')
start()

How Client send Messages to Each Other

Using Sockets in python i have made a Server-client socket server which just send messages from the clients to the server and I want send messages between clients .
My Code Looks Like this....
Server.py
import socket
import threading
import pyaudio
PORT = 5070
HEADER = 64
FORMAT= "utf-8 "
DISCONNECT_MSG="!DISCONNECTED"
SERVER=socket.gethostbyname(socket.gethostname())
ADDR = (SERVER,PORT)
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(ADDR)
connection_list = []
def handle_clinet(conn,addr):
print(f"[NEW CONNECTION]{addr} Connected")
CONNECTED = True
while CONNECTED:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length=int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MSG:
CONNECTED = False
if msg == "Disconnect":
conn.close()
CONNECTED = False
print("Dissconnected From the User...")
else:
print(msg)
def start():
server.listen()
while True:
conn,addr = server.accept()
thread = threading.Thread(target=handle_clinet,args=(conn,addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.active_count() -1}")
connection_list.append(addr)
print(connection_list)
print("[Starting] the server...")
start()
Client.py
import socket
import threading
import pyaudio
PORT = 5070
HEADER = 64
FORMAT= "utf-8 "
DISCONNECT_MSG="!DISCONNECTED"
SERVER=socket.gethostbyname(socket.gethostname())
ADDR = (SERVER,PORT)
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b" " *(HEADER - len(send_length))
client.send(send_length)
client.send(message)
send("Hello World")
send_more =True
while send_more:
a = input()
send(a)
if a =="Disconnect":
print("!Disconnected From the Server.")
As seen from the above please suggest some Method to send messages between Clients.what changes i have to made in following code

Categories