LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-24-2010, 11:28 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
Problem with recv() in c++


Hello,i want to send a file using TCP/IP...send() seems to work,but recv() doesnt,here is my code:

Code:
void Wsk::sendfile(string path)
	{
		char* buffer = new char[5];
		ifstream in(path.c_str() , ios::binary);
		while((in.read(buffer , 5).eof()) == false)
		{
			cout <<::send(sock , buffer , 5 , 0);
		}
		delete [] buffer;
	}

	void Wsk::receivefile(string path)
	{
		char* buffer = new char[5];
		int byte_recv = 0;
		ofstream in(path.c_str() , ios::binary);
		while((byte_recv = ::recv(sock , buffer , 5 , 0)) > 0)
		{
			cout <<byte_recv;
			in.write(buffer , 5);
		}
		delete [] buffer;
	}
################# CLIENT ######################################
int main()
{

	try {
		Client xx("127.0.0.1" , 2896);

		string msg;
		try {
			while(1)
			{
				xx.sendfile("/home/vBx/testfile");
			}
		}catch(ExcepClass&) {}
		xx.sendfile("/home/vBx/testfile.txt");
	}catch(ExcepClass& m) {
		cout << m.error() << endl;
	}
}

###################### SERVER ########################################

int main()
{
	string msg;
	try {
			Server x(2896);
			while(1)
			{
				Server ser;
				x.accept(ser);
			try {
				while(1)
				{
			x.receivefile("home/vBx/recvtestfile.txt");
					
				}
			}catch(ExcepClass&) {}
			}
	}catch(ExcepClass& m) {
		cout << m.error() << endl;
	}
}
the client prints 5555555555555555555555555555555555555.....so its sending the chunks of data....but in recievefile recv return -1,so its not working.....anyone knows what am i doing wrong ? thank you
 
Old 02-24-2010, 01:02 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by vbx_wx View Post
...anyone knows what am i doing wrong?
Check the value of "errno" after the failed call to "recv" and print the error string using "strerror". You will have to include "errno.h" and "string.h".
 
Old 02-24-2010, 05:13 PM   #3
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
i am getting this : Transport endpoint is not connected...........
 
Old 02-24-2010, 10:23 PM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by vbx_wx
i am getting this : Transport endpoint is not connected...........
Try searching the error message "Transport endpoint is not connected" on Google. I did the same (for you) and found that many people have faced this before !! You can have a look at their situations and may be you'll get a hint !
 
Old 02-25-2010, 04:03 AM   #5
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
i did that already...but i still don't have a clue what to do..i didnt find nothing related to my program...some people just used a command in the terminal but they were using solaris or something,i use fedora,and it seems it didnt work for me
 
Old 02-25-2010, 04:10 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by vbx_wx
i did that already...but i still don't have a clue what to do..i didnt find nothing related to my program...some people just used a command in the terminal but they were using solaris or something,i use fedora,and it seems it didnt work for me
U mean that command didn't execute on your system or it didn't have the effect after execution ?
 
Old 02-25-2010, 04:18 AM   #7
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
i didnt execute on my sistem...but i found a similar thread here with this error and someone said :
The problem is the wait = yes option. For TCP connections xinetd seems to try and pass sockets around, instead of starting the server and redirecting STDIN/STDOUT.

Set wait = no for TCP streams. This is what I did to fix this exact same problem I was having. Below is my final configuration in /etc/xinetd.d/myservice:

but in /etc/xinetd i dont have myservice to change that....
 
Old 02-25-2010, 04:28 AM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Post the link to that thread here
 
Old 02-25-2010, 04:55 AM   #9
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
http://www.linuxquestions.org/questi...nnected-37633/
 
Old 02-25-2010, 06:24 AM   #10
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
i found this : http://www.troubleshooters.com/codecorn/sockets/
i made everything he said there...still getting the error............
 
Old 02-25-2010, 06:31 AM   #11
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
That new link says you create a file in /etc/xinetd.d.

If you followed that, put your created file here in [CODE] tags .
 
Old 02-25-2010, 06:47 AM   #12
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
these is teh process in /etc/services:

Code:
vbxsock         4196/tcp                # my client server program
and this is the file in /etc/xinetd.d/

Code:
# default: on
# description: vBx client server program
service vbxsock
{
   port            = 4196
   socket_type     = stream
   wait            = no
   user            = vBx
   server          = /home/vBx/workspace/server/src/server.cpp
   log_on_success  += USERID
   log_on_failure  += USERID
   disable         = no
}
i changed the root from "vBx" to "root" still not working,and the port is not used in /etc/services ..what did i do wrong ?
 
Old 02-25-2010, 09:21 AM   #13
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
i solved that error....in main() i was using the old socket to receive the file instead of the new one from accept() function,stupid me...thanx for the replies
 
Old 02-25-2010, 10:28 PM   #14
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by vbx_wx
i solved that error....in main() i was using the old socket to receive the file instead of the new one from accept() function,stupid me...thanx for the replies
It is good something worked for you !

Could you now post the working code here for other newbie's to see if they face the same problem ?
 
Old 02-26-2010, 04:07 AM   #15
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
Client:
Code:
        void Wsk::sendfile(string path)
	{
		char* buffer = new char[5];
		ifstream in(path.c_str() , ios::binary);
		while((!in.read(buffer , 5).eof()))
		{
			cout <<::send(sock , buffer , 5 , 0);
		}
		delete [] buffer;
	}

int main()
{

	try {
		Client xx("127.0.0.1" , 4196);

		string msg;
		xx.sendfile("/home/vBx/testfile");
	     }catch(ExcepClass& m) {
		    cout << m.error() << endl;
	}
}
Server:
Code:
void Wsk::receivefile(string path)
	{
		char* buffer = new char[5];
		int byte_recv = 0;
		ofstream in(path.c_str() , ios::binary);
		while(((byte_recv = ::recv(sock , buffer , 5 , 0)) > 0))
		{
			cout <<byte_recv;
			in.write(buffer , 5);
		}
		delete [] buffer;
	}

int main()
{
	string msg;
	try {
			Server x(4196);
			while(1)
			{
				Server ser;
				x.accept(ser);
			ser.receivefile("/home/vBx/recvtestfile.txt");		
			}
	}catch(ExcepClass& m) {
		cout << m.error() << endl;
	}
}
 
1 members found this post helpful.
  


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
Urgent---TSclient Problem -Error: recv:Connection Reset by Peer? teluguswan Linux - Networking 5 10-12-2007 01:52 AM
pthreads and recv sayarsoft Linux - Kernel 2 08-11-2006 04:06 AM
Can't recv() Ephracis Programming 2 01-04-2005 02:49 PM
recv() buffer linuxanswer Programming 1 03-22-2004 12:11 PM

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

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