客户端 - 服务器程序在通过DBX运行时工作正常,但在通过终端运行时失败

时间:2016-03-17 15:17:22

标签: c sockets

我收到了这个奇怪的错误。客户端 - 服务器程序在通过DBX运行时工作正常,但在终端运行时失败。

Server.c:

int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided\n");
        exit(1);
    }
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    error("ERROR opening socket");
    memset((char *) &serv_addr,'\0',sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    if (bind(sockfd, (struct sockaddr *) &serv_addr,
            sizeof(serv_addr)) < 0)
            error("ERROR on binding");
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    for(;;)
    {
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr,&clilen);
        if (newsockfd < 0)
            error("ERROR on accept");
        memset(buffer,'\0',256);
        n = read(newsockfd,buffer,255);
        if (n < 0) error("ERROR reading from socket");
        printf("Here is the message: %s\n",buffer);
        n = write(newsockfd,"I got your message",18);
        if (n < 0){ error("ERROR writing to socket");break;}
        close(newsockfd);
    }
    close(sockfd);
    return 0;
}

Client.c:

int main(int argc, char *argv[])
{
    int sockfd, portno, n,c;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    char server_name[256];
    static u_long   addr;
    static int      addrflag;

    char buffer[256];
    if (argc < 3) {
    fprintf(stderr,"usage %s hostname port\n", argv[0]);
    exit(0);
    }

    for(c=1;c<argc;c++)
    {
        if (strcmp(argv[c],"-h")==0)
        {
                if ((c+1) < argc){ strcpy(server_name,argv[c+1]); c++;}
        }
        if (strcmp(argv[c],"-p")==0)
        {
                if ((c+1) < argc) { portno=(u_short)strtol(argv[c+1],(char **)NULL,10); c++; };
        }
    }
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");

    memset((char *) &serv_addr,'\0',sizeof(serv_addr));
    addr = inet_addr(server_name);
    addrflag=0;

    if ((int)addr == -1)
    {
            if((server = gethostbyname(server_name)) == NULL)
            {
                    server = gethostbyaddr((char *)&addr, sizeof (addr), AF_INET);
                    if(server == NULL)
                    {
                            printf("Fuck off not able to get host name\n");
                            exit(0);
                    }
            }
    }
    else
            addrflag=1;

    if (addrflag==1)
    {
            memcpy((char *) &serv_addr.sin_addr, (char *)&addr, sizeof(addr));
    }
    else
    {
            memcpy((char *) &serv_addr.sin_addr, server->h_addr, server->h_length);
    }
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
            error("ERROR connecting");
    memset(buffer,'\0',256);
    n = write(sockfd,"Hi I am Gajendra",strlen("Hi I am Gajendra"));
    if (n < 0)
            error("ERROR writing to socket");
    memset(buffer,'\0',256);
    n = read(sockfd,buffer,255);
    if (n < 0)
            error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(sockfd);
    return 0;
}

客户端通过终端运行:

>client -h localhost -p 15090

recv: Connection reset by peer

客户端通过dbx运行:

(dbx) run -h localhost -p 15090
Running: client -h localhost -p 15090
(process id 18604)
I got your message

execution completed, exit code is 0

有人可以指出我错过了什么吗? 在此先感谢。

0 个答案:

没有答案