C 从udp服务器访问数据

C 从udp服务器访问数据,c,udp,C,Udp,我有一个问题,我有一个客户端服务器udp,在服务器端我需要访问客户端的数据来控制机器人的移动。对于实验,如果数据值为1,我使用打印“oke”。 下面是程序代码: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #inclu

我有一个问题,我有一个客户端服务器udp,在服务器端我需要访问客户端的数据来控制机器人的移动。对于实验,如果数据值为1,我使用打印“oke”。 下面是程序代码:

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <netdb.h>
#define MYPORT 4950
#define MAXBUFLEN 100
int sockfd;
struct sockaddr_in my_addr; 
struct sockaddr_in their_addr; 
struct hostent *he;
int addr_len, numbytes;
char dt[30];
char buf[MAXBUFLEN];
int main() 
{
printf("‐‐‐‐‐ PROGRAM CHATTING ‐‐‐‐‐\n");

if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
    perror("socket");
    exit(1); }

my_addr.sin_family = AF_INET; 
my_addr.sin_port = htons(MYPORT); 
my_addr.sin_addr.s_addr = INADDR_ANY; 
memset(&(my_addr.sin_zero),'\0',8);

if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
    perror("bind");
    exit(1); }


while(1){
    addr_len = sizeof(struct sockaddr);
    if((numbytes=recvfrom(sockfd,buf,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,&addr_len))==-1)
    {
        perror("recvfrom");
        exit(1);}

    buf[numbytes]='\0';
    printf("%s : \"%s\"\n", inet_ntoa(their_addr.sin_addr), buf);

    if (buf[0]==1)
    { printf("oke\n");}

    printf("Me : ");
    scanf("%s", dt); 
    if((numbytes=sendto(sockfd,dt,strlen(dt),0,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)))==-1)
    {
        perror("sendto");
        exit(1); 
    }

}
close(sockfd);
return 0;
}
即使在该计划中,也有:

if (buf[0]==1)
{ printf("oke\n");}
为什么程序无法访问if内部?

请尝试
if(buf[0]='1')


数字字符映射到与其数字不同的ASCII值,因此,例如,
'1'==49

尝试
if(buf[0]='1')
<代码>'1'==49。(数字字符映射到与其数字不同的ASCII值)谢谢。。你救了我的命。@zebediah49你应该把它作为一个答案。
if (buf[0]==1)
{ printf("oke\n");}