LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-26-2008, 01:40 AM   #1
tqa
LQ Newbie
 
Registered: Jun 2006
Posts: 24

Rep: Reputation: 15
sock.send question


my code looks as below:

Code:
cout<<"Sending List of Node..\n";
    //code to send List1 and List2
    sckt::byte data[256];
	 for(int g=c; g>=0; g--)
	     {
              arraylist2[g]=d.Data(path2[g]);
	      //sckt::byte data[256];
	      strcpy( reinterpret_cast <char*> (data), arraylist2[g].c_str());
	      sock.Send (data, sizeof (data));
             }
the code can run well, but the output is not as I thought it would be.
only value of g=c are send.

is it the sock.send only run once?
that means even we do the for loops, the sock.send only run once then it will terminate?

hope my question is clear.
Thank you
 
Old 11-26-2008, 06:17 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I recognise the namespace is this http://code.google.com/p/sckt/ ?
What type of socket is it UDP or TCP? what is the value of C? is it 0?
Why are you copying the data and not just sending it like so
Code:
 sock.Send (arraylist2[g].c_str(), arraylist2[g].size() );
If it is a problem with the library you can contact the author via the google project page or on <a href="http://www.gamedev.net/community/forums/profile.asp?mode=display&id=99059">gamedev.net</a>

Last edited by dmail; 11-26-2008 at 06:23 PM.
 
Old 11-26-2008, 09:03 PM   #3
tqa
LQ Newbie
 
Registered: Jun 2006
Posts: 24

Original Poster
Rep: Reputation: 15
I am using the TCP socket. I think it is not the problem with the library since I only use the sckt function from the site that u mention ( but the file that I found is sckt-0.4), and I have add a few code to suit my program.

Actually I thought that we have to use the variable to send a data, so my intention is to replace the var data with my value.

The c value is an integer which > 0.

Anyway, I have try the code that you have suggested, but i get this error

error: invalid conversion from ‘const char*’ to ‘const sckt::byte*’
error: initializing argument 1 of ‘sckt::uint sckt::TCPSocket::Send(const sckt::byte*, sckt::uint)’
 
Old 11-26-2008, 11:58 PM   #4
tqa
LQ Newbie
 
Registered: Jun 2006
Posts: 24

Original Poster
Rep: Reputation: 15
Lightbulb

Anyway, this is my code for client side.
I am not using the for loops for at the client side because I thought all the value will be stored in buf..


Code:
 sckt::byte buf[1024];

    sock.Recv(buf, sizeof(buf));
    cout << buf <<endl;
Is it my code incorrect?
 
Old 11-27-2008, 05:09 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by tqa View Post
I am using the TCP socket. I think it is not the problem with the library since I only use the sckt function from the site that u mention ( but the file that I found is sckt-0.4), and I have add a few code to suit my program.

Actually I thought that we have to use the variable to send a data, so my intention is to replace the var data with my value.

The c value is an integer which > 0.

Anyway, I have try the code that you have suggested, but i get this error

error: invalid conversion from ‘const char*’ to ‘const sckt::byte*’
error: initializing argument 1 of ‘sckt::uint sckt::TCPSocket::Send(const sckt::byte*, sckt::uint)’
Change your call to Send() as follows:
PHP Code:
sock.Send ((const sckt::byte*)arraylist2[g].c_str(), arraylist2[g].size() ); 
P.S. You should read the sckt.hpp file so that you further understand how the sckt::byte and sckt::uint are defined. I'm not quite sure I understand why the author of the sckt library thought this was important to define (as opposed to using standard system types), but nevertheless he did.
 
Old 11-27-2008, 05:39 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by tqa View Post
Anyway, this is my code for client side.
I am not using the for loops for at the client side because I thought all the value will be stored in buf..


Code:
 sckt::byte buf[1024];

    sock.Recv(buf, sizeof(buf));
    cout << buf <<endl;
Is it my code incorrect?
This code is correct, however it *may* only work for short messages. When using TCP sockets, the delivery of the message is guaranteed... however not necessary in one packet!

