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

  • Home
  • SEARCH
  • 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 7177973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:49:31+00:00 2026-05-28T16:49:31+00:00

I used to think that write() system call is unbuffered and that fwrite and

  • 0

I used to think that write() system call is unbuffered and that fwrite and fread are used for buffered IO. However I wrote simple programs to establish that some buffering was still going on when using write(). I am using write() and read() on sockets. Due to buffering, it is possible for the client to lag behind while server keeps sending packets. I do not want that. I want that the client must consume the record before the server sends more records.

How can I make that happen without adding network load of acknowledgments etc !

I am using gcc on linux

server.c :

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/tcp.h>

int remote_rr_port=2000; // Server will send RR logs using connection on this port.
char const *remote_server_ip="127.0.0.1";
int connFD_rr;


static void startTcpServer(int *sd, const int port) {
  *sd= socket(AF_INET, SOCK_STREAM, 0);

  // Set socket option so that port can be reused
  int enable = 1;
  setsockopt(*sd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
  struct sockaddr_in a;
  memset(&a,0,sizeof(a));
  a.sin_family = AF_INET;
  a.sin_port = port;
  a.sin_addr.s_addr = INADDR_ANY;
  int bindResult = bind(*sd, (struct sockaddr *) &a, sizeof(a));
  listen(*sd,2);
}


// Wait for connection from client
static int getTcpConnection(int sd) {
  char buf[100];
  socklen_t len;
  struct sockaddr_in clientAddress;
  printf("\nWaiting for connection from remote client\n");
  len = sizeof(clientAddress);
  int connFD = accept(sd, (struct sockaddr*) &clientAddress, &len);
  setsockopt(connFD_rr, SOL_SOCKET, SO_SNDBUF, (int[]){0}, sizeof(int));
  printf("\n Connection from : %s:%d\n",inet_ntop(AF_INET, &clientAddress.sin_addr, buf, sizeof(buf)),clientAddress.sin_port);
  fflush(stdout);
  return connFD;
}

FILE* rdrr_server_start(void) {

  // Socket Descriptors for the two connections
  int rr_sd;
  int input_sd;

  startTcpServer(&rr_sd, remote_rr_port);

  connFD_rr = getTcpConnection(rr_sd);

  return fdopen(connFD_rr, "w");
}

int main() {
  int i = 0;
  rdrr_server_start();

  for(i=0;i<10000000; i++) {
    write(connFD_rr, &i, sizeof (int));
    printf("%d\n", i);
  }
  return 0;
}

client.c :

#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/tcp.h>



int remote_rr_port=2000; // Server will send RR logs using connection on this port.
char const *remote_server_ip="127.0.0.1";
int connFD_rr;

FILE* rdrr_client_start(void) {

  connFD_rr = socket(AF_INET, SOCK_STREAM, 0);

  struct sockaddr_in a;
  memset(&a,0,sizeof(a));
  a.sin_family = AF_INET;
  a.sin_port = remote_rr_port;
  a.sin_addr.s_addr = inet_addr(remote_server_ip);

  printf("\nConnecting to Server on RR port");
  int CONNECT_TO_SERVER= connect(connFD_rr,(struct sockaddr *)  &a, sizeof(a));
  printf("\nConnected to server on RR port");
  setsockopt(connFD_rr, SOL_SOCKET, SO_RCVBUF, (int[]){0}, sizeof(int));
  return fdopen(connFD_rr, "r");
}  

int main() {
  int i = 0;
  rdrr_client_start();
  getrchar();
  while(1) {
    read(connFD_rr, &i, sizeof (int));
    printf("%d\n", i);
  }
  return 0;
} 
  • 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-05-28T16:49:32+00:00Added an answer on May 28, 2026 at 4:49 pm

    Perhaps what you mean is that you want to disable Nagle’s Algorithm in which case the solution is:

    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (int[]){1}, sizeof(int));
    

    Edit: Hmm, it looks like you want more than this, and I doubt what you want is possible without designing your own protocol on top of UDP.

    Edit 2: You may be able to get an effect similar to what you want by limiting the send and receive buffer sizes. The server (sender) should do:

    setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (int[]){YOUR_BUF_LIMIT}, sizeof(int));
    

    and the client (receiver) should do:

    setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (int[]){YOUR_BUF_LIMIT}, sizeof(int));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the brainstorming process for a system that will be used to write
So... I used to think that when you accessed a file but specified the
I think that it used to the be the case that in Liferay 4,
I've used ConfigParser for quite a while for simple configs. One thing that's bugged
I'm trying to write a simple generator that uses an expression tree to dynamically
I used to work in JavaScript a lot and one thing that really bothered
We used to use SourceSafe, and one thing I liked about it was that
I currently don't know any thing about web services, except that they are used
Is there a "very bad thing" that can happen &&= and ||= were used
Yesterday I used jQuery UI for the first time and I think I'm going

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.