LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2005, 05:53 PM   #1
ashucool83
LQ Newbie
 
Registered: Sep 2005
Posts: 11

Rep: Reputation: 0
recv() function of SOCKET


hello

I am trying to send a file from a client to a server using SOCKETS
hence I send up 3 peices of information
1) Command [2 bytes]
2) File Size [4 bytes]
3) File Data [In sets of 1024 bytes, if file > 1024]

Code:
char command[] ="A";
char fileDataBuffer[] ="My Name is bla bla asnd an asdkbgas asd asdk asmbdas sdasd ad w  adw   dada  ad a d  ad a  a d ad a d   ad  ad a d a d  ad a d a d a dadadasda   a d a e kahdi ajshgd hasd jhaf ajshgd kasud  awd , asduayd";
	sendByte.size = sizeof(fileDataBuffer);

int len1,len2,len3;

	if( (len1 =	send(sd, command, sizeof (command), 0)) < 0) {
		perror("command: ");
	}

    if( (len2 =send(sd, sendByte.array, sizeof (sendByte), 0)) < 0 ) {
		perror("file size: ");
	}

	if( (len3 =	send(sd, fileDataBuffer, sizeof (fileDataBuffer), 0)) < 0) {
		perror("data: ");
	}



	//cout << "Bytes: " << sendByte.size << endl;
	cout <<"Bytes transferred successfully are: " << fileDataBuffer <<endl;
	cout <<"Len1 send(): " << len1 <<endl;
	cout <<"Len2 send(): " << len2 <<endl;
	cout <<"Len3 send(): " << len3 <<endl;
Code:
//increasing the buffer size of sockets
    setsockopt(s,SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));


  	saun.sin_family = AF_INET;
  	saun.sin_port = htons(4444);
  	saun.sin_addr.s_addr = INADDR_ANY;

    if (bind(s,  (struct  sockaddr *)&saun, sizeof(saun)) < 0) {
        perror("server: bind");
        printf("This Suks\n");
        exit(1);
    }


while(1){

    if (listen(s, 5) < 0)
    {
        perror("server: listen");
        exit(1);
    }

    printf("Send a Request ... \n");
    if ((ns = accept(s, (struct sockaddr *)&saun, &fromlen )) < 0)
    {
        perror("server: accept");
        exit(1);
    }

  recv_len1=recv(ns,&buf,sizeof(buf),0);
           

    printf("Print 1: %i\n", recv_len1);
    printf("Recieved 1:%s\n", buf);


    switch(buf[0])
    {
		case 'A' :
		case 'a': getFileFromClient(s,ns);
			      break;
		    default:
	             printf("ERROR IN SWITCH");

	}

}//end while

   close(s);
   exit(0);
}

void getFileFromClient(s,ns)
{
	int recv_len2;
	int recv_len3;
    int recv_len4;
    int cmdSize = 4;
    char buf2[4];
    int temp =0;
    int test = 0;

	char fileBuffer[BUFFERSZ];
    char fileBuffer2[BUFFERSZ];		 
   do {
          if( (recv_len2=recv(ns,&getData.array,sizeof(getData),0)) < 0) {
          perror("RECV size:");
          }    
    }while(recv_len2 != 4);	 
	 
 while(temp<getData.size)	 {
	 	if( (test = recv(ns,&fileBuffer,sizeof(fileBuffer),0)) < 0) {
	 	 perror("recv() ");
	 	 }	 	
	 	temp+=test;
	 	printf("DATA: %s\n", fileBuffer );
	 }
	 
	 printf("Print 2: %i\n", recv_len2);
	 printf("Recieved 2:%i\n", getData.size);
           
             //write buffer to file
             //writeToFile(fileBuffer, getData.size);     
}
The problem is that when I have the fileDataBuffer[] with data > 1400 bytes. I can successfuly recieve the content of the file. However if the size of that buffer is smaller like 200 bytes or so. The server does not even recieve the second send [the fileSize or bufferSize]. What am i doing wrong ??
 
Old 10-08-2005, 07:38 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
1. Have you tried "sockopt(TCP_NODELAY)"?

2. Does the same occur if you use TCP (reliable streams) vs UDP (unreliable datagrams)?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Help, Linux mysql nfs : Bind on unix socket: Function not implemented nmotte Linux - Networking 1 10-29-2005 07:58 AM
Can I get a socket from a function and use it on another function? elmafiacs Programming 2 10-14-2005 10:26 PM
A question about connect() function of socket realos Programming 4 07-27-2005 03:21 PM
problem with socket() function marri Programming 9 09-14-2004 07:59 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration