LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-13-2009, 08:40 AM   #1
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
recv unable to receive first to characters. REALLY head ditching!!!!!!


Hi,


OS:red flag
language: "C"
I wrote a socket server program, in which receive function is like this..

Code:
char* receive(int connected, char recv_data[])
{

        unsigned int len, bytes_received;

        bytes_received = recv(connected,(char *)&len,6,0);//receive msglength

        memset(recv_data, 0, sizeof(recv_data));

        unsigned int h_len = ntohl(len);

        printf("received lenth is %d\n", h_len);

        char *recvbufptr = recv_data;

        while(1)
        {
                recv:bytes_received = recv(connected, (char*)recvbufptr, h_len ,0);//receive msg
                printf("BYTES RECEIVED IS %d\n H_LEN IS %d\n   bufptyr%s\n", bytes_received, h_len, recvbufptr);

                if(bytes_received == h_len)
                {
                        printf("INSIDE IF\n");
                        break;
                }
                else
                {
                        if((bytes_received == 0) ||(bytes_received == -1))
                        {
                                perror("Recv Fails:");
                                exit(1);

                        }
                        goto recv;
                        printf("INSIDE ELSE \n");
                        h_len = h_len-bytes_received;
                                                                       recvbufptr = recvbufptr+bytes_received;
                }
        }//while

        printf("bytes received is %d\n", bytes_received);


        recv_data[bytes_received] = '\0';

#ifdef DEBUG
        printf("\n RECIEVED BUFFER LENGTH = %d\n" ,bytes_received);
#endif
        printf("\n RECIEVED DATA = %s\n " , recv_data);

return recv_data;
}


I have to communicte with windows in which client is running. the problem is - sometime the first two characters of xml message is are not receiving. pls suggest except using "MSG_WAITALL" flag.

I raised this question many tiomes, but i dint get good answer.. this is the problem from 3 months. I hope this time I will get the ans.

Thankx in Advance
 
Old 08-13-2009, 11:46 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by naveenisback View Post
Hi,

OS:red flag
language: "C"
I wrote a socket server program, in which receive function is like this..

I have to communicte with windows in which client is running. the problem is - sometime the first two characters of xml message is are not receiving. pls suggest except using "MSG_WAITALL" flag.

I raised this question many tiomes, but i dint get good answer.. this is the problem from 3 months. I hope this time I will get the ans.

Thankx in Advance
Well, you got answers in your other threads about this same issue, but never said if they worked or not. And you have one solution, which you mention, but say you don't want it.

Don't know what else to suggest....except for you to spell out your words, and write/spell clearly.
 
Old 08-16-2009, 12:41 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
For starters, fix this statement:
Code:
bytes_received = recv(connected,(char *)&len,6,0);//receive msglength
to be:
Code:
bytes_received = recv(connected,(char *)&len,sizeof(len),0);//receive msglength
Your original code is indicating that you want to receive 6 bytes, and store them into a 4-byte entity. Silly mistakes like this are the reason that hard-coded constants are shunned in programming.
 
Old 08-17-2009, 07:51 AM   #4
naveenisback
Member
 
Registered: Jun 2009
Posts: 80

Original Poster
Blog Entries: 1

Rep: Reputation: 16
Thanks

Thanks a lot Mr.dwhitney67.

It is working well.
 
  


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
recv doesn't receive everything it should jody.xha Linux - Networking 1 01-27-2009 03:00 AM
Beating my head against the email wall - cannot receive email dazed-and-confused Linux - Server 16 09-02-2008 05:14 AM
Unable to receive external emails samnjugu Linux - Server 11 05-03-2008 04:46 PM
Unable to send/receive email newlinuxgeek Linux - Software 2 01-10-2005 09:07 AM
unable to receive incoming connection munisp Linux - Newbie 3 11-07-2001 12:39 PM

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

All times are GMT -5. The time now is 11: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