0

i'm tring to test some online game with socket, and i'm getting error in the line of s.bind((my ip, the port)) when i use the port (5555), but when i use a dهfferent port (4444) the error does not appear, even thouth the same (5555) port was working at first, so, is there any way to fix the error of the port (5555) or i just have to use another port? thanks, and this is my server code(it's not completed yet).

import socket as sct
import _thread as trd
import sys
from colorama import init, Fore
init(autoreset=True)

server="192.168.43.222"
port=5555
s= sct.socket(sct.AF_INET, sct.SOCK_STREAM)

def info(color:str, title: str, msg:str =None): 
    if color=='red':
        f=Fore.RED
    elif color=='blue':
        f=Fore.BLUE
    elif color=='green':
        f=Fore.GREEN
    elif color=='cyan':
        f=Fore.CYAN
    elif color=='magenta':
        f=Fore.MAGENTA
    elif color=='yellow':
        f=Fore.YELLOW
    print("[", end="")
    print(f+title, end="")
    if len(title)!=13:
        d=13-int(len(title))
        for l in range(d):
            print(" ", end="")
    if msg!=None:end=''
    else:end=None
    print('] ', end=end)
    if msg!=None:
        print(msg)

info('magenta', "START SERVER", server)
    
try:
    s.bind((server, port)) #error is excepted hear
    connectable=True
except sct.error as e:
    connectable=False
    info('red', "ERROR", str(e))
if connectable:
    try:
        s.listen(2)
    except Exception as e:
        info('red', "NO CONNECTION", f"Server can not get started, please check your connection: {e}")
    info('magenta', "LESTENING", "Wating for a connection")

def trdc(conn):
    reply=""
    while True:
        try:
            data=conn.recv(2038)
            reply=data.decod('utf-8')
            if not data:
                info('y', f"DISCONNECTION")
            else:
                info('b', 'RECEIVED', reply)
                info('c', 'SENDING', reply)
            conn.sendall(str.encode(reply))
        except:
            break
    info('red', "DISCONNECTION", f"{addr[0]} is no more connected!")
    conn.close()

while True:
    try:
        conn ,addr=s.accept()
        info('green', "NEW CONNECTION" ,f"Connected to: {addr[0]}")
        trd.start_new_thread(trdc, (conn,)) 
    except:pass
LORD_M.D
  • 11
  • 1
  • Maybe the port 5555 is already in use. Try netstat in the command line in order to check if there is some "listening" on this port 5555. – Paulo Jun 25 '22 at 10:48
  • 2
    Could please you post the error message? – Paulo Jun 25 '22 at 10:49

0 Answers0