Python: 套接字 - 运行 来自同一台 PC 的服务器和客户端
Python: socket - running Server and Client from the same PC
嗨,我刚刚转发了我的端口和我的 Python 服务器 <---> 当 运行 来自另一台 PC 的客户端时,客户端聊天按预期工作。
当我尝试从我自己的 PC 连接客户端(服务器文件本身所在的位置)时,我得到了他的错误:
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
Q1:这意味着只有 1 个应用程序可以连接到特定端口,对吗?
Q2:如何在同一台PC上同时开发Server和Client?
(我没有其他电脑可以做)
如果需要,这是我的代码。 (我刚刚开始,所以不要评判)
服务器:
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()
客户:
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()
在两个文件中,只需将 host
设置为 localhost
或 127.0.0.1
,并在两个文件中将 port
设置为相同的端口号。比如说 '6000`
关闭服务器所有打开的连接。有时你不小心离开了服务器运行。如果你想得到你电脑的HOST IP地址,你可以使用host = socket.gethostbyname(socket.gethostname())
。在服务器和客户端上保持相同的端口号。
嗨,我刚刚转发了我的端口和我的 Python 服务器 <---> 当 运行 来自另一台 PC 的客户端时,客户端聊天按预期工作。
当我尝试从我自己的 PC 连接客户端(服务器文件本身所在的位置)时,我得到了他的错误:
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
Q1:这意味着只有 1 个应用程序可以连接到特定端口,对吗?
Q2:如何在同一台PC上同时开发Server和Client? (我没有其他电脑可以做)
如果需要,这是我的代码。 (我刚刚开始,所以不要评判)
服务器:
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()
客户:
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()
在两个文件中,只需将 host
设置为 localhost
或 127.0.0.1
,并在两个文件中将 port
设置为相同的端口号。比如说 '6000`
关闭服务器所有打开的连接。有时你不小心离开了服务器运行。如果你想得到你电脑的HOST IP地址,你可以使用host = socket.gethostbyname(socket.gethostname())
。在服务器和客户端上保持相同的端口号。