I am having a problem of only receiving one line at a time in my client side. I am trying ti figure a way to receive the whole stream at once. Can anyone correct what I have in regards to the receiving issue? I have included the client code and a portion of the server side code I believe to be relavent.
#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;
}
Here is the client side of things:
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;
I can provide more info if needed. I believe the server side is ok. I have compiled with -Wall and -Wextra and received no warnings.
You are receiving data a line at a time because it’s being sent a line at a time. The fact that it ends up coming out this way is pure luck, though; TCP streams can be split up and recombined arbitrarily by the client TCP stack, or by buffering on the other end. You need some form of explicit framing in the stream — for instance, a count sent before each line, or a
\nat the end of a line.