[英]C# client Python server : Connection refused
I'm working on a basic socket communication between C# (client) and Python (server), and I don't understand the reason why I've this error from the client: 我正在C#(客户端)和Python(服务器)之间进行基本的套接字通信,但我不明白为什么客户端会出现此错误:
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Sockets.SocketException: Connection refused at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00159] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System/System.Net.Sockets/Socket_2_1.cs:1262 at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System/System.Net.Sockets/TcpClient.cs:284 at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x000b3] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System/System.Net.Sockets/TcpClient.cs:355 [错误]致命异常:System.Net.Sockets.SocketException:在/ private / tmp / monobuild / build / BUILD /中的System.Net.Sockets.Socket.Connect(System.Net.EndPoint remoteEP)[0x00159]拒绝连接/ private / tmp /中的System.Net.Sockets.TcpClient.Connect上的mono-2.10.9 / mcs / class / System / System.Net.Sockets / Socket_2_1.cs:1262(System.Net.IPEndPoint remote_end_point)[0x00000] monobuild / build / BUILD / mono-2.10.9 / mcs / class / System / System / System.Net.Sockets / TcpClient.cs:284(位于System.Net.Sockets.TcpClient.Connect(System.Net.IPAddress [] ipAddresses,Int32端口)/private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System/System.Net.Sockets/TcpClient.cs:355中的[0x000b3]
My programs are really short and easy so I suppose it's a noob question, but I just don't get it. 我的程序确实简短易用,所以我认为这是一个菜鸟问题,但我不明白。 All I want is a Client sending a message to the server which will print it on the console.
我只需要一个客户端向服务器发送消息,该消息将在控制台上打印出来。
Here is the C# client (the error comes from :socket.Connect("localhost",9999);) 这是C#客户端(错误来自:socket.Connect(“ localhost”,9999);)
using System;
using System.Net.Sockets;
namespace MyClient
{
class Client_Socket{
public void Publish(){
TcpClient socket = new TcpClient();
socket.Connect("localhost",9999);
NetworkStream network = socket.GetStream();
System.IO.StreamWriter streamWriter= new System.IO.StreamWriter(network);
streamWriter.WriteLine("MESSAGER HARGONIEN");
streamWriter.Flush();
network.Close();
}
}
}
And the Python Server: 和Python服务器:
from socket import *
if __name__ == "__main__":
while(1):
PySocket = socket (AF_INET,SOCK_DGRAM)
PySocket.bind (('localhost',9999))
Donnee, Client = PySocket.recvfrom (1024)
print(Donnee)
Thx for your help. 谢谢您的帮助。
You've got two problems. 你有两个问题。 The first is that you're binding to
localhost
. 首先是您要绑定到
localhost
。 You probably want to bind to 0.0.0.0
if you want other computers to be able to connect: 如果希望其他计算机能够连接,则可能要绑定到
0.0.0.0
:
PySocket.bind (('0.0.0.0',9999))
The other problem is you're serving with UDP and trying to connect with TCP. 另一个问题是您正在使用UDP并尝试与TCP连接。 If you want to use UDP, you could use
UdpClient
rather than TcpClient
. 如果要使用UDP,则可以使用
UdpClient
而不是TcpClient
。 If you want to use TCP, you'll have to use SOCK_STREAM
rather than SOCK_DGRAM
and use listen
, accept
, and recv
rather than recvfrom
. 如果要使用TCP,则必须使用
SOCK_STREAM
而不是SOCK_DGRAM
并使用listen
, accept
和recv
而不是recvfrom
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.