*BSD News Article 56497


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!news.unimelb.EDU.AU!munnari.OZ.AU!news.ecn.uoknor.edu!fizban.solace.mh.se!vampire.xinit.se!newsfeed.tip.net!news.seinf.abb.se!inquo!hookup!news.nstn.ca!nds.fonorola.net!digdon
From: digdon@nds.fonorola.net (Mike Digdon)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Reading from sockets
Date: 10 Dec 1995 01:32:00 GMT
Organization: Nova Scotia Technology Network
Lines: 37
Message-ID: <4addag$m8v@news.nstn.ca>
NNTP-Posting-Host: nds.fonorola.net
X-Newsreader: TIN [version 1.2 PL2]

I'm trying to do some socket reading, and I'm a little distressed by the
fact that my read statement is only reading 100 bytes at a time when I
explicitly tell it to read 1024.

I open the socket, bind and listen to it and wait for connections (via
accept).  Here's the important part:

while (TRUE) {
	client_length = sizeof(client_addr);

	if ((clientfd = accept(serverfd, (struct sockaddr *)&client_addr,
		&client_length)) < 0)
                        error("server: accept error");

        do {
		bzero(buffer, sizeof(buffer));

                if ((rval = read(clientfd, buffer, 1024)) < 0)
                        perror("server: can't read from stream");
                else if (rval == 0)
                        printf("Ending connection\n");
                else
                        printf("%s\n", buffer);
        } while (rval > 0);

        close(clientfd);
}

buffer is defined as char buffer[1024].

When I send in a line of greater than 100 characters, it gets split up onto
2 separate lines, with read reading in only 100 bytes at a time.  What's up
with this?

--
       Mike Digdon # Network Support Specialist # iSTAR internet inc
           Phone: +1 613 780-2200 # E-mail: support@fonorola.net