Results 1 to 17 of 17

Thread: Sockets

  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

    Java Code:
        Socket TestClient;
        try {
               TestClient = new Socket("Machine name", PortNumber);
        }
        catch (IOException e) {
            System.out.println(e);
        }

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  7. #7
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  8. #8
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  10. #10
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    This program is just for learning purposes. I just wanted to learn how to use sockets.
    My IP address is 127.0.0.1

  12. #12
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Is there away to do networking without working on the socket level.
    My IP address is 127.0.0.1

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  14. #14
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I was just wondering if it was possible
    My IP address is 127.0.0.1

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  16. #16
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    In Java at higher level you can use RMI.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  17. #17
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    @ 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

Similar Threads

  1. Sending files over sockets!
    By rameshraj in forum Networking
    Replies: 2
    Last Post: 05-30-2008, 10:18 PM
  2. An echo server using UDP sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:09 PM
  3. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:05 PM
  4. how to send files through sockets
    By gabriel in forum Advanced Java
    Replies: 3
    Last Post: 01-12-2008, 08:10 AM
  5. Help with Sockets
    By Eric in forum Networking
    Replies: 3
    Last Post: 12-01-2007, 08:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •