Reputation: 2642
I am developing a network software as a part of an university exam. The software is almost finished, but actually I am finishing the concurrent part (with fork()). My needs are to exchange between client and server these two messages as handshake. Here an example: PING:3506:DOWNLOAD PONG:5605
Here my way to handle these messages: On client side, that is the host who sends PING:3506:DOWNLOAD, I wrote
int *childLocalPort;
childLocalPort = malloc(sizeof(int));
childLocalPort[0] = (SERV_PORT_OFFSET + getPort(&portArray, &pidArray, &arrayCounter, cpid));
char *pingProcedureString;
pingProcedureString = malloc(30*sizeof(char));
strcpy(pingProcedureString, "PING:");
char *itoaPortBuffer;
itoaPortBuffer = malloc(6*sizeof(char));
itoa((childLocalPort[0]), itoaPortBuffer, 10);
strcat(pingProcedureString, itoaPortBuffer);
strcat(pingProcedureString, ":DOWNLOAD");
if (sendto(sockfd, pingProcedureString, strlen(pingProcedureString), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("errore in sendto1");
exit(1);
}
free(itoaPortBuffer);
free(pingProcedureString);
n = recvfrom(sockfd, buff, MAXLINE, 0, NULL, NULL);
buff[n] = 0;
if(strcmp(buff,"PONG"))
{
int *childRemotePort;
childRemotePort = malloc(sizeof(int));
strtok(buff, ":");
childRemotePort[0] = ntohs(strtok(NULL, ":"));
printf("Remote port is %d\n", childRemotePort[0]);
close(pipeLocalPort[0]); /* Close unused read end */
write(pipeLocalPort[1], childLocalPort, sizeof(int)
close(pipeLocalPort[1]); /* Reader will see EOF */
close(pipeRemotePort[0]);
write(pipeRemotePort[1], childRemotePort, sizeof(int));
close(pipeRemotePort[1]);
}
On server side, that is the host who sends PONG:5605, I wrote
if ((n > 0) && strcmp(recvline,"PING"))
{
int *childRemotePort;
childRemotePort = malloc(sizeof(int));
strtok(recvline, ":");
char *buffTemp;
buffTemp = calloc(5, sizeof(char));
strcpy(buffTemp,strtok(NULL, ":"));
childRemotePort[0] = ntohs(atoi(buffTemp));
strtok(recvline, ":");
printf("Remote child client port is: %d\n", childRemotePort[0]);
}
As you can notice, the PONG part is missing, because I would like to focus on first non working part. The server receives correctly (as I can see from Wireshark) the message PING:3506:DOWNLOAD, but he tells me he received 19476 instead of 3506, and this is not true. I also noticed that if I try to send numeric messages without converting them into network byte order, things get worse. I am fighting with this from many days, and I don't know anymore what to think about.
Upvotes: 1
Views: 340
Reputation: 17441
when you'r sending a PING from client , the client fails to do a ntohs
on the integer port before converting to ascii but while receiving at server you do a ntohs
, i think this is causing the error. Doing something on these lines on the client PING side might help however this is still error prone if the endianness of server and client is different.
char *itoaPortBuffer;
itoaPortBuffer = malloc(6*sizeof(char));
itoa(ntohs(childLocalPort[0]), itoaPortBuffer, 10);
strcat(pingProcedureString, itoaPortBuffer);
strcat(pingProcedureString, ":DOWNLOAD");
Upvotes: 1