[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::digital_unix

Title:DIGITAL UNIX(FORMERLY KNOWN AS DEC OSF/1)
Notice:Welcome to the Digital UNIX Conference
Moderator:SMURF::DENHAM
Created:Thu Mar 16 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:10068
Total number of notes:35879

9299.0. "Can't read more than 8192 bytes from a socket" by ZYDECO::REDDY () Wed Mar 26 1997 11:20

I have the following two programs, server.c and client.c, communicating using
sockets.  The client program sends 10000 bytes of data, but the server only
receives 8192.  If the client sends less than 8192, all the data comes in.

Am I hitting some sort of limit?  I have a customer who wants to send large
amounts of data and wants to know if there are any limits.  

To compile and run:

	cc -o server server.c
	cc -o client client.c
	
	./server &
	./client

I am running this on digital UNIX 4.0b.

Thanks,

Sumithra

/*** Server.c***/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/un.h>

main()
{
	int                     stat;
	int 			sockfd;
	int                     ssz;
	int 			newsockfd;
	struct sockaddr_un	serveraddr;
	char                    mesg[10000];

	if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		perror("socket_create");
		exit(1);
	}

	bzero((char *) &serveraddr, sizeof(struct sockaddr_in));
	serveraddr.sun_family      = AF_UNIX;
	strcpy (serveraddr.sun_path, "sumithra");

	if ( bind(sockfd, 
	    (struct sockaddr *)&serveraddr, 
	    sizeof(struct sockaddr_in)) < 0) {
		perror("socket_bind");
		exit(2);
	}

	listen(sockfd, 8);

	if ( (newsockfd = accept(sockfd, &serveraddr, &ssz)) == -1) {
		perror("RECEIVER: Accept");
		return(-1);
	}

	stat= read(newsockfd, mesg, sizeof(mesg));
	printf (" BYtes read is :  %d\n", stat);
	printf("Data read = %s\n", mesg);

	shutdown (sockfd,SHUT_RDWR);
	close (sockfd);
	shutdown(newsockfd,SHUT_RDWR);
	close(newsockfd);
	return(0);
}

/*** Client.c ***/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/un.h>

main(int argc, char *argv[])
{
	int 			sockfd;
	struct sockaddr_un	serveraddr;
	struct hostent		*he;
	char DATA[10000];
	int stat;

	if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		perror("socket_create");
		exit(1);
	}

	serveraddr.sun_family      = AF_UNIX;
	strcpy (serveraddr.sun_path, "sumithra");

	if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0) {
		perror ("connect");
		exit(2);
	}

	memset(DATA,'a',10000);
	stat = write(sockfd, DATA, sizeof(DATA));
	printf ("Data sent is, %d\n" , stat);

	shutdown(sockfd, SHUT_RDWR);
	close(sockfd);
	exit(0);
}

    
T.RTitleUserPersonal
Name
DateLines
9299.1COL01::LINNARTZWed Mar 26 1997 11:266
    without trying the program, have you ever tried a second read
    or perhaps a loop until all data is red. 
    other possibilities would be to enlarge buffers, but this
    would be a chicken and egg if you only want one read command.
    
    Pit
9299.2COL01::LINNARTZWed Mar 26 1997 11:274
    oops, forgot to mention there are examples
    in /usr/examples/network_programming. 
    
    Pit
9299.3VAXCPU::michaudJeff Michaud - ObjectBrokerWed Mar 26 1997 11:568
>     without trying the program, have you ever tried a second read
>     or perhaps a loop until all data is red. 

	this is the correct answer.  .0 does a single read.

	also .0 should make sure they understand that TCP is a STREAM
	protocol and has no concept as messages at the user level.
	Ie. you data stream has to be self-describing.
9299.4ZYDECO::REDDYWed Mar 26 1997 15:579
    
    Thank you both for the reply.  When I did a second read, I got rest of
    the data.  Using the setsocketopt, I set the SO_RCVBUF to 20000, but
    still I could only get 8192 bytes.
    
    Does this mean that a read is limited to 8192 bytes max?
    
    Sumithra
    
9299.5netrix.lkg.dec.com::thomasThe Code WarriorWed Mar 26 1997 17:102
No.  It means that read returns you the amount of data that is available
at the time of the read.  If more was available it would have been returned.
9299.6VAXCPU::michaudJeff Michaud - ObjectBrokerWed Mar 26 1997 20:3513
>     Thank you both for the reply.  When I did a second read, I got rest of
>     the data.  Using the setsocketopt, I set the SO_RCVBUF to 20000, but
>     still I could only get 8192 bytes.

	try setting the low-water mark to how much data you are expecting
	(and it should not be a value >SO_RCVBUF).  not sure if this
	works for read/recv, if not, you can do a select() first
	(after setting the low-water mark).

	of course setting the low-water mark to higher than what the
	sender actually sent will cause you to block until the
	sender sends some more (if they are waiting for a response
	from you then you've deadlocked).
9299.7Thanks. ZYDECO::REDDYThu Mar 27 1997 17:011