Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

c - How do I hange the server, so it wouldn't turn off?

So I have written the tcp server-client codes, which is supposed to let the 2 clients send 5-letter messeges to each other. Code works and the communication is correct but I have this problem with server:

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

int main() {
    char col1[] = "white";
    char col2[] = "black";

    int welcomeSocket, Client1, Client2;
    struct sockaddr_in serverAddr;
    struct sockaddr_storage serverStorage;
    socklen_t addr_size;
    char buffer[5];

    welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(7891);
    serverAddr.sin_addr.s_addr = INADDR_ANY;
    memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
    bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

    if (listen(welcomeSocket,5)==0)
        printf("Listening
");
    else
        printf("Error
");
        
    while (1) {
        addr_size = sizeof serverStorage;
        Client1 = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
        send(Client1,col2,5,0);
        Client2 = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
        send(Client2,col1,5,0);

        int cmdEXIT = 0;

        while (cmdEXIT == 0) {
            if(recv(Client1, buffer, 5, 0)!= -1){
                send(Client2,buffer,5,0);
                printf ("WYSYL DO CZARNYCH %s 
", buffer);
            } else {
                cmdEXIT =1;
            }
            memset(&buffer[0], 0, sizeof(buffer));

            if(recv(Client2, buffer, 5, 0)!= -1){
                send(Client1,buffer,5,0);
                printf ("WYSY? BIALYCH %s 
", buffer);
            } else {
                cmdEXIT =1;
            }

            memset(&buffer[0], 0, sizeof(buffer));
        }
    }
    return 0;
}

simple client in python:

import socket

TCP_IP = '192.168.0.103'
TCP_PORT = 7891
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((TCP_IP, TCP_PORT))
MESSAGE = input("type ur message")
for k in range(5):
    s.send(MESSAGE.encode('utf-8'))
    data = str(s.recv(5))
    data = data[2:-1]
    print("received data:", data)

s.close()


the problem is when 2 clients connect and then close, my server closes as well. Is there a simple solution to my code that after firts 2 clients close, server instead of closing will be listening for the next 2 clients to connect?

question from:https://stackoverflow.com/questions/65938946/how-do-i-hange-the-server-so-it-wouldnt-turn-off

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...