Python: socket - run server and client from the same pc

Hy, I just forwarded my ports and my Python Server client chat <---> works as expected when starting the client from another PC.

When I try to connect a client from my own computer (where the server file itself is located), I get his error:

OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

      

Q1: This means only 1 app can connect to a specific port, right?

Q2: How can I develop both my server and my client on the same PC? (I have no other PCs for this)

If needed, here is my code. (I just started so NOT JUDGE)

SERVER:

from tkinter import *
#from mysql.connector import (connection)
import socket
from _thread import *

import sys
root = Tk()
T = Text(root, height=2, width=30)
T2 = Text(root, height=2, width=30)
B = Button(root, text="Send")
T.pack(side=LEFT,fill=X)
T2.pack(side=TOP,fill=X)
B.pack(side=LEFT,fill=X)




#statick ip
host = 'x.x.x.x'
port=yyyy
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))

try:
    s.bind((host,port))
except socket.error as e:
    print(str(e))

s.listen(5)
print("waiting for connection")
def threaded_client(conn):
    conn.send(str.encode("Connection with the server established\n"))

while True:
    data = conn.recv(2048)
    reply = "You: " + data.decode('utf-8')
    if not data:
        break
    conn.sendall(str.encode(reply))
conn.close()

while True:
    conn, addr = s.accept()
    print('connected to: '+ addr[0]+':'+str(addr[1]))
    start_new_thread(threaded_client,(conn,))
root.mainloop()

      

CUSTOMER:

from tkinter import *
import socket

print("everything is imported")

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print("socket is established")
#the public ip
host = 'y.y.y.y'
port=xxxx
s.connect((host,port))

print("s.connect done")

def sendShit(event):
    textToSend = T.get("1.0",END)
    s.send(str.encode(textToSend))
    T2.insert(END, s.recv(1024))

print("sendshit defined")

root = Tk()
T = Text(root, height=2, width=30)
T2 = Text(root, height=2, width=30)
B = Button(root, text="Send")
T.pack(side=LEFT,fill=X)
T2.pack(side=TOP,fill=X)
B.pack(side=LEFT,fill=X)
T.insert(END, "Type here")
T2.insert(END, s.recv(1024))
B.bind("<Button-1>",sendShit)
mainloop()

      

+3


source to share


2 answers


In both files, just set host

to localhost

or 127.0.0.1

, and also set port

to the same port number in both files. for example, "6000`



+4


source


Close all open connections for the server. Sometimes you accidentally leave the server. If you want to get your HOST IP address of your computer, you can use host = socket.gethostbyname(socket.gethostname())

. Keep the port number the same on both the server and client.



0


source







All Articles