我遇到了一次只在客戶端接收一行的問題。我正在嘗試通過一種方式一次接收整個流。任何人都可以糾正我對接收問題的看法嗎?我已經包含了客戶端代碼和一部分我認爲是相關的服務器端代碼。c socket編程,一次只接收一行
#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,並沒有收到任何警告。
當你說「行結束」時,你是否在sprintf語句中建議\ n?我不遵循你所說的,你能告訴我嗎? –
是的,這正是我的意思。否則,您可能會收到您發送的所有數據,如下所示:'「部門123部門456部門789」' – duskwuff
這似乎不起作用。我仍然不得不壓低輸入以獲得下一行信息。 –