
05-21-2008, 04:17 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
Sockets
I'm writing a program that I want the user to be able to add new connections to other people and listen in for other connections. Is there an easier way to do this.
__________________
My IP address is 127.0.0.1
|
|

05-22-2008, 05:46 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
I'm not clear what you expecting as an easier way. We can do in regular way we do,
normally I do it in this way.
|
Code:
|
Socket TestClient;
try {
TestClient = new Socket("Machine name", PortNumber);
}
catch (IOException e) {
System.out.println(e);
} |
|
|

05-22-2008, 06:30 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
the problem is that the other user than has to have a server socket waiting for a connection. Is there an easy way to have a socket that listens for someone to connect to it then make it a normal socket then create another socket that waits for another connection or is there an easier way altogether.
__________________
My IP address is 127.0.0.1
|
|

05-22-2008, 06:46 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
That means you want to open multiple sockets on one side. I think the easiest way to do this is through a thread. Rapidly checking on available connections, we can open existing one. I don't know there is another easy way to do this.
|
|

05-22-2008, 02:37 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
Can I have one socket dedicated to listening for connections to new people then when it finds someone have it make another socket equal to it then have the listening socket waiting for another new connection.
__________________
My IP address is 127.0.0.1
|
|

05-23-2008, 04:30 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
I don't think so. When a socket is communicating, how can it release without destroying the connection.
But I think if you can distribute the time intervals in communicating thread, you can use the socket as another listener. But it's not easy, the numbers of events can handle at a time can be few.
|
|

05-23-2008, 02:50 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Canada
Posts: 191
Rep Power: 2
|
|
|
Use a server socket. It does exactly what you want. This is how servers work. they listen for connection requests on one port and then create a connection on another port for the client and continue to listen on the original port.
|
|

05-23-2008, 04:00 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
I understand that much about server sockets, but what I want is a client to be able to add new connections from his computer and simultaneously be able to have connections connect to him. Is there a way to do this I'm very new to networking actually I'm just doing this as a way to learn how to use sockets. I have used server sockets and regular sockets before, but all it did was send over a string.
__________________
My IP address is 127.0.0.1
|
|

05-23-2008, 10:45 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
What if I made a "Socket" class that had two constructors. The class would have a Socket and a ServerSocket in it. Depending on which constructor I called it would set up the correct one. That way I could have a vector of "Sockets" that in the end where all the same thing. The last Socket in the Vector would be the socket listening in for a new connection. Would this work like I would like or what are your thoughts.
__________________
My IP address is 127.0.0.1
|
|

05-24-2008, 12:44 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Canada
Posts: 191
Rep Power: 2
|
|
|
Hi Zosden,
Before you do anything you have to have clear in mind what the end result is? What do you want to create? A peer to peer chat program? Each client is also a server? My question is how do you know how to find the clients? Usually every peer to peer network is based on some well known servers. The information has to be centralized somewhere. Look at the IRC protocol if you want to know more about chat applications. Everything else is just a refined copy.
|
|

05-24-2008, 02:50 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
This program is just for learning purposes. I just wanted to learn how to use sockets.
__________________
My IP address is 127.0.0.1
|
|

05-26-2008, 05:25 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
Is there away to do networking without working on the socket level.
__________________
My IP address is 127.0.0.1
|
|

05-27-2008, 04:27 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
How did you communicate without a socket. I don't think there is another way.
|
|

05-27-2008, 04:58 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
I was just wondering if it was possible
__________________
My IP address is 127.0.0.1
|
|

05-27-2008, 05:28 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
If I found something I'll let you know.
|
|

05-27-2008, 03:20 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Canada
Posts: 191
Rep Power: 2
|
|
|
In Java at higher level you can use RMI.
|
|

05-27-2008, 05:55 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
Rep Power: 2
|
|
|
@ daniel
I don't think that will help me with what I wont to do. I think that if I make a class that has a Socket and a ServerSocket in it then after they have connected to something they can both be used the same way. When I have time I will try it. If I works and I finish I will post my code so everyone can see it.
__________________
My IP address is 127.0.0.1
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 10:55 AM.
|
|
VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org