在C中以UDP发送和接收

时间:2012-11-05 11:23:04

标签: c

我在ActClientSender中遇到的问题当我在ActServerReceiver上发送它接收的消息时,当我在ActSeverSender中发送消息时,它自己接收消息。

struct发送 s =(struct Sending )索引;这里我正在从ActServerReceiver

中找到portno和sock_des

这是我的代码:

void *ActServerSender(void *index)
{
    char msg[200];
    struct sockaddr_in serv_addr;
    int sock_des = socket(PF_INET,SOCK_DGRAM,0);
    struct Sending *s=(struct Sending*)index;
    serv_addr=s->serv_addr;
    sock_des=s->sock_des;
    printf("%d sock des",sock_des);
    int sockaddr_len = sizeof(serv_addr);

    printf("%d sock des",sock_des);

    while(1)
    {
        int count=0;
        printf("Enter Your Message\n");
        while (scanf("%c", &msg[count]) )
        {
            if(msg[count]=='\n')
                break;
            count++;
        }
        msg[count]='\0';
        printf("This message is send:%s\n",msg);
        sendto (sock_des, msg, 200, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
    }
}

void *ActsenderSender(void *index)
{   
    char msg[200];  
    struct sockaddr_in serv_addr;
    int sock_des = socket(PF_INET,SOCK_DGRAM,0);
    struct Sending *s=(struct Sending*)index;
    serv_addr=s->serv_addr;
    sock_des=s->sock_des;

    int sockaddr_len = sizeof(serv_addr);
    while(1)
    {
        printf("yahoooooooooooo!\n");   
        recvfrom(sock_des, msg, 200, 0, (struct sockaddr *)&serv_addr, &sockaddr_len);
        printf("Message Received %s\n",msg);
        fflush(stdout);
    }   
}

void *ActSenderReceiver(void *port)
{   
    pthread_t ActsenderSend;
    int portno=(int)port;
    printf("Your Port%d\n",portno);
    char msg[200];
    struct Sending s;
    struct sockaddr_in serv_addr;
    int sock_des = socket (AF_INET,SOCK_DGRAM,0);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons (portno);
    serv_addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
    s.serv_addr=serv_addr;
    s.sock_des=sock_des;
    s.port=portno;

    int j=0;
    while(1)
    {
        if(j==2)
        {
            pthread_create( &ActsenderSend , NULL , ActsenderSender , (void*) &s);
        }
        int count=0;
        printf("Enter Your Message or say Bye to leave the chat\n");
        while (scanf("%c", &msg[count]) )
        {
            if(msg[count]=='\n')
                break;
            count++;
        }

        msg[count]='\0';
        printf("This message is send:%s\n",msg);
        if(strcmp(msg,"Bye")==0)
        {
            clientSender=1;
        }
        sendto (sock_des, msg, 200, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
        j++;
    }
}

void *ActAsAServerReceive(void *index)
{
    char msg[200];
    int portno=(int)index;
    //UDP Connection
    pthread_t ActServerSend;
    struct Sending s;
    printf("Your Port%d\n",portno);
    struct sockaddr_in serv_addr;
    int sock_des = socket(PF_INET,SOCK_DGRAM,0);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(portno);
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    bind(sock_des,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
    int sockaddr_len = sizeof(serv_addr);
    s.serv_addr=serv_addr;
    s.sock_des=sock_des;
    s.port=portno;
    int count=0;
    while(1)
    {
        if(count==2)
        {
            pthread_create( &ActServerSend , NULL , ActServerSender , (void*) &s);
            //pthread_join(ActServerSend, NULL);    
        }   
        recvfrom(sock_des, msg, 200, 0, (struct sockaddr *)&serv_addr, &sockaddr_len);
        printf("Message Received%s\n",msg);
        fflush(stdout);
        count++;
    }
}

1 个答案:

答案 0 :(得分:0)

检查ActSeverSender中的portno,端口号应该是客户端侦听端口号。我认为在你的情况下,portno被设置为服务器的监听端口号,因此ActSeverSender中的Send消息是接收消息给自己。