我在客户端一次只接收一条线路的问题。我正在尝试一种方法来一次接收整个流。谁能纠正我对接收问题的看法?我已经包含了客户端代码和我认为相关的部分服务器端代码。
#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 进行了编译,但没有收到任何警告。