ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When i call Connect() in side HandleConnection() it returns -1 and when i set strerror(errno) it returns "No Error" with value 0. Anyone has an idee what am i doing wrong ?
When you kick off the thread, and then (almost) immediately perform the pthread_join(), you are blocking your main-thread from continuing until the child-thread completes. What is the point of that?
As for your thread, what is the purpose of HandleConnection(). You already have a socket that is "connected" to the external client. Think of it as a two-lane highway to the remote client. There's no need to setup yet another connection. The TCPSocket arg you have is quite capable of sending and receiving on it's own.
P.S. Forget for now programming for multiple platforms (i.e. Linux and Windows). Choose one, and stick with it. When you are completely done with everything, then focus on porting (to the other platform).
Ok,i see that there was no point to use thread,after removing it my error in connect is "Result too large". HandleConnection() is just a function for performing some exghange of packets between client and this server,and when its all good i want to connect from this to another server,me acting like a proxy server.
I forgot about Google translate,after reading that thread,one sugestion was to replace inet_addr with inet_aton(). After doing this,it gives me "Connection refused".
Are you attempting to put together a Server that serves as a data forwarder so that a Client can in essence send data to another Server? If you are, the first Server will need to be given (or it could be hard-coded) information on where to connect to the remote Server. And for now, I would push the multi-threading thoughts to the back-burner; worry about it later.
Consider something like:
Code:
int main(int argc, char** argv)
{
try
{
TCPServerSocket listenSocket;
listenSocket.bind(0, 12345); // bind socket to INADDR_ANY and port 12345
listenSocket.listen();
while (true)
{
TCPSocket* client = listenSocket.accept();
if (client)
{
handleClient(client);
}
}
//...
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
void handleClient(TCPSocket* client)
{
try
{
TCPClientSocket forwarder;
forwarder.connect("192.168.1.110", 54321); // attempt to establish foreign connection
bool connected = true;
while (connected)
{
char buf[1024];
int len = client->recv(buf, sizeof(buf));
if (len > 0)
{
forwarder.send(buf, len);
}
else if (len == 0)
{
std::cout << "Client disconnected." << std::endl;
connected = false;
}
else
{
// error attempting to receive data from the client
}
}
// forwarder will also disconnect when it falls out of scope.
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
delete client; // close client connection;
}
The code above assumes that your TCP Socket classes will augment their error handling to throw an exception should something fail. The alternative is to use C-style return codes (yuck!), and to check these after each call to bind(), listen(), accept(), connect(), etc.
It also assumes that the 'forwarder' will only send data; it will not receive. I would suggest that you study how to use select() so that you can handle both the client and the forwarder with regards to receiving data. Remember, recv() is a blocking call; you don't really want to call recv() on a socket unless you know for fact that their is data to be read. select() will assist you in determining whether there is data available.
Last edited by dwhitney67; 02-05-2011 at 10:26 AM.
Yes thats is what i am trying to do,but to take your example the line:
Code:
forwarder.connect("192.168.1.110", 54321);
gives me error. That is my problem,i do not know what it wont connect.
For example:
Code:
CLient ---> My Server ---> Server
A client connects to me,after that it enters the HandleConnection function,where the authetication is made by sending and receiving packets.The last packet i rceived from the Client contains an address(ip,or domain) and a port,witch i extract from the packet and call Connect() to establish a connection between me(My Server) and Server. Here it gives me error.
The return value of client.sin_addr.s_addr = inet_addr(address.c_str()); is -1 and it gives "Result too large". So i am sure that,there is my problem. But when i try in a separate program to call Connect("www.google.com", 80) it works with the same code. I dont get it why here it doesnt work
The only way that the connect() will work is if a Server is indeed listening at the foreign addr/port. Do you have that other server running? Can you confirm that it has called bind(), listen(), and accept()?
Well i want to connect to this: www.google.com. So while there server is up and running i guess it should work no ? Again, making a simple client to connect to google works,the Connect() function doesnt give any error. In my program trying to connect to google gives error.
edit: i take that back,it doesnt work even with a simple client, i was not testing good the return value from connect.
Google has port 80 open, thus there should be no reason why you should not be able to connect. Perhaps you need to revisit the code implemented in your Connect() function. Personally, I would have it call a function similar to the following to setup the sockaddr struct that is necessary for the ::connect() system library function.
void Connect(const string& address, const unsigned short port)
{
sockaddr_in sin;
if (fillAddress(AF_INET, address.c_str(), port, &sin) ||
::connect(desc, (sockaddr*)&sin, sizeof(sin)))
{
cout << "Error in Connect()" << strerror(errno) << endl;
// You need better error handling here; a mere cout is not enough.
}
}
Btw, once you connect to Google's port 80 (HTTP port), what do you plan to do then? Will you have a web-browser connect to your Server so that it's requests will be forwarded to Google?
Last edited by dwhitney67; 02-05-2011 at 11:38 AM.
Well i am using this client Proxifier,in his settings i can set where i want to connect.For example i put it on google,port 80,and there is an option Load a default web page from the host. I just wanna to this,so i can see it works.If i dont select that option with load a web page,the client programs says:
Code:
Connection to www.google.com:80 established through the proxy server
Kind if confusing,since i didnt even call Connect(). I' doing this for the first time,thats why i am a litlle confuse.
edit: btw your code doesnt give an error anymore,but its hard for me in linux since i dont have this program to test it,and in windows i dont have thoose functions you use.
Let me try again and explain,what i said before it was about proxy cheker.That works.
Ok,this Proxifier intercepts all connections made to the net.So if strat my program,and then proxifier,and lets say i open a webpage linuxquestions.org.The page will be intercepted by Proxifier,and my program prints whats is happening(the page will not open in browser,gives error):
Code:
You are connected !!!
-> Received 3 bytes (1st packet)
NO AUTHENTICATION REQUIRED !
<- Sending 2 bytes (1st packet)
-> Received 10 bytes (2nd packet)
Size of packet: 10
Version: 5
Connection type: 1
Address type: 1
Destination adrress: 209.34.245.100
Destination port: 80
<- Sending 10 bytes (2nd packet)
CONNETING TO :209.34.245.100 80
The proxifier display is this:
Code:
opera.exe - 209.34.245.100:80 open
Btw,i get error in the function only when i use that proxy checker,so lets forget about it
Now,when i test with the debugger i saw that it remains block after i call connect(),it just waits for something,after a few seconds after printing thoose messages with CONNETING TO :209.34.245.100 80 It gives me an error "Invalid argument" at coonect() then it retries again to connect and error again,so it continuosly tries to connect but gives error. I' really confusing you i know.Sorry.
edit: looking better i noticed that the first error is again Result too large and then it gives Invalid argument
edit2: ussing a separate functiin like yours fillAddr it returns -1 but says No Error.Just like at the begining
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.