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.
Hello. I guess its not posibile to create 2 sockets in the same project no ? I have an socket that is listening and the other one tries to connect and my connect() function does nothing,looks like waiting for something.How can i use threads to make them bot work in th same project ? Can someone help me with a scenario how to implement this? Where to create the thread,what does the thread function do. I use C++. Many thanks.
It seems that you want someone to develop the entire project for you, whilst you sit back and enjoy the sunset.
What specific problems are you having with your project? And btw, it is possible to have more than one socket in use within an application. Employing threads (or multiple processes) is easiest, but not absolutely necessary.
Can you please post your code where you setup the "listen" socket, and the code where you setup the other socket that performs the connect().
"It seems that you want someone to develop the entire project for you, whilst you sit back and enjoy the sunset." I must say this was funny.
I am developing a proxy server,so i need to listen to a connection and then I made when accept()-ing the connection to call connect to the server that the client connected to me said.(for example,cleint connects to me and says: "connect to google on port 80" and i must connect to google.
One thing that I see is confusing is that the Socket() constructor creates a socket, and then Connect() does the same again (and yields a different socket descriptor).
From the high-level perspective, I would assume you have something like:
Code:
int main()
{
Socket listenSock;
listenSock.Bind();
listenSock.Listen();
listenSock.Accept();
...
}
I do not agree with your implementation of Accept(); this function should return a Socket object that represents the peer that has connected. For example:
Sorry, I read your reply with the code in haste; I was thinking that you had a class named Socket, but instead it seems that it is merely a function (perhaps part of a class?).
Anyhow, the common way to develop socket classes in an Object Oriented paradigm is to define a base-class for common socket operations into a Socket class, then specific operations applicable to the Server and Client into two other classes. I've done something similar to this:
The Socket constructor should create a socket, aptly using socket(). It's destructor should close it.
Functions such as Bind(), Listen(), and Accept() should be defined in TCPServerSocket, but not in the TCPClientSocket (since these do not apply). The Connect() function should be defined in TCPClientSocket.
Functions such as Send() and Recv() should be defined in TCPSocket.
The Accept() function should return a TCPSocket. TCPSocket should have two constructors; a no-arg constructor and another that accepts a socket descriptor (this one is used by TCPServerSocket). Both constructors should be declared as protected.
P.S. Consider defining parameters for Bind(), such as port and address.
Last edited by dwhitney67; 02-04-2011 at 08:30 AM.
Yes,ive done an implementation like this,but i lost my code,thats why now i made it simple,just a Server class.But how can i resolve my problem with 2 sockets created in the same program ?
edit: i will try to implement how you describe.i wil llet you know if i have problems.
edit: and then how is server gonna use send and recv functions ?
You do not use the Server socket (aka Listen socket) to send/receive data. It is merely used to accept connections.
The descriptor returned by ::accept() is used to construct a TCPSocket, which in turn, is used to send/receive data to/from the peer (client). The allocated TCPSocket is what is "managed" by the server. Whether you want to create a separate thread or separate process to manage the connected client, is purely up to you.
Other than that, you seem to be on your away to a good design.
Last edited by dwhitney67; 02-04-2011 at 08:43 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.