gpt4 book ai didi

c - 无法显示或recv()客户端缓冲区中的数据

转载 作者:行者123 更新时间:2023-11-30 17:10:54 24 4
gpt4 key购买 nike

我正在尝试从服务器端的文件/tmp/k 读取数据并将数据复制到消息并发送到客户端。在这里,我可以将数据发送到客户端,但我的客户端无法 recv() 或显示数据。任何人都可以帮我解决这个问题,或者可以提出更好的解决方案来在客户端显示数据。

谢谢

客户端.c

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>

#define MAX_SIZE 100000
#define NUM_CLIENT 5
void *connection_handler(void *);

int main(int argc , char *argv[])
{
int sockfd;
long i;
pthread_t sniffer_thread;
printf("memory allocated for the creation of socket \n");
for (i=1; i<=NUM_CLIENT; i++) {
if( pthread_create( &sniffer_thread , NULL , connection_handler , (void *) i) < 0)
{
perror("could not create thread");
return 1;
}
printf("Thread created \n");
sleep(2);
}
pthread_exit(NULL);
return 0;
}
void *connection_handler(void *threadid)
{
long etid; /*each thread id */
etid = (long)threadid;
pthread_t tid;
tid = pthread_self();
int sockfd;
struct sockaddr_in serv_addr;
char sbuff[MAX_SIZE] , rbuff[MAX_SIZE];
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("Failed creating socket\n");
perror("could not create thread");
return 0;
}
memset(&serv_addr, 0 , sizeof (serv_addr));
printf("the bytes of memory area pointed by serv_addr is filled with constant '0' \n");
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(8888);

if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0) {
perror("Failed to connect to server. Error");
return 0;
}
printf("Connected successfully client:%ld \n", tid);
printf("before calling pthread_create getpid: %d getpthread_self: %lu tid:%lu\n",getpid(), pthread_self(), syscall(SYS_gettid));
while(1)
{
printf("For thread : %ld\n", etid);
printf("thread id given by kernel: %ld\n", tid);
fgets(sbuff, MAX_SIZE , stdin);
send(sockfd,sbuff,strlen(sbuff),0);

if(recv(sockfd,rbuff,MAX_SIZE,0)==0)
printf("Error");
else
fputs(rbuff,stdout);
bzero(rbuff,MAX_SIZE);
sleep(2);
}
close(sockfd);
return 0;
}

服务器.c

 #include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#include<pthread.h> //for threading , link with lpthread
#include <fcntl.h>

#define PROCESSLIST "/tmp/k"
void *connection_handler(void *);
int main(int argc , char *argv[])
{
int sockfd , new_socket , c , *new_sock;
struct sockaddr_in server , client;
char *message;

//Create socket
sockfd = socket(AF_INET , SOCK_STREAM , 0);
if (sockfd == -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 );

//Bind
if( bind(sockfd,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");

//Listen
listen(sockfd , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(sockfd, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");

pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = new_socket;

if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}
puts("Handler assigned");
}

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

return 0;
}

/* This will handle connection for each client */
void *connection_handler(void *sockfd)
{
//Get the socket descriptor
int sock = *(int*)sockfd;
int read_size;
char message[100000], client_message[2000];
int fd;
void *buff;
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
system ("ps -ef > /tmp/k");
puts("temp file getting created");
fd = open (PROCESSLIST, O_RDONLY);
puts("file open has taken place");
read(fd, buff, sizeof(*buff));
puts("reading the file");
write(sock, buff, 1);
puts("writing to the buffer");
puts("copied data from buffer to message");
//Send the message back to client
send(sock,message,strlen(message),0);
puts("data has been sent");
}

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

//Free the socket pointer
free(sockfd);

return 0;
}

最佳答案

几个问题:

在您的服务器中,您可以执行以下操作:

new_sock = malloc(1);

您只分配 1 个字节来保存 int 这空间不够。由于 newsock 指向 int,因此您应该为它分配足够的空间:

new_sock = malloc(sizeof(int));

同样在服务器中,您在读取来自客户端的消息后执行此操作:

    fd = open (PROCESSLIST, O_RDONLY);
puts("file open has taken place");
read(fd, buff, sizeof(*buff));
puts("reading the file");
write(sock, buff, 1);
puts("writing to the buffer");
puts("copied data from buffer to message");
//Send the message back to client
send(sock,message,strlen(message),0);

buff 是未初始化的 void *,因此尝试写入它会导致未定义的行为。 write 正在通过套接字从未初始化的 buff 发送单个字节,这可能不是您想要的。然后,您通过套接字从 message 发送数据,而不向其中放入任何内容。

这里您真正需要做的就是直接从文件读取到消息,然后发送。您首先需要清除 message 中的内存,以便在从文件读取后,它将以 NULL 终止。

因此您可以将上面的内容替换为:

    fd = open (PROCESSLIST, O_RDONLY);
puts("file open has taken place");
memset(message, 0, sizeof(message));
read(fd, message, sizeof(message));
puts("reading the file");
//Send the message back to client
send(sock,message,strlen(message),0);

在客户端中,您有 5 个线程尝试同时读取用户的消息。只有他们中的一个人能够做到。由于服务器忽略客户端发送的数据,因此最好让每个客户端线程发送一些虚拟数据而不提示用户,然后在从服务器获取消息后退出。

关于c - 无法显示或recv()客户端缓冲区中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656390/

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