Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Connecting a client and server program using .NET framework but different source code

Connecting a client and server program using .NET framework but different source code

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpc++sysadminhelpdotnet
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kogee San
    wrote on last edited by
    #1

    Hello. Im doing a client server program where the client program is created using Visual C++.NET meanwhile the server program is created using C#.NET. Just for my own knowledge, can both of these program be connected via a network using TCP protocol. I tried to execute the sample. there was no error but it could not be connected. Which part that must i edit? Is it ok if i only edit the client part (created using C++). This is because the server part also have multithreading mechanism, so i dont want to mess that up. For your information, i tried to connect both client and server using port 5000. I will also include here the connection part of the client and server source code. Did i miss anything in the source code in the code snippet attachment? Thank you and your help is very appreciated. /****** The client program. created using C++.NET ********************/ InitWSA(); sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { printf("Error no %d occured when creating socket\n", errno); exit(-1); } myaddr.sin_family = AF_INET; myaddr.sin_port = 5001; myaddr.sin_addr.s_addr = INADDR_ANY; status = bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr)); if(status < 0) { printf("Error no %d occured when binding\n", errno); exit(-1); } receiver.sin_family = AF_INET; receiver.sin_port = 5000; receiver.sin_addr.s_addr = inet_addr("127.0.0.1"); status = connect(sock, (struct sockaddr *)&receiver, sizeof(struct sockaddr)); if (status < 0) { printf("Cannot connect to server, error no %d occured\n", errno); //here is where the message is //displayed where it could not connect to the server. exit(-1); } /********** end source code **************/ /********** the server part. created using C# ***********/ string portStr = textBoxPort.Text; //set the port using port 5000 int port = System.Convert.ToInt32(portStr); // Create the listening socket... m_mainSocket = new Socke(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port); // Bind to local IP Address... m_mainSocket.Bind( ipLocal ); // Start listening... m_mainSocket.Listen(4); // Create the call back for any client connections...

    M 1 Reply Last reply
    0
    • K Kogee San

      Hello. Im doing a client server program where the client program is created using Visual C++.NET meanwhile the server program is created using C#.NET. Just for my own knowledge, can both of these program be connected via a network using TCP protocol. I tried to execute the sample. there was no error but it could not be connected. Which part that must i edit? Is it ok if i only edit the client part (created using C++). This is because the server part also have multithreading mechanism, so i dont want to mess that up. For your information, i tried to connect both client and server using port 5000. I will also include here the connection part of the client and server source code. Did i miss anything in the source code in the code snippet attachment? Thank you and your help is very appreciated. /****** The client program. created using C++.NET ********************/ InitWSA(); sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { printf("Error no %d occured when creating socket\n", errno); exit(-1); } myaddr.sin_family = AF_INET; myaddr.sin_port = 5001; myaddr.sin_addr.s_addr = INADDR_ANY; status = bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr)); if(status < 0) { printf("Error no %d occured when binding\n", errno); exit(-1); } receiver.sin_family = AF_INET; receiver.sin_port = 5000; receiver.sin_addr.s_addr = inet_addr("127.0.0.1"); status = connect(sock, (struct sockaddr *)&receiver, sizeof(struct sockaddr)); if (status < 0) { printf("Cannot connect to server, error no %d occured\n", errno); //here is where the message is //displayed where it could not connect to the server. exit(-1); } /********** end source code **************/ /********** the server part. created using C# ***********/ string portStr = textBoxPort.Text; //set the port using port 5000 int port = System.Convert.ToInt32(portStr); // Create the listening socket... m_mainSocket = new Socke(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port); // Bind to local IP Address... m_mainSocket.Bind( ipLocal ); // Start listening... m_mainSocket.Listen(4); // Create the call back for any client connections...

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Maybe try NOT binding the client socket - let the protocol pick the socket name. You should be able to debug this easily.  What calls are failing? On the client, I don't see any code that checks error codes returned from failed socket API calls Shouldn't you be using WSAGetLastError instead of errno? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      K 1 Reply Last reply
      0
      • M Mark Salsbery

        Maybe try NOT binding the client socket - let the protocol pick the socket name. You should be able to debug this easily.  What calls are failing? On the client, I don't see any code that checks error codes returned from failed socket API calls Shouldn't you be using WSAGetLastError instead of errno? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        K Offline
        K Offline
        Kogee San
        wrote on last edited by
        #3

        hello and thanks for the reply I tried the method you said. So I disable the binding socket. Still, the client could not connect to the server. Actually, there is no error in the code but it just could not connect the client which is programmed in C meanwhile the server side is programmed using C#. So i think it is not about the errno or the WSAGetLastError problem. Actually, this is just only a test program. After i proved that it can connect, then I will produce a real version. I just want to know is there any method or code which can connect a client programmed in C to a server which is programmed in C#. I assume that the connect() call failed since the printf("Cannot connect to server, error no %d occured\n", errno); as in the code snippet will be displayed. So this clearly means that it just could not connect to the server. Anyway, thank for your reply and help. Any other idea's or solution? hehe..

        M 1 Reply Last reply
        0
        • K Kogee San

          hello and thanks for the reply I tried the method you said. So I disable the binding socket. Still, the client could not connect to the server. Actually, there is no error in the code but it just could not connect the client which is programmed in C meanwhile the server side is programmed using C#. So i think it is not about the errno or the WSAGetLastError problem. Actually, this is just only a test program. After i proved that it can connect, then I will produce a real version. I just want to know is there any method or code which can connect a client programmed in C to a server which is programmed in C#. I assume that the connect() call failed since the printf("Cannot connect to server, error no %d occured\n", errno); as in the code snippet will be displayed. So this clearly means that it just could not connect to the server. Anyway, thank for your reply and help. Any other idea's or solution? hehe..

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Kogee San wrote:

          I just want to know is there any method or code which can connect a client programmed in C to a server which is programmed in C#

          TCP/IP is TCP/IP, no matter what platform it runs on or what language the code is written in. Why do you dismiss error codes?  They can give you important clues to reasons for failure. That's why they are there.  Why not use the clues the system offers? Otherwise all you can do is guess. Using the socket APIs properly will help... First you need the port number in network byte order: receiver.sin_port = htons(5000); Then you need to properly check for errors: status = connect(sock, (struct sockaddr *)&receiver, sizeof(struct sockaddr)); if (SOCKET_ERROR == status) {     printf("Cannot connect to server, error no %d occured\n", ::WSAGetLastError()``); } Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          K 1 Reply Last reply
          0
          • M Mark Salsbery

            Kogee San wrote:

            I just want to know is there any method or code which can connect a client programmed in C to a server which is programmed in C#

            TCP/IP is TCP/IP, no matter what platform it runs on or what language the code is written in. Why do you dismiss error codes?  They can give you important clues to reasons for failure. That's why they are there.  Why not use the clues the system offers? Otherwise all you can do is guess. Using the socket APIs properly will help... First you need the port number in network byte order: receiver.sin_port = htons(5000); Then you need to properly check for errors: status = connect(sock, (struct sockaddr *)&receiver, sizeof(struct sockaddr)); if (SOCKET_ERROR == status) {     printf("Cannot connect to server, error no %d occured\n", ::WSAGetLastError()``); } Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            K Offline
            K Offline
            Kogee San
            wrote on last edited by
            #5

            hello mark Thanks for the reply. I just add the byte order method htons and it works. Thanks for the solution. I really2 appreciated it. Also thanks for the tips to take error messages seriously. I will take your advice. I know error messages is important for debugging, but i just consider a simple connection from client/server, thats why i dont want to concern the error messages. I will be careful next time. Thanks again.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups