成功连接2-3次后,客户端停止连接到服务器c中的套接字编程

时间:2016-11-09 22:43:49

标签: c linux sockets

我正在使用c中的套接字编程尝试简单的客户端 - 服务器程序。当我首先执行服务器然后执行客户端时程序工作正常,但是在成功执行了两到三次之后,客户端停止连接到服务器。如果我在一段时间后执行程序,这将再次开始工作。

客户代码:

<a href="#" class="anchorButton" id="subAnchor">Submit</a>
<p class="targetMessage" id="message">Insert text here.</p>

服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
    int clientSocket,len;
    char buffer[1024];
    struct sockaddr_in sadd;

    clientSocket = socket(PF_INET, SOCK_STREAM, 0);

    sadd.sin_family = AF_INET;
    sadd.sin_port = htons(12345);
    sadd.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(sadd.sin_zero, '\0', sizeof sadd.sin_zero);  

    len = sizeof sadd;
    while(connect(clientSocket, (struct sockaddr *) &sadd, len) == -1) {
        sleep(1);
        printf("trying again..\n");
    }

    read(clientSocket, buffer, sizeof(buffer));
    printf("Data received: %s",buffer);  

    return 0;
}

多次在终端中执行客户端:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
    int defSock, newSock, len;
    char buffer[1024];
    struct sockaddr_in sadd, cadd;


    socklen_t addr_size;

    defSock = socket(PF_INET, SOCK_STREAM, 0);

    sadd.sin_family = AF_INET;
    sadd.sin_port = htons(12345);
    sadd.sin_addr.s_addr = INADDR_ANY;  

    bind(defSock, (struct sockaddr *) &sadd, sizeof(sadd));

    if(listen(defSock,5)==0)
        printf("Listening\n");
    else
        printf("Error\n");

    len = sizeof(cadd);

    newSock = accept(defSock, (struct sockaddr *) &cadd, &len);
    strcpy(buffer,"Hello World\n");
    int n = write(newSock,buffer,sizeof(buffer));

    close(newSock);

    return 0;
}

在另一个终端上同时执行服务器:

[kj@localhost Downloads]$ gcc client.c -o client
client.c: In function ‘main’:
client.c:16:25: warning: implicit declaration of function ‘inet_addr’ [-Wimplicit-function-declaration]
  sadd.sin_addr.s_addr = inet_addr("127.0.0.1");
                         ^
client.c:21:3: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
   sleep(1);
   ^
client.c:25:2: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
  read(clientSocket, buffer, sizeof(buffer));
  ^
[kj@localhost Downloads]$ ./client 
Data received: Hello World
[kj@localhost Downloads]$ ./client 
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
trying again..
^C
[kj@localhost Downloads]$ ./client 
trying again..
trying again..
trying again..
^C
[kj@localhost Downloads]$ ./client 
trying again..
trying again..
trying again..
trying again..
^C
[kj@localhost Downloads]$ 

1 个答案:

答案 0 :(得分:1)

使用private FutureCallback<ImageView> collapseAppBarOnError() { return new FutureCallback<ImageView>() { @Override public void onCompleted(Exception e, ImageView result) { stopLoading(); if (e != null && !(e instanceof CancellationException)) {//TODO: Denne vil ikke funke om errorDrawable ligger i cache? collapse(); } else if (result != null && result.getDrawable() != null && !result.getDrawable().equals(getDrawable(R.drawable.ic_movie_white_48dp))) { expand(); } } }; } 隐藏了另一个问题:

您的服务器只能为一个客户端提供服务,并且在服务一个客户端时不会干净地关闭其侦听套接字。

要解决此问题,您应在服务器中添加SO_REUSEADDR循环,以允许多个客户端连接。

在您的代码中,我添加了:

  • 第一个客户端连接后,一个while循环不会停止,
  • 一次测试/ while以便在发生错误时停止服务器,
  • 一个perror用于侦听套接字以进行干净退出。

这是代码:

close