C套接字 - 无法重新连接服务器

时间:2015-10-08 08:58:41

标签: java c sockets

我在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());

    }
}
}

1 个答案:

答案 0 :(得分:0)

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