gpt4 book ai didi

c socket编程,一次只接收一行

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:33 26 4
gpt4 key购买 nike

我的客户端一次只能接收一行。我正在尝试想出一种方法来一次接收整个流。任何人都可以纠正我关于接收问题的内容吗?我已经包含了客户端代码和我认为相关的服务器端代码的一部分。

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>


int main(int argc, char *argv[])

{
FILE *log;
int opt;
int count = 0;
int sock, bytes_received;
char send_data[2048],recv_data[2048];
struct hostent *host;
struct sockaddr_in server_addr;

if(remove("client_log")) ;

if(argc == 2)
{
if(strcmp(argv[1], "-v") == 0)
{
log = fopen("client_log", "a");
opt = 1;
}
else if(strcmp(argv[1], "-V") == 0)
{
log = fopen("client_log", "a");
opt = 2;
}
}

}
else
opt = 0;

host = gethostbyname("127.0.0.0");

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(noneya);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);

if (connect(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
perror("Connect");
exit(1);
}

while(1)
{

bytes_received=recv(sock,recv_data,2048,0);
recv_data[bytes_received] = '\0';

if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
{
close(sock);
break;
}

else
{
if(opt != 0)
fprintf(log, "\n Received Data: %s \n", recv_data);
if(opt == 2)
printf("\nReceived Data: %s \n" , recv_data);
}

if(count == 0)
{
printf("Pick an option: \n");
printf("1 - to search by Department only\n");
printf("2 - to search by Department and Class Number\n");
printf("3 - to search more specifically\n");
printf("q or Q - to Quit");
}
printf("\nSEND: ");
gets(send_data);
if(count == 0)
{
count = (int)send_data;
if(count == 3)
count++;
}
else if(count > 0)
count--;

if(opt != 0)
fprintf(log, "\n Sending Data: %s \n", send_data);
if(opt == 2)
printf("\n Sending Data: %s \n" , send_data);

if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
send(sock,send_data,strlen(send_data), 0);

else
{
send(sock,send_data,strlen(send_data), 0);
close(sock);
break;
}

}
return 0;
}

这是客户端的事情:

 case 1:

fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
fclose(filePointer);
char send_data[] = "Enter Department Name";
send(connected, send_data, strlen(send_data), 0);
bytes_received = recv(connected, recv_data, BUF, 0);
recv_data[bytes_received] = '\0';
strcpy(tempDept, recv_data);
for (i=0; i<MAX_RECORD; i++){
if ((strcmp(tempDept, data[i].Dept)==0) && tempCourse != data[i].course){
sprintf(send_data,"%s %d", data[i].Dept, data[i].course);
send(connected, send_data, strlen(send_data), 0);
tempCourse = data[i].course;

}
}
break;

如果需要,我可以提供更多信息。我相信服务器端没问题。我使用 -Wall 和 -Wextra 进行了编译,没有收到任何警告。

最佳答案

您一次接收一行数据,因为一次发送一行数据。不过,它最终以这种方式出现的事实纯属运气; TCP 流可以由客户端 TCP 堆栈或通过另一端的缓冲任意拆分和重新组合。您需要在流中使用某种形式的显式框架——例如,在每行之前发送一个计数,或在一行末尾发送一个 \n

关于c socket编程,一次只接收一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10434525/

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