开发者

Send chars with UDP

开发者 https://www.devze.com 2023-03-04 05:45 出处:网络
I\'m trying to send text with UDP, by sending every character separately, but something seems to be wrong.

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(soc开发者_如何学Gok, &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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号