【问题标题】:C Socket - Unable to reconnect the serverC Socket - 无法重新连接服务器
【发布时间】:2015-10-08 08:58:41
【问题描述】:

我在 c 中创建了一个服务器程序,当我尝试连接服务器时它运行良好,但我断开客户端并第二次连接,没有任何反应。请找到我的源代码。

服务器代码:

主要方法

int main(int argc, char** argv) {
printf(">>>>Server Started!<<<<\n");
while(server() == -1) {
    printf(">>>>>>Restarting the Server....<<<<<<.\n");
}
printf(">>>>Server Stopped!<<<<\n");
return (EXIT_SUCCESS);
}

服务器代码

#define INTERVAL 3
#define PORT 7777
#define MAX_OUTSTATLEN 1000000
#define MAX_STATLEN 100000
#define MAX_CLIMSGLEN 2000

int server() {
#ifdef _WIN32
    SOCKET agent_socket, client_socket;
    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) {
        printf("Initializing WinSock Failed.\n");
        return 1;
    }
#else
    int agent_socket, client_socket;
#endif
    struct sockaddr_in agent, client;
    char *outStat, client_message[MAX_CLIMSGLEN], timestamp[0];
    int client_socket_len, isReconnet = 1, x = 0;
    if ( (agent_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
        printf("Agent creation failed.\n");
        return -1;
    } else {
        printf("Agent creation successful.\n");
    }
    memset(&agent, 0, sizeof agent);
    memset(&client, 0, sizeof client);
    agent.sin_family = AF_INET;
    agent.sin_port = htons(PORT);
    agent.sin_addr.s_addr = INADDR_ANY;
    if(setsockopt(agent_socket, SOL_SOCKET, SO_REUSEADDR, &isReconnet, sizeof(int)) == -1) {
        printf("Unable to set Socket Option.\n");
    }
    if ( bind(agent_socket, (struct sockaddr*)&agent, sizeof(agent)) != 0 ) {
        printf("Agent bind failed.\n");
        return -1;
    } else {
        printf("Agent bind successful.\n");
    }
    if ( listen(agent_socket, 20) != 0 ) {
        printf("Can't listen on the Port.\n");
        return -1;
    } else {
        printf("Waiting for client...\n");
    }
    client_socket_len = sizeof (struct sockaddr_in);
#ifdef _WIN32
    client_socket = accept(agent_socket, (struct sockaddr *) &client, &client_socket_len);
#else
    client_socket = accept(agent_socket, (struct sockaddr *) &client, (socklen_t*) & client_socket_len);
#endif
    if (client_socket < 0) {
        printf("can't accept the Client.\n");
        return -1;
    } else {
        printf("Agent connected to client.\n");
    }
    if (recv(client_socket, client_message, MAX_CLIMSGLEN, 0) > 0) {
        send(client_socket, client_message, strlen(client_message), 0);
        while (1) {
            outStat = malloc(MAX_OUTSTATLEN * sizeof (char));
            *outStat = '\0';
            strcat(outStat, "Some String to Client");
            printf("%s\n", outStat);
            if (recv(client_socket, client_message, MAX_CLIMSGLEN, 0) > 0) {
                send(client_socket, outStat, strlen(outStat), 0);
            } else {
                printf("Unable to reach the client.\n");
                close(client_socket);
                close(agent_socket);
                return -1;
            }
#ifdef _WIN32
            Sleep(INTERVAL * 1000);
#else
            sleep(INTERVAL);
#endif
        }
    } else {
        printf("Message receive failed.\n");
        close(client_socket);
        close(agent_socket);
        return -1;
    }
    return 0;
}

客户端代码:(Java)

public class Test {
public static void main(String[] args) throws UnknownHostException, IOException {
    Socket echoSocket = new Socket("10.8.157.7", 7777);
    PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    out.println("Send Messages");
    while(true){
        out.println("OK");
        System.out.println(in.readLine());

    }
}
}

【问题讨论】:

  • 尝试使用 SO_REUSEADDR = true 打开套接字

标签: java c sockets


【解决方案1】:

您需要将此代码包装在 for 循环或 while 循环中,以便服务器多次接受连接。

【讨论】:

  • 你能举个例子吗?
  • @Ulaganathan 你需要一个循环的例子吗?
猜你喜欢
  • 2017-02-13
  • 1970-01-01
  • 2016-04-03
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多