10

编辑:下面的代码已修复为正确接收和发送,并考虑发送和接收的消息的实际字节(后来感谢 EJP)

我在 Unix 中用 C 编程。

我有应该交换消息的服务器和客户端。虽然客户端似乎可以正常发送消息,但服务器没有收到客户端发送的消息。我试过使用recv()and read()(我知道它们实际上是一样的,但有额外的标志recv())但我没有运气,我不确定问题到底是什么。

sleep(3)每次发送消息后我都会输入客户端代码,但我看到一旦客户端和服务器连接,服务器立即关闭而不等待传入消息。我究竟做错了什么?

这是客户端代码:

#define SERVER_TCP_PORT 11112
#define MAX_DATA_SIZE   500

int main(int argc, char * argv[])
{
    int sockfd;
    char * host;
    char msg[MAX_DATA_SIZE];/* = "get my msg!\n";*/
    int msg_len;

    struct hostent * hp;
    struct sockaddr_in client_address, server_address;


    printf("y halo thar\n");


    // looking up from the host database
    if (argc == 2)
        host = argv[1];
    else
        exit(1);
    printf("sdf\n");


    hp = gethostbyname(host);
    if (!hp)
        exit(1);
    printf("host found\n");


    // setting up address and port structure information
    bzero((char * ) &server_address, sizeof(server_address)); // copy zeroes into string
    server_address.sin_family = AF_INET;
    bcopy(hp->h_addr, (char *) &server_address.sin_addr, hp->h_length);
    server_address.sin_port = htons(SERVER_TCP_PORT);
    printf("set\n");


    // opening up socket
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        exit(1);
    printf("opened\n");


    // connecting
    if (connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
        exit(1);
    printf("connected\n");


    int i;

    for (i = 0; i < MAX_DATA_SIZE; ++i)
    {
        msg[i] = '.';
    }

    msg[MAX_DATA_SIZE-1] = '\0';

    for(i = 0; i < 11; i++)
    {
        // send message to connected socket
        msg_len = write(sockfd, msg, MAX_DATA_SIZE);
        if(msg_len < 1)
            printf("notsent\n");
        else
            printf("%i  bytes sent\n", msg_len);

        // recieve messages from connected socket
        msg_len = read(sockfd, msg, MAX_DATA_SIZE);
        if (msg_len < 1)
            printf("not recieved\n");
        else
        {
            printf("%i bytes received\n", msg_len);
            printf(msg);
            printf("\n");

        }
    }


    // close connection
    close(sockfd);
    printf("closed\n");

}

这是服务器端

#define SERVER_TCP_PORT 11112
#define MAX_DATA_SIZE   500


int main()
{

    printf("o halo thar\n");

    int sockfd, new_sockfd;
    int client_addr_len;
    char msg [MAX_DATA_SIZE];
    int msg_len;
    char got_msg [11] = "got ur msg\0";
    struct sockaddr_in server_address, client_address;


    // setting up address and port structure information
    bzero((char * ) &server_address, sizeof(server_address)); // copy zeroes into string
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_port = htons(SERVER_TCP_PORT);


    // opening up socket
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        exit(1);
    printf("socket is opened\n");


    // binding
    if (bind(sockfd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
        exit(1);
    printf("socket is bound\n");


    // listening
    listen(sockfd,5);
    printf("listening\n");

    // block and wait for an incoming connection

    client_addr_len = sizeof(client_address);
    new_sockfd = accept(sockfd, (struct sockaddr *) &client_address, &client_addr_len);
    if (new_sockfd < 0)
        exit(1);

    printf("accepted\n");


    int i;

    for( i = 0; i < 11; i++)
    {
        // recieve messages from connected socket
        printf("waiting\n");
        msg_len = read(new_sockfd, msg, MAX_DATA_SIZE);
        if (msg_len < 1)
        {
            printf("no msg recieved\n");    
        }
        else
        {
            printf("bytes recieved: %i\n", msg_len);
        }


        // send message to connected socket
        msg_len = write(new_sockfd, got_msg, sizeof(got_msg));
        if (msg_len < 1)
            printf("not sent\n");
        else
            printf("%i bytes sent\n", msg_len);
    }


    // close connection
    close(sockfd);
    printf("socket closed. BYE! \n");


}
4

5 回答 5

9

在服务器代码中,问题出在这一行:

msg_len = read(sockfd, msg, MAX_DATA_SIZE);

您正在调用readon sockfd,但您需要调用reador recvon new_sockfd(由 返回的套接字accept())。 new_sockfd是连接到客户端的那个(sockfd用于接受进一步的连接 - 例如,如果另一个客户端连接)。

于 2010-03-01T05:29:31.773 回答
0

您应该从accept.

尝试调用返回的readfrom 。socketaccept

于 2010-03-01T05:30:12.570 回答
0

发送方:

while(!feof(fp))
{
    len=fread(buff,sizeof(char),MW,fp);
    if(len==0)
    {
      //EOF
      st=write(cd,&d,sizeof(int));      
      break;
    }
    else
    {
        st=write(cd,buff,len);
    }
}
于 2013-03-15T09:52:56.460 回答
0

接收方:

   while(1)
    {       
        len=read(sd,buff,sizeof(buff));

        if(len==0)
        {
            //End of File receving.             

            break;
        }
        else    
        {   
            st=fwrite(buff,sizeof(char),len,fp);

        }
    }
于 2013-03-15T09:56:41.810 回答
-2

是基于流还是数据报的实现?

你的操作流程有问题。服务器可能会在客户端发送任何内容之前开始读取。

由于客户端和服务器是分开的,您可以想象它们同时运行。在您的服务器端“接受”连接请求之后,可能会发生一些握手开销或网络延迟导致服务器应用程序提前执行,尝试提取数据但遇到错误(尚未收到数据)。您可以通过在接受连接后在服务器代码中添加睡眠来尝试这一点,客户端应该有足够的时间来发送数据。

另一个更好的解决方案是让数据检索处理空缓冲区或异步读取。

于 2010-03-01T05:26:57.630 回答