3

Hy, i have just forwarded my ports and my Python Server <---> Client chat works as expected when running the client from a different PC.

When I try to connect the client from my own PC (where lies the server file itself) then i get his error:

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

Q1: This means that 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, then? (I don't have any other PC to do it on)

If needed, here is my code. (i have just started, so DON'T 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()

THE CLIENT:

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()
2
  • Just set on both files host = '127.0.0.1'(which is your localhost and set port to same number (for example 6000)
    – Iron Fist
    Jun 26, 2015 at 20:05
  • I've posted as answer...Please consider accepting it if you see it helped you. Thanks
    – Iron Fist
    Jun 26, 2015 at 20:25

2 Answers 2

4

In both files, just set host to localhost or 127.0.0.1 and also set port to same port number in both files. say for example '6000`

0

Close all the open connections for the server. Sometimes you accidentally leave a server running. If you want to get your HOST IP address of your computer, you can use host = socket.gethostbyname(socket.gethostname()). Keep port number same on both server and client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.