BSD套接字无法正常工作

时间:2012-01-28 06:03:28

标签: linux sockets embedded system

更新:根据建议更新src。 strcat a \ n \ 0到命令并改变sizeof。客户端在发送时有一个“成功”,但服务器在接收时永远不会“成功”。

我已经付出了很多努力来尝试让常规的旧TCP套接字工作。我基本上有一个简单的客户端和服务器设置。目前我的服务器上根本没有收到任何东西,我的客户端只有4个字节(本例中为“GOTY”)。

服务器输出输出并继续:

RECEIVED (null)

客户端输出只是阻止而且永不停止。如果我杀了服务器,我得到这个输出

Connect success
Received Response:

SERVER

int Server::update(char *getbuf)
{
    int ready = poll(pollfds, 1, 100);

    if (ready == -1)
    {
        logger.info("Poll failed");
        return FALSE;
    }

    if (pollfds[0].revents & POLLIN)
    {    
        ssize_t z;

        logger.info("Connection available");

        struct sockaddr_in client_address;
        memset(&client_address, 0, sizeof client_address);
        socklen_t alen = sizeof(client_address);

        // ACCEPT
        clientsocket = accept(serversocket, (struct sockaddr *)&client_address, &alen);
        if(!clientsocket)
            logger.fail("Accept Fail");

        // READ
        z = recv(clientsocket, getbuf, 512, MSG_WAITALL);
        if (z < 0)
            fprintf(stderr,"receive failure\n");
        else
            fprintf(stderr,"receive succeed\n");

        getbuf[z] = 0;

        respond(getbuf);

        //closeConnection();

        logger.data("RECEIVED %s", getbuf);

        getbuf[z] = 0;

        return TRUE;
    }

    return FALSE;
}

void Server::respond(char *response)
{
    // SEND
    ssize_t z;

    z = send(clientsocket, response, strlen(response), 0);
    if (z < 0)
        fprintf(stderr,"send failure\n");
    else
        fprintf(stderr,"send succeed\n");
}

客户

我添加了客户端代码。我现在使用strlen但它似乎没有帮助。

int main(int argc, char **argv)
{
    int abort = 0;
    int port;
    in_addr_t address;
    char *command;

    ssize_t z;
    int com_socket;
    struct sockaddr_in server_address;
    struct timeval timeout;

    int opt;
    int val = 0;
    char getbuf[512];

    //Defaults
    address = inet_addr("127.0.0.1");
    port = 4949;

    //INCLUDED ARGUMENTS FROM CLI
    while((opt = getopt(argc, argv, "a:p:c:")) > 0) 
    {
        switch(opt)
        {
            case 'a':
                address = inet_addr(optarg);
                break;
            case 'p':
                port = atoi(optarg);
                break;
            case 'c':
                abort = 1;
                command = (char *)optarg;
                break;   
            default:
                fprintf(stderr, "-a IPADDRESS -p PORT -c COMMAND\n",argv[0]);
        }
    }

    // Server
    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(port);
    server_address.sin_addr.s_addr = address;

    if (server_address.sin_addr.s_addr == INADDR_NONE)
        fprintf(stderr, "Server address failed\n");

    if(!abort)
    {
        fprintf(stderr, "No Command given\n");
        fprintf(stderr, "-a IPADDRESS -p PORT -C COMMAND\n");
        exit(0);
    }
    else
    {
        fprintf(stderr, "Address %s Port %d Command %s\n", inet_ntoa(server_address.sin_addr), port, command);
    }

    // Create com_socket
    com_socket = socket(PF_INET, SOCK_STREAM, 0);
    if (com_socket == -1)
         fprintf(stderr, "Socket failed\n");

    /*
    // Client
    struct sockaddr_in client_address;
    memset(&client_address,0,sizeof client_address);
    client_address.sin_family = AF_INET;
    client_address.sin_port = 0;
    client_address.sin_addr.s_addr = ntohl(INADDR_ANY);

    if (client_address.sin_addr.s_addr == INADDR_NONE)
        fprintf(stderr, "Client address failed\n");

    // Bind
    z= bind(com_socket, (struct sockaddr *)&client_address, sizeof (client_address));
    if ( z == -1 )
        fprintf(stderr,"Binding port\n");
    */

    timeout.tv_sec = 2; /* 2 seconds */ 
    timeout.tv_usec = 0; /* + 0 usec */

    socklen_t addrlen = sizeof(struct sockaddr_in);

    // Connect
    //z = connectWithTimeout(com_socket, (struct sockaddr *) &server_address, len_inet, &timeout);
    z = connect(com_socket, (struct sockaddr *) &server_address, sizeof(server_address));
    if(z == -1)
    {
        if(errno == EINPROGRESS)
        {
            fprintf(stderr, "EINPROGRESS non block start\n");
        }

        if(errno == EALREADY)
        {
            fprintf(stderr, "EALREADY non block subsequent request\n");
        }

        fprintf(stderr, "Connect failed\n");
    }
    else 
        fprintf(stderr, "Connect success\n");

    strcat(command, "\n\0");

    // SEND
    z = send(com_socket, command, strlen(command)+2, 0);
    if (z < 0)
        fprintf(stderr,"send failure\n");
    else
        fprintf(stderr,"send succeed\n");

    // READ
    z = recv(com_socket, getbuf, 512, MSG_WAITALL);
    if (z < 0)
        fprintf(stderr,"receive failure\n");
    else
        fprintf(stderr,"receive succeed\n");

    // Output
    fprintf(stderr, "Received Response: %s\n", getbuf);

    close(com_socket);

    exit(1);
}

