简体   繁体   English

C#-服务器/ C ++客户端:Socket.Accept()永远不会完成

[英]C#-Server / C++ Client: Socket.Accept() never finishes

I'm writing a simple C++ client that should connect to a C# server. 我正在编写一个应该连接到C#服务器的简单C ++客户端。 And, while the client runs throught fine and even states it sent a bunch of bites, the C# server never gets past listener.Acceppt() : 而且,尽管客户端运行得很好,甚至说它发送了很多消息,但C#服务器永远不会越过listener.Acceppt()

Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();

I took the server source from here: Microsoft MSDN: Synchronous Server Socket Example 我从此处获取服务器源: Microsoft MSDN:同步服务器套接字示例

The client code I use can be found here: Microsoft MSDN: Complete Winsock Client Code 我使用的客户端代码可以在这里找到: Microsoft MSDN:完整的Winsock客户端代码

I also checked to server with the according C# client , which worked fine. 我还使用相应的C#客户端检查了服务器,效果很好。 I also checked the return values of the client, which also looked pretty plausible. 我还检查了客户端的返回值,看起来也很合理。 So I'd guess the problem lies somewhere in how C++ / C# handle the details. 因此,我想问题可能出在C ++ / C#如何处理细节上。

I tried Wireshark to find some hints, but mysteriously there was absolutely no trace of any communication on the used port (11000). 我尝试了Wireshark来找到一些提示,但神秘地发现在使用的端口(11000)上绝对没有任何通信的痕迹。

Where should I start looking to solve this issue? 我应该从哪里着手解决这个问题?

Update: All communication at the moment runs just locally. 更新:目前所有通信仅在本地运行。 I tried my IPAddress (from ipconfig ) and 127.0.0.1 for both server and client. 我为服务器和客户端尝试了我的IPAddress(来自ipconfig )和127.0.0.1

I just have one network adapter installed, I use that IP adress. 我只安装了一个网络适配器,我使用的是IP地址。 The client checks the getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result) and returns valid information (eg TCP as protocol). 客户端检查getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result)并返回有效信息(例如,TCP作为协议)。 connect() returns 0 , which should be ok. connect()返回0 ,应该可以。

Update 2: I tried different combinations of C++/C# Server-Client-Setups: 更新2:我尝试了C ++ / C#Server-Client-Setups的不同组合:

  • C++-Server, C++ Client: Works C ++服务器,C ++客户端:有效
  • C++-Server, C# Client: Works C ++-Server,C#客户端:有效
  • C#-Server, C# Client: Works C#服务器,C#客户端:有效
  • C#-Server, C++ Client: Does not work C#服务器,C ++客户端:不起作用
  • C#-Server, putty : Works C#-Server, 腻子 :有效

Update 3: I tried Wireshark on the other Server-Client constellations, but neither of them did show any traffic on tcp.port == 11000 , although they did work (see Update 2). 更新3:我在其他服务器-客户端星座上尝试了Wireshark,但是它们都没有显示tcp.port == 11000上的任何流量,尽管它们确实起作用(请参阅更新2)。 It looks like Wireshark does not show any results, because everything is just local (see Wireshark localhost traffic capture ) 看起来Wireshark没有显示任何结果,因为所有内容都只是本地的(请参阅Wireshark本地主机流量捕获

I had the same problem. 我有同样的问题。 The connection with winsock is asynchronous. 与winsock的连接是异步的。 And the example in C# that you are using is synchronous. C#中使用的示例是同步的。 You have to use a Asynchronous example to get communication with your C++ code. 您必须使用一个异步示例来与C ++代码进行通信。

I used this two examples! 我用这两个例子!

C Sharp server https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx C Sharp服务器https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx

C plus plus client https://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx C plus加客户端https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms737591(v=vs.85).aspx

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

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