0

我在 ActClientSender 中面临的问题是当我发送它在 ActServerReceiver 接收的消息时,当我在 ActSeverSender 中发送消息时它自己接收消息。

结构发送s=(结构发送)索引;在这里,我从 ActServerReceiver 获取端口号和 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++;
    }
}
4

1 回答 1

0

查看 ActSeverSender 中的端口号,端口号应为客户端监听端口号。我认为在您的情况下,端口号设置为服务器的侦听端口号,因此 ActSeverSender 中的发送消息正在接收消息给自己。

于 2012-11-05T12:35:24.450 回答