Thus you will need to insert your Recv() method in a loop to continue receiving data until a condition has been met (i.e. a complete message has been received). Pay careful attention to the return value of Recv() so that you can use it to adjust the insert position within your buf array.

I have not compiled the following, so think of it as "pseudocode":
PHP Code:
sckt::byte buf[1024];

// assume messages are newline terminated
sckt::uint bytesReceived 0;

while (!
strchr(buf'\n') && (bytesReceived sizeof(buf))
{
  
bytesReceived sock.Recv(buf bytesReceivedsizeof(buf) - bytesReceived);

P.S. If you are developing for Linux-only, feel free to consider using the TCP/UDP Socket library I developed (it is very similar to the sckt library you are currently using). It is located here.

Last edited by dwhitney67; 11-27-2008 at 06:08 AM.
 
Old 11-27-2008, 06:24 AM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
"that means even we do the for loops, the sock.send only run once then it will terminate?"
Is the socket set to non blocking and when you say the above does the application end due to an exception or are you just referring to the loop exiting? Try wrapping the code in a try/catch block.
 
Old 11-27-2008, 07:14 PM   #8
tqa
LQ Newbie
 
Registered: Jun 2006
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks for the great ideas
I have tried the send code. It work well..but still cant receive all the data that i want to send.

then, i try below code, as suggested by you.

Code:
sckt::byte buf[1024];

// assume messages are newline terminated
sckt::uint bytesReceived = 0;

while (!strchr(buf, '\n') && (bytesReceived < sizeof(buf))
{
  bytesReceived = sock.Recv(buf + bytesReceived, sizeof(buf) - bytesReceived);
}
error: call of overloaded ‘strchr(sckt::byte [1024], char)’ is ambiguous
/usr/include/string.h:168: note: candidates are: char* strchr(const char*, int) <near match>
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/cstring:107: note: char* std::strchr(char*, int) <near match>

Actually my program have been run well using this code on client side

Code:
int a = 4;
    cout<<"List1:"<<endl;
    for (int b=a; b>0; b--)
    { 
      sock.Recv(buf, sizeof(buf));
         cout << buf <<endl;       
    }*/
though i think it is not a good code because im defining the value for looping.
And I think that your suggested code is very efficient and I want to use that code.BUt, it have errors.hope that you can help me.

anywaY, I already get the page that you suggested, but can't open it,
The requested URL /SocketLibrary-1.05.tgz was not found on this server.
Thank you

Last edited by tqa; 11-27-2008 at 07:16 PM.
 
Old 11-27-2008, 08:34 PM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by tqa View Post
Thanks for the great ideas
I have tried the send code. It work well..but still cant receive all the data that i want to send.

then, i try below code, as suggested by you.

Code:
sckt::byte buf[1024];

// assume messages are newline terminated
sckt::uint bytesReceived = 0;

while (!strchr(buf, '\n') && (bytesReceived < sizeof(buf))
{
  bytesReceived = sock.Recv(buf + bytesReceived, sizeof(buf) - bytesReceived);
}
error: call of overloaded ‘strchr(sckt::byte [1024], char)’ is ambiguous
/usr/include/string.h:168: note: candidates are: char* strchr(const char*, int) <near match>
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/cstring:107: note: char* std::strchr(char*, int) <near match>
It would seem that you need to cast the buf to a const char* in the strchr() function.

Quote:
Originally Posted by tqa View Post
anywaY, I already get the page that you suggested, but can't open it,
The requested URL /SocketLibrary-1.05.tgz was not found on this server.
Thank you
Sorry for the URL issue; it has been corrected.
 
  


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
dbus-send question. HarryBoy Linux - Newbie 3 02-02-2010 11:39 PM
Postfix : mail cannot send to send outside ( can send/receive locally) bobbinsupport Linux - Networking 3 12-15-2007 10:40 PM
just a very small mysql.sock question =) xushi Slackware 12 09-23-2004 10:27 AM
how to send data from winsock to unix sock husniteja Programming 6 08-25-2004 06:40 AM
Sendmail question How to send email from command line anjaan Linux - General 1 07-08-2004 08:20 PM

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

All times are GMT -5. The time now is 02:21 AM.

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