因为我无法解决这个问题,所以我想发布它。我的设置:
我如何创建套接字:
HOST = '0.0.0.0'
PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(False)
# Die socket Adresse bei Windows setzt sich aus dem Host und der IP zusammen.
sockAddress = (HOST, PORT)
# Bindet den Socket an die gewünschte Adresse
sock.bind(sockAddress)
我的侦听器线程看起来像这样(简短版本):
while(globals.stopListener == False):
try:
dataPackage, address = sock.recvfrom(1024)
except socket.error as e:
if e.errno == 10035:
t.sleep(0.1)
continue
except Exception as e:
print >>sys.stderr, "Unknown Error !"
print >>sys.stderr, e
break
if dataPackage:
print >>sys.stderr, "Packet erhalten: " + dataPackage
header = int(dataPackage[0])
time = dataPackage[1:6+1]
package = dataPackage[7:]
if header == 0:
# -------------------- LOGIN --------------------------
isValid = Login.validateLogin(dataPackage)
if isValid:
sock.sendto("\nWilkommen auf dem Server "+name, address)
globals.players.append(address)
print >>sys.stderr, "Player: " + len(globals.players)
else:
print >>sys.stderr, "Sended wrong PW or NAME"
sock.sendto("Falsche Login Daten!", address)#, address)
sendto产生错误: ConnectionError :('连接已中止。',错误(10061,' Es konnte keine Verbindung hergestellt werden,da der Zielcomputer die Verbindung verweigerte')
我已经尝试过了:
套接字连接:
public class SocketManager : MonoBehaviour
{
private byte[] _recData = new byte[1024];
private bool _stopListen = false;
private const string HOST = "XXX.XXX.XXX.XXX"; <-- my local ip 192....
private const int PORT = 8888;
private Socket _sock;
public static SocketManager socketHandler { get; private set; }
public IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(SocketManager.HOST), SocketManager.PORT);
public EndPoint rec_endpoint = new IPEndPoint(IPAddress.Any, 0);
this._sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this._sock.Connect(endpoint);
this._sock.Blocking = false;
监听器的简短版本(也是一个新线程):
public void startListen()
{
byte[] recData = new byte[1024];
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 8888);
EndPoint senderRemote = (EndPoint)sender;
while (!this._stop)
{
try
{
this.sock.ReceiveFrom(recData, ref senderRemote);//, ref endpoint);.......
我希望我的风格可读。
此致
Sens4
Edit1 :我在python中快速编写了客户端...并发生了同样的错误。 -.-&#34;
Edit2 :现在我已经使用netstat进行了检查,如果客户端的端口正在侦听并且它不是。但我认为他应该听:O
netstat -a -p UDP (shortended)
Aktive Verbindungen (active connections)
Proto Lokale Adresse Remoteadresse Status
UDP 0.0.0.0:8888 *:* <---- my server
我认为我的客户也应该出现吗?
我已使用sock.getsockname()
收到了客户端的当前端口和IP,但未显示。