多个客户端和服务器通信

时间:2013-08-07 10:43:12

标签: multithreading sockets client-server command-line-arguments

我是客户端服务器编程的新手,但有些我如何理解某些事情并尝试在客户端和服务器之间进行通信。我的代码是错误的,它不会消除整个字符串,有人可以告诉我如何从客户端向服务器发送整个字符串(有空格)。

输出:
RHU>> confgure CM端口(这是我的整个字符串)服务器回复=配置
RHU>>服务器回复= CM RHU>> server reply = port

以上整个内容输出为一个字符串配置CM端口

还有一件事可以用argv []中的方法来捕获这些参数。这是我的服务器代码。

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
    printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
    //print the error message
    perror("bind failed. Error");
    return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);


//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
pthread_t thread_id;

while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
    puts("Connection accepted");

    if( pthread_create( &thread_id , NULL ,  connection_handler , (void*) &client_sock) < 0)
    {
        perror("could not create thread");
        return 1;
    }

    //Now join the thread , so that we dont terminate before the thread
    //pthread_join( thread_id , NULL);
    puts("Handler assigned");
}

if (client_sock < 0)
{
    perror("accept failed");
    return 1;
}

return 0;

}

//void connectionhandler(void *sock_desc)
{
//write(sock , message , strlen(message));

//message = "Now type something and i shall repeat what you type \n";
//write(sock , message , strlen(message));

//Receive a message from client
while( (read_size = recv(sock , client_message , 2000, 0)) > 0)
{
    client_message[read_size] = '\0';
    write(sock, client_message, strlen(client_message));
    memset(client_message, 0, 2000);
}

if(read_size == 0)
{
    puts("Client disconnected");
    fflush(stdout);
}
else if(read_size == -1)
{
    perror("recv failed");
}


return 0;

}
我的客户代码

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
    printf("Could not create socket");
}
//puts("Socket created");

server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );

//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
    perror("connect failed. Error");
    return 1;
}

//puts("Connected\n");
printf("!!Welcome to RHU CLI!!\n"); 
//keep communicating with server
while(1)
{
    fflush(stdout);
    printf("RHU>> : ");  
    scanf("%s" , message);

    //Send some data
    if( send(sock , message , strlen(message) , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
    //Receive a reply from the server
   if( recv(sock , server_reply , 2000 , 0) < 0)
    {
        puts("recv failed");
        break;
    }
    printf("server reply=%s\n", server_reply);
    //if(strcmp(server_reply, "?") == 0)
    //puts("confgiure");
    parse_init(server_reply);
    memset(server_reply, 0, 2000);
    fflush(stdout);
}

close(sock);
return 0;

}

0 个答案:

没有答案