2

I'm trying to send text with UDP, by sending every character separately, but something seems to be wrong. This is the client:

while(strcmp(sir,"0")!=0)
{
     printf("Text number %d:", i);
     i++;
     scanf("%s",sir);
     printf("\n");
     sirLen=strlen(sir);
     for(j=0;j<sirLen;j++)
     {     c=sir[j];
          printf("%c",c);
          sendto(sock, &c, sizeof(char), 0, (struct sockaddr *)&ServAddr, sizeof(ServAddr));
     }
}

raspunsLen=recvfrom(sock, raspuns, SIRMAX, 0,(struct sockaddr *) &fromAddr, &fromSize);
raspuns[raspunsLen] = '\0';
printf("%s",raspuns);

And this would be the server:

for (;;) 
{
    cliAddrLen = sizeof(ClntAddr); 
    while(sir != '0')
    { 
      recvfrom(sock, &sir, sizeof(char), 0,(struct sockaddr *) &ClntAddr, &cliAddrLen);
      raspuns[i]=sir;
      printf("%c",sir);
    }
    raspunsMsgSize=strlen(raspuns); 
    sendto(sock, raspuns, raspunsMsgSize, 0, (struct sockaddr *) &ClntAddr,             sizeof(ClntAddr));
}

It works when I send whole pieces of text but this way the server doesn't seem to be receiving anything. Hope someone can help. Thank you.

Pepe
  • 6,360
  • 5
  • 27
  • 29
Vidi
  • 183
  • 1
  • 1
  • 9
  • 1
    Your code expects '0' at the end, but I don't see you sending it - unless it's a part of the string the user types in. In fact, you probably meant 0, as in "byte with all bits clear", but you don't send that either. Try typing, say, "CS 101" as your input string. –  May 09 '11 at 19:24
  • What is the declaration of `sir` in the server portion? – Mark Wilkins May 09 '11 at 19:28
  • One quick suggestion, never use UDP for a sending mechanism where u have individual character to be sent in a packet. Because UDP is not a reliable protocol and u will never know what comes after what. If this is the case you have to use tcp protocol. – maheshgupta024 May 09 '11 at 19:17
  • @maheshgupta024 I know it is unreliable, but I have to send each character separately.Again, I know this would be something you would do using TCP, but this is how I'm requested to do it.Don't know why.School project. – Vidi May 09 '11 at 19:48
  • @Mark Wilkins It is `char sir='1';` – Vidi May 09 '11 at 19:58
  • @VDCan: I was mainly wondering since it was an array in the client. – Mark Wilkins May 09 '11 at 20:22

1 Answers1

1

I suspect:

while(sir != '0')

should be:

while(sir != 0)

or possibly:

while( * sir != 0 )

But really, you haven't posted enough code to be sure.

  • I feel it is not *sir, it is a normal variable you see he has used the address of the sir variable in other calls too.. According to me it is because of the unreliable udp protocol. – maheshgupta024 May 09 '11 at 19:18
  • I'm sorry for not posting the whole code. It would have taken to much space. As I said, the program works with sending strings, but not with characters. Maybe I'll just stick with sending strings. – Vidi May 09 '11 at 19:57