简体   繁体   English

从客户端发送任何消息之前,无法从服务器端向客户端打印消息

[英]Unable to print message from server side to client side before sending any message from client

I have two programs for server and client. 我有两个用于服务器和客户端的程序。 My server can connect to multiple client. 我的服务器可以连接到多个客户端。 But the problem right now I am facing is that, I want to display some message from the server side to client side but I am unable to do that. 但是我现在面临的问题是,我想显示一些从服务器端到客户端的消息,但我无法做到这一点。 Both the server and client program are provided below. 下面提供了服务器程序和客户端程序。 I think the syntax that is preventing me to print the message on the client side is scanf which is used the code of client (second line of the while loop). 我认为阻止我在客户端打印消息的语法是scanf ,它用于客户端的代码(while循环的第二行)。 I am not getting any solution for that about how to display the two messages from the server side when the client will get connected. 对于客户端连接时如何显示来自服务器端的两条消息,我没有任何解决方案。

Problem 1: Server side messages that I want to display on the client side when client gets connected : (can be found in the new_connection_handler function) 问题1:当客户端建立连接时,我想在客户端上显示的服务器端消息:(可以在new_connection_handler函数中找到)

 message = "This is connection handler\n";
 message = "Type something \n";

Problem 2: 问题2:

Why I need to use the sleep (2) on the client side? 为什么我需要在客户端使用sleep (2) If I do not use the sleep (2) , then I cannot receive the data sent by the client on the client side. 如果我不使用sleep (2) ,那么我将无法在客户端接收客户端发送的数据。 The data shows when I send something second time from the client side. 数据显示我第二次从客户端发送邮件的时间。

Problem 3: 问题三:

Is it possible to write both the client and server code together and compile and run it using command line arguments? 是否可以将客户端和服务器代码一起编写并使用命令行参数进行编译和运行?

Server Code: 服务器代码:

#include<stdio.h>
#include<string.h>    //strlen
#include<stdlib.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>    //write
#include<pthread.h> //for thread


 #define MAX_CLIENTS 5


//the thread function
void *new_connection_handler(void *);

int main(int argc , char *argv[])
{
    int socket_desc , client_sock , c , *new_sock;
    struct sockaddr_in server , client;


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


    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );
    bzero (&server.sin_zero, 8);



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


    //Listen
    listen(socket_desc , MAX_CLIENTS);



    //Accept and incoming connection
    printf("Waiting for incoming connections\n");




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



        pthread_t thread_id;




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



     printf("Handler assigned\n");
    }


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

    return 0;
}


void *new_connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size;
    char *message , client_message[2000];



    //Send some messages to the client
    message = "This is connection handler\n";
    write(sock , message , strlen(message));


    message = "Type something \n";
    write(sock , message , strlen(message));


    //Receive a message from client
    while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
    {
        //Send the message back to client
        write(sock , client_message , strlen(client_message));
    }



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

    //Free the socket pointer
    free(socket_desc);

    return 0;
}

Client Code: 客户代码:

#include<stdio.h> //printf
#include<string.h>    //strlen
#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr

int main(int argc , char *argv[])
{
    int sock;
    struct sockaddr_in server;
    char message[1000] , server_reply[2000];
    int len;

    //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");

    //keep communicating with server
    while(1)
    {
        printf("Enter message : ");
        scanf("%s" , message);


        //Send some data
        if( send(sock , message , strlen(message) , 0) < 0)
        {
            puts("Send failed");
            return 1;
        }
        sleep (2);
        //Receive a reply from the server
        if((len = recv(sock , server_reply , 2000 , 0)) < 0)
        {
            puts("recv failed");
            break;
        }

        puts("Server reply :");
        server_reply [len]='\0';
    printf("%s\n", server_reply);


    }

    close(sock);

    return 0;
}

Your frames are out of sync You open your client handling thread with two sends from the server. 您的帧不同步您从服务器发送了两个发送消息,以打开客户端处理线程。 Without your "sleep", you pick up one, but not the other. 没有“睡眠”,您会选择其中一个,而不会选择另一个。 your buffer sizes are also not being used correctly, as they're inconsistently being treated as terminated strings when in fact their send length is based on strlen (which is ok, so long as it is consistent). 您的缓冲区大小也没有得到正确使用,因为实际上它们的发送长度基于strlen时,不一致地将它们视为终止字符串(只要是一致的,就可以)。

I think this is what you're trying to do, with a few modifications: 我认为这是您正在尝试做的事情,并做了一些修改:

Client Code 客户代码

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdint.h>

int main(int argc , char *argv[])
{
    int sock;
    struct sockaddr_in server;
    char message[1000] , server_reply[2000];
    int len;

    //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");

    //keep communicating with server
    while((len = recv(sock, server_reply, sizeof(server_reply), 0)) > 0)
    {
        printf("Server reply: %.*s", len, server_reply);

        printf("Enter message : ");
        if (fgets(message, sizeof(message), stdin) == NULL)
            break;

        //Send some data
        if( send(sock , message , strlen(message) , 0) < 0)
        {
            puts("Send failed");
            return 1;
        }
    }

    close(sock);

    return 0;
}

Server Code 服务器代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
#include<stdint.h>


#define MAX_CLIENTS 5


//the thread function
void *new_connection_handler(void *);

int main(int argc , char *argv[])
{
    int socket_desc , client_sock;
    struct sockaddr_in server , client;
    socklen_t c = sizeof(client);

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

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );
    bzero (&server.sin_zero, 8);

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

    //Listen
    listen(socket_desc , MAX_CLIENTS);

    //Accept and incoming connection
    printf("Waiting for incoming connections\n");

    c = sizeof(client);
    while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, &c)) )
    {
        printf("Connection accepted");
        pthread_t thread_id;

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

        printf("Handler assigned\n");
    }


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

    return 0;
}


void *new_connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = (intptr_t)socket_desc;
    int read_size = 0;
    char client_message[2000];

    static const char rdy[] = "READY\n";
    write(sock, rdy, sizeof(rdy)-1);

    //Receive a message from client
    while( (read_size = recv(sock , client_message , sizeof(client_message) , 0)) > 0 )
        write(sock , client_message , read_size);

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

    return 0;
}

This still needs better join-logic on the client threads, but if you paste and compile each with appropriate flags I think it does what you're trying to achieve. 仍然需要在客户端线程上使用更好的连接逻辑,但是如果您使用适当的标志粘贴和编译每个逻辑,我认为它可以实现您想要实现的目标。

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM