gpt4 book ai didi

c - 在从客户端发送任何消息之前,无法将消息从服​​务器端打印到客户端

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:24 24 4
gpt4 key购买 nike

我有两个程序用于服务器和客户端。我的服务器可以连接到多个客户端。但我现在面临的问题是,我想从服务器端向客户端显示一些消息,但我无法做到这一点。服务器和客户端程序都在下面提供。我认为阻止我在客户端打印消息的语法是 scanf,它使用客户端代码(while 循环的第二行)。关于客户端连接时如何从服务器端显示两条消息,我没有得到任何解决方案。

问题 1:当客户端连接时,我想在客户端显示的服务器端消息:(可以在 new_connection_handler 函数中找到)

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

问题 2:

为什么我需要在客户端使用sleep (2)?如果我不使用sleep(2),那我在客户端是收不到客户端发来的数据的。数据显示我第二次从客户端发送内容。

问题 3:

是否可以同时编写客户端和服务器代码并使用命令行参数编译和运行它?

服务器代码:

#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;
}

客户端代码:

#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;
}

最佳答案

你的帧不同步你打开你的客户端处理线程,有两个来自服务器的发送。没有你的“ sleep ”,你拿起一个,而不是另一个。您的缓冲区大小也没有被正确使用,因为它们被不一致地视为终止字符串,而实际上它们的发送长度是基于 strlen (没关系,只要它是一致的)。

我认为这就是您尝试要做的,并进行一些修改:

客户端代码

#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;
}

服务器代码

#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;
}

这仍然需要客户端线程上更好的连接逻辑,但如果您使用适当的标志粘贴和编译每个逻辑,我认为它可以实现您想要实现的目标。

祝你好运。

关于c - 在从客户端发送任何消息之前,无法将消息从服​​务器端打印到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24648709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com