I have a stream server application written in C, that I need to incorporate in a GUI C++ managed new program, but I am having trouble finding the equivalent of the C sockets function calls.

The C apps initialize this way:

   *  Init socket
   if ( 0 > (sock_lci = socket(AF_INET, SOCK_STREAM, 0)) )

  int len, on;
  on = 1;
  setsockopt(sock_lci, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
  ser_lci.sin_family = AF_INET;
  ser_lci.sin_addr.s_addr = INADDR_ANY;
  ser_lci.sin_port = htons(PORT);
  len = sizeof(ser_lci);
  if ( 0 > bind(sock_lci, (struct sockaddr *)&ser_lci, len) )
   { exit(-1)

  if ( -1 == listen(sock_lci, 1) )

Then, the main loops does something like:

fd_set ready;
   int sockfd_in, nbin, nb;
   char *bb, buff[MAX_LEN_MESS];
   FD_SET(sock_lci, &ready);
   if ( 0 > (nb = select(sock_lci+1, &ready, (fd_set *)NULL, (fd_set *)NULL, &to)) )
    { exit(-1);
   if(nb == 0) return;
   if ( FD_ISSET(sock_lci, &ready) )
     {  if ( 0> (sockfd_in = accept(sock_lci, (struct sockaddr *)NULL,
                                (int *)NULL)) )
         { perror("rr_lci");
        bb = (char *)buff;
        ioctlsocket(sockfd_in, FIONREAD, &nbin);
        if ( nbin != 0 && nbin <= MAX_LEN_MESS)
         {  if ( 0 >= (nb = recv(sockfd_in, bb, nbin, 0)) )
             { exit(-1);
       /* Answer */
       if ( -1 == send(sockfd_in, "Answer", 7, 0))

So, basically, the app listens on the port PORT, from any possible host, gets the string sent and replies.

I guess that I have to use now the Socket Class, but I can't find an example that reproduces my code, including how to define what function will be triggered from the main loop when a message is ready to be read.

Any help would be much appreciated.

6 Years
Discussion Span
Last Post by BobFX

You will probably have to do things a lot differently when porting from C to CLR/C++. Microsoft has made a lot of things much easier to do. Here is a tutorial for C#, which is a very close relative of CLR/C++.


Thanks, this tutorial will give some pointers indeed.

I'll need a few days probably to go around it, I'm not familiar with C# and just a beginner with managed C++.

I'll post again if I am stuck, or if I succeed.


Ancient Dragon,

I made a quick test with the TcpListener class:

IPAddress^ ipAddress = IPAddress::Parse( "" );
 TcpListener^ server;
    server = gcnew TcpListener(ipAddress, PORT_lock);
    // Start listening for client requests.
    // Buffer for reading data
    array<Byte>^bytes = gcnew array<Byte>(256);
    String^ data = nullptr;
    // Enter the listening loop.
    while ( true )
       Console::Write( "Waiting for a connection... " );
       // Perform a blocking call to accept requests.
       // You could also user server.AcceptSocket() here.
       TcpClient^ client = server->AcceptTcpClient();

But this is not compatible with the client side of the existing application.

It does not answer to a request made with calls similar to the ones I described in my first post, so I can't use it. Unfortunately, the client that runs on non Windows systems cannot be changed, so I have to be able to make a compatible server.


I'm not familiar with C#

All you have to remember is that C# used '.' for both namespaces (which would use '::' in C++) and member variables/methods (which would use either '.' or -> to access a class/struct depending on whether the variable is a pointer or not).

I agree with AD, if you're using C++/CLI, use the available methods in .NET.


Edit: if I change "" to the actual IP address, I do receive a connection from the client.

I have more tries to do, but you pointed me in th right direction, thanks.



I am marking this thread as solved, thanks for your help.

I may have some specific problems with the TcpListener class, but if that happens, I'll open a new thread.


Thanks for the pointers.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.