I have a sockets projects using tcp/ip and it is structured like this:

class Socket {
   int desc;
   Socket(); // create socket here
}:

class TCPSocket: public Socket {
public:
   int send();
   int recv();
};

class ClientSocket: public TCPSocket {
public:
   void connect();
};

class ServerSocket: public Socket {
public:
   void bind();
   void listen():
   TCPSocket* accept();
};

My question is,it is posible to create in the same program a ServerSocket to listen for incoming connections,and a ClientSocket to client to a specific address:

int main()
{
	ServerSocket s;
	s.Bind();
	s.Listen();
	while(true)
	{
		TCPSocket* sock = s.Accept();
	        //code here
		ClientSocket c;
		c.Connect(address_here, port_here);
	}
	return 0;
}

Because everytime i do this it doesnt connect,it waits for a few seconds,and then Connect() gives me error with the description "No Error".If i create only a ClientSocket and connect directly,it works.So can someone help me understand what is wrong,and how to resolve this.

Thanks in advanced.

Edited 5 Years Ago by vbx_wx: n/a

I believe it would be possible.

From memory the server socket has to accept the connection after the client tries to connect not before as you have coded in your example.

I see what you are saying,the ideea is to work like a proxy,so a client connects to ServerSocket and then the ServerSocket (proxy) will connect to another server. Thats why i am wondering if i can use this code.

That's definitely possible but you'll need another client socket to connect to the other server and then feed the response from the new client to the original client.

I suggest you split your application up into three, the client, proxy and server. If the proxy is the software you're trying to build you could use a web browser as the client and a web server as the server so you can test your proxy without having to write those applications too.

Your proxy would then contain two sockets. A client socket to connect to the web server and a server socket for the web browser to connect to. The proxy would forward any data it receives from the web server on its client socket out its server socket to the web browser.

Edited 5 Years Ago by james.newell: n/a

My question is,it is posible to create in the same program a ServerSocket to listen for incoming connections,and a ClientSocket to client to a specific address:

Something I've been expermenting with these past few days. I got this working by running server/client classes in there own threads & used mutex for synchronization of any data.

The only problem have been experiencing so far is that if the client drop connection & tries to reconnect it's “unable to bind to the socket” again even when socket clean up & reset. Server class runs ok.

Hope this helps.

@ziggystarman: thanx for the suggestion i will try your way.

@james.newell: for the client i am using a program called Proxifier that will intercept every connection to the net,so if i browse a webpage it will be intercepted by this client and sends to me,the proxy the webpage address and i am trying to connect to that webpage. I made it to connect but then i remain stuck didnt know how to receive bytes from it and sent them along to the client

Edited 5 Years Ago by vbx_wx: n/a

Something like this ?

void* serverThread(void* sock)
{
	Handle handle((TCPSocket*)sock);
	handle.HandleAuthentication();
}

void* clientThread(void* sock)
{
	TCPSocket(some_address, some_port);
}

int main()
{
	unsigned short echoServPort = 9898;

	try {
		TCPServerSocket servSock(echoServPort);

		for (;;) {   // Run forever
			TCPSocket* sock = servSock.accept();
			pthread_t pidClient, pidServer;
			if(pthread_create(&pidServer, NULL, serverThread, (void*)sock) != 0)
			{
				throw SocketException("Unable to create server thread (pthread_create()", true);
			}
			if(pthread_create(&pidClient, NULL, clientThread, NULL) != 0)
			{
				throw SocketException("Unable to create client thread (pthread_create()", true);
			}
		}
	} catch (SocketException &e) {
		cerr << e.what() << endl;
		exit(1);
	}
	return 0;
}

I'm having problem making serverThread execute before clientThread.Because i need to get address and port from serverThread,so i need to wait to finish before executing clientThread. I dont know if this is good:

void* serverThread(void* sock)
{
	sem_wait(&mutex);
	Handle handle((TCPSocket*)sock);
	handle.HandleAuthentication();
	sem_post(&mutex);
	sem_post(&mutex2);
}

void* clientThread(void* sock)
{
	sem_wait(&mutex2);
	TCPSocket(Handle::getAddress(), Handle::getPort());
	sem_post(&mutex2);
}

int main()
{
	sem_init(&mutex, 0, 1);
	sem_init(&mutex2, 0, 0);
	unsigned short echoServPort = 9898;

	try {
		TCPServerSocket servSock(echoServPort);

		for (;;) {   // Run forever
			TCPSocket* sock = servSock.accept();
			pthread_t pidClient, pidServer;
			if(pthread_create(&pidServer, NULL, serverThread, (void*)sock) != 0)
			{
				throw SocketException("Unable to create server thread (pthread_create()", true);
			}
			if(pthread_create(&pidClient, NULL, clientThread, NULL) != 0)
			{
				throw SocketException("Unable to create client thread (pthread_create()", true);
			}
		}
	} catch (SocketException &e) {
		cerr << e.what() << endl;
		exit(1);
	}
	return 0;
}

You'll need to read/receive data from the web server and then write/send data to the proxifier client. I'm not sure what socket library you're using but it should be pretty similar. Reading a tutorial for your chosen library might help you out.

You don't need separate threads for your server and client sockets. You can just read from the web server into a string and send that string to the proxifier client and then repeat until the web server closes the connection.

Basically in pseudocode:

ServerSocket.bind();
ServerSocket.listen();

while (true) {

[proxifier connects]
ServerSocket.accept();

ClientSocket.connect(web server address and port);
ClientSocket.send(HTTP GET request);

while (true) {
data = ClientSocket.receive();
if (data == "") {
break;
}
ServerSocket.send(data);
}

ClientSocket.close();

}

ServerSocket.close();

Sorry i had some problem with the internat past 2 days. So when i want to read data from the browser i first need to send a HTTP GET request ?

Edited 5 Years Ago by vbx_wx: n/a

I've done something like this:

int main()
{
#ifdef WIN32
	Initialize();
#endif
	ServerSocket s;
	s.Bind();
	s.Listen();
	while(true)
	{
		TCPSocket* sock = s.Accept();
		Handle handle(sock);
		handle.HandleAuthentication();
		ClientSocket client;
		string addr = handle.getAddress();
		short prt = handle.getPort();

		client.Connect(addr, prt);
		char data[5000];
		client.Send("GET /index.html HTTP/1.0\r\n\r\n", 28) ;
		while(true)
		{
			int numbytes=client.Recv(data, 5000);
			if(numbytes == 0)
			{
				break;
			}
			cout <<"Received from web server: " << numbytes << endl;
			int numbytes2 = sock->Send(data, numbytes);
			cout << "Sent to client: " << numbytes2 << endl;
		}
		client.Close();
	}
	s.Close();
	return 0;
}

But when I acces for example google,i get this:

302 Moved
The document has moved here.

So still i can browse the net with the proxy on. I am doing something wrong? Thanks.

The problem is with the HTTP request,i don't know if GET /index.html HTTP/1.0\r\n\r\n is just necesary,i try to send User agent too,still no change.

This article has been dead for over six months. Start a new discussion instead.