简体   繁体   English

将C#客户端连接到C ++服务器

[英]Connect C# client to C++ server

I just can't get it to work, I'm frustrated. 我很沮丧,我无法正常工作。

My C++ code ( _details is addrinfo* and _socket is SOCKET ): 我的C ++代码( _detailsaddrinfo*_socketSOCKET ):

struct addrinfo hints;
_socket = socket(AF_INET, SOCK_STREAM, 0);
if (_socket < 0)
    throw "Invalid socket";
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;


if (getaddrinfo(NULL, Port.c_str(), &hints, &_details) != 0)
{
    throw "GetAddrInfo failed";
}

if ((bind(_socket, _details->ai_addr, this->_details->ai_addrlen)) != 0)
{
    throw "Bind Unsuccessful";
}

freeaddrinfo(this->_details);
if (listen(this->_socket, SOMAXCONN) != 0)
        throw "Listen Unsuccessful";
client = accept(this->_socket, (struct sockaddr*)&client_addr, &addrlen);

A simple C++ server. 一个简单的C ++服务器。 C++ and Python clients work perfectly with the above server, and doing everything as expected. C ++和Python客户端可以与上述服务器完美配合,并按预期完成所有操作。

Then comes the client: 然后是客户:

        Socket s;

        IPAddress ip = Dns.GetHostEntry("127.0.0.1").AddressList[0];
        int port = 7777;
        IPEndPoint remoteEP = new IPEndPoint(ip, port);

        s = new Socket(AddressFamily.InterNetworkV6,
            SocketType.Stream, ProtocolType.Tcp);

        bool isConnected = false;
        while (!isConnected)
        {
            try
            {
                s.Connect(remoteEP);
                isConnected = true;
            }
            catch (Exception e1)
            {
                System.Threading.Thread.Sleep(2000);
            }
        }

The C# client works perfectly with a C# server. C#客户端与C#服务器完美配合。

Each of these work, together - they just don't. 所有这些都一起工作-他们只是没有。 I try to connect to the server but the client always ends up throwing System.Net.Sockets SocketException with the error code of 10061 , claiming that the C++ server rejects it. 我尝试连接到服务器,但是客户端始终最终抛出System.Net.Sockets SocketException ,错误代码为10061 ,声称C ++服务器拒绝了它。

One thread on SO asked a similar question, receiving the answer that he should sleep after every connection request and then try again (as you can see, done already). SO上的一个线程问了一个类似的问题,得到的答案是,每个连接请求后他都应该睡觉,然后再试一次(如您所见,已经完成)。

Another thread, on another site, claimed that the problem is in the server and that he converted the C++ server parameters from host byte order to network byte order: 另一个站点上的另一个线程声称问题出在服务器上,并且他将C ++服务器参数从主机字节顺序转换为网络字节顺序:

listenSocketAddress.sin_port = htons((u_short)port);
listenSocketAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

but I use addrinfo which doesn't contain the same variables as sockaddr_in . 但是我使用的addrinfo不包含与sockaddr_in相同的变量。

How can I connect those two? 如何连接这两个?

As far as I can tell, your C++ side is using IPv4 ( AF_INET ), while your C# side is using IPv6 ( AddressFamily.InterNetworkV6 ). 据我所知,您的C ++ AF_INET在使用IPv4( AF_INET ),而您的C# AddressFamily.InterNetworkV6在使用IPv6( AddressFamily.InterNetworkV6 )。 Either change the server to use IPv6, or change the client to use IPv4. 更改服务器以使用IPv6,或更改客户端以使用IPv4。 Or ideally, let them use whichever works best, and use hostnames instead of IP addresses. 或者理想情况下,让他们使用最有效的方式,并使用主机名代替IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM