I’m currently learning the network programming with C/C++… I have experience .NET but not in C (esp: in Unix network programming)
The problem that I’m facing right now is that when I receive the message, I like to split it into two parts. The format of message will be like [command] filepath (For example: get a.txt or put a.txt)
I’m using recvform to receive the message.
int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
struct sockaddr *from, int *fromlen);
The problem is that I dont know how to convert “void *buff” into string so that I can do String.Split(” “) to get the command and file name.
Could anyone please explain me a way to convert “void *buff” to string?
You actually pass the pointer to the start of a character array as the buffer. The
recvfrom()system call writes into that array. You then process the array as a character string – after ensuring that it is properly null terminated (whichrecvfrom()will not do). Of course, the sending end has to put the data on in a compatible format, but therecvfrom()will give you what the sender sent you.So, the direct answer is “You don’t convert the
void *into a character string; you convert the character string into avoid *by passing it torecvfrom()“.