客户端向服务器发送消息并显示无法发送错误,程序中有什么问题

时间:2018-10-09 01:15:05

标签: sockets

客户端程序:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define LEN 256

int main(int argc, char *argv[]) {
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int sock, s, n, r;
    char  message[256] , receive[1024];
    int msglen, recvlen;
    if (argc < 3)
    {
        fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* Obtain address(es) matching host/port */

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM; /* streaming socket */
    hints.ai_flags = 0;
    hints.ai_protocol = 0; /* Any protocol */

    s = getaddrinfo(argv[1], argv[2], &hints, &result);
    if (s != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
        exit(EXIT_FAILURE);
    }



    /* getaddrinfo() returns a list of address structures.
     Try each address until we successfully connect(2).
     If socket(2) (or connect(2)) fails, we (close the socket
     and) try the next address. */

    for (rp = result; rp != NULL; rp = rp->ai_next) {
        sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (sock == -1)
            continue;

        if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
            break; /* Success */

        close(sock);
    }

    if (rp == NULL) { /* No address succeeded */
        fprintf(stderr, "Could not connect\n");
        exit(EXIT_FAILURE);
    }

    /* Send remaining command-line arguments as separate
     datagrams, and read responses from server */

    while(1)
    {
        bzero(message, LEN );
        fgets(message, LEN, stdin);
        printf("The server sent: %s\n",message);
        n = send(sock, message, strlen(message), 0);
        {
            fprintf(stderr, "Could not send\n");
                    exit(EXIT_FAILURE);
        }

        bzero(message, 255);
        n = recv(sock, message, LEN, 0);
        if (n < 0)
        {
            fprintf(stderr, "Could not receive\n");
                    exit(EXIT_FAILURE);
        }
        printf("The client received: %s\n", message);
        int i = strncmp("Exit", receive, 4);
                        if(i == 0)
                            break;
    }
return 0;
}

服务器程序:

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#define LEN 256

int main(int argc, char *argv[]) {
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    struct sockaddr_in storage;
    int sock, s,l,a,n;
    char receive[LEN], response[1024];
    int recvlen = strlen(receive);
    pid_t childpid;

    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s port\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
    hints.ai_flags = 0; /* For wildcard IP address */
    hints.ai_protocol = 0; /* Any protocol */
    hints.ai_canonname = NULL;
    hints.ai_addr = NULL;
    hints.ai_next = NULL;

    s = getaddrinfo(NULL, argv[1], &hints, &result);
    if (s != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
        exit(EXIT_FAILURE);
    }

    /* getaddrinfo() returns a list of address structures.
     Try each address until we successfully bind(2).
     If socket(2) (or bind(2)) fails, we (close the socket
     and) try the next address. */

    for (rp = result; rp != NULL; rp = rp->ai_next) {
        sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (sock == -1)
            continue;

        if (bind(sock, rp->ai_addr, rp->ai_addrlen) == 0)
            break; /* Success */

        close(sock);
    }

    if (rp == NULL) { /* No address succeeded */
        fprintf(stderr, "Could not bind\n");
        exit(EXIT_FAILURE);
    }


    l = listen(sock, 5);
    if(l != 0)
    {
        fprintf(stderr, "error in listening\n");
        exit(EXIT_FAILURE);
    }


    socklen_t str_storage = sizeof(storage);

        a = accept(sock, (struct sockaddr*)&storage, &str_storage);
            if(a < 0)
            {
                fprintf(stderr, "the acceptance is not done\n");
                exit(EXIT_FAILURE);
            }

            while(1)
            {
                bzero(receive, LEN);
                n = recv(a, receive, LEN , 0);
                if(n < 0)
                {
                    fprintf(stderr, "error in reading\n");
                            exit(EXIT_FAILURE);
                }
                printf("The client message: %s\n ", receive);
                bzero(receive, LEN);
                fgets(receive, LEN, stdin);

                n = send(a, receive, recvlen, 0);
                if(n < 0)
                {
                    fprintf(stderr, "error in replying\n");
                            exit(EXIT_FAILURE);
                }
                int i = strncmp("Exit", receive, 4);
                if(i == 0)
                    break;

                printf("The server message: %s\n", receive);

            }
close(a);
return 0;

}

linux输出双方:

$ ./client localhost 5690
deeeksha hey
The server sent: deeeksha hey

Could not send

服务器输出:

$ ./server 5690
The client message: deeeksha hey

1 个答案:

答案 0 :(得分:1)

    n = send(sock, message, strlen(message), 0);
    {
        fprintf(stderr, "Could not send\n");
                exit(EXIT_FAILURE);
    }

无论send是否成功,您的客户端都将执行此块。看来您忘记了检查if (n<0)了,但只是有一个没有任何条件的区块。可能是由于忘记复制第一行而从代码中的其他地方复制并粘贴的情况。