0

there are total of 3 processes, one manager and two client, the manager send a message to both clients by TCP, the code below is part from client's. as i pass all the parameters into the add_to_ring function, and start to recv() from TCP socket which is from the passed parameter. the recv() functions will and both clients received correct message, but then i discovered that the parameters passed to this function are all changed, i have no idea who changed them, they are correct before the recv() and have been changed right after the recv(), nothing happened in between.i also tried assign these parameters to some variable in the function, and after recv() those variables changed too. can anyone see why is that? thanks!

void add_to_ring(int pid,int s_sockfd,int i,traid_info *traidinfo,tcp_recv recvbytcp,     unsigned identifier, struct sockaddr_in my_addr)
{
    //receive add message from TCP
    printf("client %d:pid is %d,i is %d,nonce is %lu,identifier is %lu,port is  %d\n",i,pid,i,recvbytcp.nonce,identifier,my_addr.sin_port);
    char addmsg[100];
    int apid=pid;
    int as_sockfd=s_sockfd;
    int ai=i;
    tcp_recv arecvbytcp=recvbytcp;
    unsigned long int aidentifier=identifier;
    struct sockaddr_in amy_addr=my_addr;
    if((recv(s_sockfd, addmsg, MAXDATASIZE, 0)) == -1)
    {
        perror("recv1");
        exit(1);
    }
    printf("client %d:pid is %d,i is %d,nonce is %lu,identifier is %lu,port is  %d\n",i,pid,i,recvbytcp.nonce,identifier,my_addr.sin_port);
    printf("client %d:pid is %d,i is %d,nonce is %lu,identifier is %lu,port is %d\n",ai,apid,ai,arecvbytcp.nonce,aidentifier,amy_addr.sin_port);
    //start add to ring
    if(addmsg[0]=='a')
    {
    if(i==1)
    {
        traidinfo->succ_port = recvbytcp.FP;
        traidinfo->succ_identifier = identifier;
        traidinfo->pred_port = recvbytcp.FP;
        traidinfo->pred_identifier = identifier;
        traidinfo->my_port = my_addr.sin_port;
        traidinfo->my_identifier = identifier;
    }
}
    printf("client %d:pid is %d,i is %d,nonce is %lu,identifier is %lu,port is %d\n",ai,apid,ai,arecvbytcp.nonce,aidentifier,amy_addr.sin_port);

    //finished adding,send back my message by TCP
    char msg2man[100];
    data_process(arecvbytcp.nonce, msg2man, apid, amy_addr.sin_port);
    printf("client %d: port is %lu\n",ai,amy_addr.sin_port);
    if(send(as_sockfd, msg2man, MAXDATASIZE, 0) == -1)
    {
        perror("send");
        exit(1);
    }
}
2
  • 3
    What is MAXDATASIZE?
    – Rohan
    Oct 23, 2013 at 10:49
  • 2
    OT: The code misses to check whether recv()/send() really did receive/sent whould they were told to.
    – alk
    Oct 23, 2013 at 11:05

2 Answers 2

3

addmsg is only 100 bytes, but you recv up to MAXDATASIZE bytes. If you receive more than 100 bytes then apid and as_sockfd will get overwritten.

Try changing the declaration of addmsg:

char addmsg[MAXDATASIZE];
2
  • 2
    yep, mostly when you encounter variables changing values 'by itself' it means you are writing on some (mostly) random memory, because of dynamic allocation, buffer overflow etc.
    – zoska
    Oct 23, 2013 at 16:34
  • Better still, get rid of MAXDATASIZE and use 'sizeof addmsg'. Then you really cannot go wrong.
    – user207421
    Oct 23, 2013 at 21:25
0

Besides the problem with you possibly receiving (and sending as well) more than can fit in the array, you have to remember that TCP doesn't promise to receive all that you ask for, or even a complete message.

You have to check the amount received, and if to little you have to receive again while appending to the buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.