Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:35:13+00:00 2026-06-03T05:35:13+00:00

I am having a problem of only receiving one line at a time in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:35:14+00:00Added an answer on June 3, 2026 at 5:35 am

    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 \n at the end of a line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

forum member I am having one problem with date time in java. Actually I
I'm having a problem with doing a sendSynchronousRequest failing. It only fails after I
I am having problem that when i am trying to submit the form by
We're having the same setup and receiving the same problem as: IIS 6 with
I'm having a problem where the asset tag time stamp is not being applied
I am having a hell of a time with this problem: Using MPI, I
Im having this problem only with IE8 it even works well on IE6!!... Im
I'm having this problem only on Motorola Milestone. Code: // init recorder recordInstance =
I seem to be having an interesting problem only in chrome (not IE, FF).
I'm wondering if this is something somewhat simple, but I'm having a problem ONLY

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.