0

I'm writing a simple C++ client that should connect to a C# server. And, while the client runs throught fine and even states it sent a bunch of bites, the C# server never gets past 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

The client code I use can be found here: Microsoft MSDN: Complete Winsock Client Code

I also checked to server with the according C# client, which worked fine. 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.

I tried Wireshark to find some hints, but mysteriously there was absolutely no trace of any communication on the used port (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.

I just have one network adapter installed, I use that IP adress. The client checks the getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result) and returns valid information (e.g. TCP as protocol). connect() returns 0, which should be ok.

Update 2: I tried different combinations of C++/C# Server-Client-Setups:

  • C++-Server, C++ Client: Works
  • C++-Server, C# Client: Works
  • C#-Server, C# Client: Works
  • C#-Server, C++ Client: Does not work
  • C#-Server, putty: Works

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). It looks like Wireshark does not show any results, because everything is just local (see Wireshark localhost traffic capture)

Community
  • 1
  • 1
DIF
  • 2,470
  • 6
  • 35
  • 49
  • Wh... what? Just to be sure - you are saying that the client connected OK and sent data, but the server accept() never returned? – Martin James Jul 17 '15 at 11:03
  • ' tried Wireshark to find some hints, but mysteriously there was absolutely no trace of any communication on the used port (11000)' - check your IP addresses... you are not communicating with the server you think you are. – Martin James Jul 17 '15 at 11:05
  • I just have one network adapter installed, I use that IP adress. The client checks the `getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result)` and returns valid information (e.g. TCP as protocol). `connect()` returns `0`, which should be ok. – DIF Jul 17 '15 at 11:26
  • I cannot think of any way that a connect could succeed at the client and accept() not return at the server IF the client is indeed connecting to the server you think it is. – Martin James Jul 17 '15 at 11:36
  • I tried changing small things, like changing the port on the server, but not the client: the connect fails. Changing the client port to the same value as the server: connect succeeds... – DIF Jul 17 '15 at 11:42
  • If your client can actually connect to the proper IP address and port, it means that something else must be listening. Maybe you didn't shutdown a different instance of the server properly? Also, note that the C# server is specifying IPv4, while the client doesn't - and the C# server only ever listens on the first IP address it finds. It might be a better idea to bind to `IPAddress.Any`. – Luaan Jul 20 '15 at 09:02
  • Thanks! Binding to `IPAddress.Any` and changing to `hints.ai_family = AF_INET` did not help... – DIF Jul 20 '15 at 09:26

1 Answers1

2

I had the same problem. The connection with winsock is asynchronous. And the example in C# that you are using is synchronous. You have to use a Asynchronous example to get communication with your C++ code.

I used this two examples!

C Sharp server 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

Monica Cid
  • 36
  • 4
  • 1
    Just provide some basic steps to make sure you are giving a good answer. – Brandon Zamudio May 10 '17 at 20:45
  • @Monica Cid Can you please tell me in details. I am trying to do the same. I copied the whole code from https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx llink. But, I think I am not getting the desired result. What changes should I make? Please suggest me. – Virtuall.Kingg Nov 15 '21 at 07:32