Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between c++ and c# application through network

Tags:

c++

c#

tcp

sockets

ip

I have a simple server written in c# listening on some port. I have an application in c++ and I need the application to send some information to the server. This information is a struct containing 5 integers. I was thinking that I can send it also as a string: something like: "ID=3, anotherInt=5...". Is it a good idea? If not, how should I do that?

How to make it work? What is your advice?

Thank you.

like image 840
DaveDum Avatar asked Jan 06 '13 17:01

DaveDum


3 Answers

I think you have a mistake in your code.

char *ln = "String to send";
connect(client_socket, (struct sockaddr *)&clientService, sizeof(clientService));  
send(client_socket,(const char*)&ln, sizeof(ln), 0);

The prototype for send function is:

ssize_t send(int socket, const void *message, size_t length, int flags);

ln is already a pointer to your char buffer. You are passing in &ln, which is the address of the pointer. Shouldn't it be just "ln"?

like image 179
OldProgrammer Avatar answered Nov 11 '22 15:11

OldProgrammer


You should fix the send() method in client code. sizeof() is wrong way to find the length of string, casts applied on "ln" aren't quite right for what you need there. Check <<this link>> for an example and see how it works for you. BTW, C# code in the server needs some serious re-writing if it were to work predictably. You are using 4096 byte buffer and calls to Read() aren't guaranteed to fetch the entire transmission in one go. You will need a loop just for Read to make sure you are reading everything you need - ofcourse, this needs a clear definition of communication semantics. Happy networking!

like image 31
Bobby Avatar answered Nov 11 '22 15:11

Bobby


First of all, (const char*)&ln is not correct. ln is a char *, so when you take the address of it using & you are getting a char **, which you are then casting to a char *. This is undefined behavior. You'll want to get rid of the & operator. Also you'll probably want to read up on what pointers are and how to use them.

As a rule of thumb, don't cast willy-nilly to make the compiler shut up. The errors are trying to tell you something. However, the sockaddr and sockaddr_in thing is correct; the api is designed that way. If you turn on -Wall in your compiler options, it should give you a warning in the correct place.

ALSO: you want strlen(ln) and not sizeof.

Quick n Dirty Rundown on Pointers

When a type includes * before the variable name, the variable holds a pointer to a value of that type. A pointer is much like a reference in C#, it is a value holding the location of some data. These are commonly passed into functions when a function wants to look at a piece of data that the caller owns, sometimes because it wants to modify it. A string is represented as a char *, which is a pointer to the first character in the string. The two operators that are related to pointers are & and *. & takes an lvalue and returns a pointer to that value. * takes a pointer and returns the value it points to. In this case, you have a string, as a char *, and the function you're calling wants a char *, so you can pass it in directly without casting or using * or &. However, for the other function you have a struct sockaddr_in and it wants a struct sockaddr *, so you (correctly) use & to get a struct sockaddr_in *, then cast it to a struct sockaddr *. This is called "type punning," and is an unpleasant reality of the API. Google will give you a better description of type punning than I can.

like image 2
Dan Avatar answered Nov 11 '22 13:11

Dan