Connecting a client and server program using .NET framework but different source code
-
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...
-
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...
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:
-
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:
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..
-
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..
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()``);
}
MarkMark Salsbery Microsoft MVP - Visual C++ :java:
-
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()``);
}
MarkMark Salsbery Microsoft MVP - Visual C++ :java:
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.