我只是不知道为什么这不起作用。我一次又一次地过去了。

这是一个嵌入式Linux系统。

3 个答案:

答案 0 :(得分:4)

在服务器中:这是完全错误的。 strlen()返回 read()/ recv()之前缓冲区中字符串的长度。

    // READ
    z = recv(clientsocket, getbuf, strlen(getbuf), MSG_WAITALL);
    if (z < 0)
        fprintf(stderr,"receive failure\n");

另外:在respond()中,strlen()假定响应“string”为空终止。事实并非如此。

z = send(clientsocket, response, strlen(response), 0);

客户端代码中似乎存在相同类型的错误。

更新:由于OP不知道如何在没有字符串函数的情况下生存,我将说明如何做到这一点。

 ... snipped ...
 // READ
    z = recv(clientsocket, getbuf, 512, MSG_WAITALL);
    if (z < 0) {
        // .... if (errno == EAGAIN) continue;
        fprintf(stderr,"receive failure %d(%s) \n"
             , errno, strerror(errno) );
        break; }

    fprintf(stderr,"receive succeed\n");

    respond(getbuf, z);

    return TRUE;
    }
return FALSE;
}

void respond(char *response, size_t siz)
{
// SEND
size_t done;
ssize_t z;

for (done = 0; done < siz; done += z) { 
    z = send(clientsocket, response+done, siz-done, 0);
    if (z < 0) {
        if (errno == EAGAIN) continue;
        fprintf(stderr,"send failure %d(%s)\n"
           , errno, strerror(errno) );
        break; }
    fprintf(stderr,"send succeed\n");
    }
}

答案 1 :(得分:2)

由于你还没有真正分享你的“回应”功能的来源,我的心灵力量告诉我它看起来像这样:

void Server::respond(char* str)
{
    send(clientsock, str, sizeof(str), 0);
}

因此,“sizeof(str)”正在评估为4,因为这是您机器上指针的大小。这与客户端收到“GOTY”(您要发送的邮件的前4个字节)一致。

将其更改为以下内容:

void Server::respond(char* str)
{
    send(clientsock, str, strlen(str), 0);
}

此外,您永远不应该假设send和recv将返回一个等于您希望发送或接收的数据量的值。因此,您应该将非阻塞调用循环到send / recv,直到整个消息被发送或消费为止。

答案 2 :(得分:-2)

套接字上的发送操作通常是缓冲的,因此您的响应功能可能不会刷新所有数据。在respond()结束时尝试刷新操作以获取额外数据。

我觉得你看到阻塞行为,你可能想看看阻塞/非阻塞如何影响套接字。