Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Connection between 2 clients

    Hello!

    So, I have to make a project which is basically a messenger, but with a lot less features. I only have to be able to send text and video between only 2 clients. I have searched for hours trying to find how to make the clients 'get in touch' with each other.
    The program will be tested on 2 random computers so I won't be able to know the IPs beforehand.
    I've read about sockets and some other stuff, but I still couldn't understand how computer A will connect with computer B and vice-verse.
    Any suggestions?

    Cheers!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    I won't be able to know the IPs beforehand.
    How will they find out the IP addresses? One will need to get the address of the other. Perphaps by having a known server that they register their IP addresses with.
    Once one PC has the address of the other, it can connect using a Socket to connect to the other's ServerSocket.

  3. #3
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Connection between 2 clients

    First do you intend to do this project in intranet(Local Lan) or internet
    if it is local lan then I know the way how you communicate

  4. #4
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Connection between 2 clients

    I have thought about the known server, but I didn't find much about how to do it. What can I use to act as a server? I haven't worked much with java or with servers :(

  5. #5
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Connection between 2 clients

    In Internet.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    For your app you could write your own server to take connections from any PC and return the IP address of another PC if the two PCs involved wanted to allow that to happen.

  7. #7
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Connection between 2 clients

    Since I only need 1 connection it shouldn't be much of a hassle, but I still haven't found a way to make that server. I've found a socket server example on oracle.com, but I don't know how to put it somewhere on the internet.
    Basically, PC A sends his IP to the server, and then PC B gets the IP from the server and connects to A, which will get B's IP from B itself? Or should the server send A B's IP as well?
    Sorry, if I'm a bit confusing.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    Basically, PC A sends his IP to the server, and then PC B gets the IP from the server and connects to A, which will get B's IP from B itself
    That sounds like a way it could be done.

  9. #9
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Connection between 2 clients

    Quote Originally Posted by Norm View Post
    That sounds like a way it could be done.
    Okay. So how do I do that? :D That's all I want to know for now. Should I read some SQL or is it easier to do?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    What does SQL have to do with this project? Have you given up on the project?

  11. #11
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Connection between 2 clients

    I guess you are beginner so Java 109 : Networking with Java
    the link helps you to create server and client

    It explains the issue as simple as System.out.println();

  12. #12
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Connection between 2 clients

    I'm still on it. It's quite important :)
    I really have no idea how to make the switch of IPs. Like I said above, I know what should be done, but I don't know how. I thought I could use SQL to store the IPs and then each pc could get the other one's IP from the database. I haven't read much about SQL either so I am not sure if this can be done. I am usually a quick learner, but I haven't a tutorial about what I want, well, because I am not sure what I should look for.

    Thank you for your efforts, this forum is kinda my last resort. :)

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    A simple text file should work for storing the client's information until the rest of the code is working. Perhaps something in a Properties file like an .ini. A database seems like overkill for what you what to save and retrieve.
    no idea how to make the switch of IPs
    You should start with a very simple client-server test program to figure out how to use the ServerSocket and Socket classes. There are lots of code examples on the Forum.

  14. #14
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Connection between 2 clients

    Just brief explanation:

    To test your code you don't need to put a server. You can use the server in your computer.Also you can run server to internet with your computer.
    If you arrange port forwarding, then anyone in internet can connect your server in your computer with your IP.

    In the beginning you can create server in your computer.Exactly 1 port can only be listened by 1 server application.

    Then other hand in client application you connect to server in your computer.
    Each client has socket that helps them to connect server.
    Sockets are contains information about connection IP & port , etc
    For example you can run your application 2 times then 2 client connects your server.
    The socket contains every detail so in your server you can store information about clients.

    //TCP connection example:
    CLIENT
    //Socket clientSocket=new Socket(InetAddress.getByName("127.0.0.1"),12345);


    SERVER
    //ServerSocket socket=new ServerSocket(12345);
    //Socket clientSocket=socket.accept(); //server waits until a connection occurance in .accept() method
    //System.out.println(clientSocket.getInetAddress());//If a client connects server then it shows the ip address of client. You can store ip address of clients.
    THIS CODE IS JUST EXAMPLE YOU HAVE TO MODIFY IT to communicate with each other.//BUT handshake is finished so you can just send information to server
    In given example , server only listens 1 client. Other clients won't be listened. // Use thread to listen each client that wants connection.

    When client A wants direct connection to client B then you can just send B's IP to client A.
    client A can create another socket and connect to client B
    //In this example each client has a server as well. Client Server listenes a port other than server's port. // This is for precaution because in your computer you can not listen 1 port with 2 applications.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    .Exactly 1 port can only be listened by 1 server application.
    Not quite. Its one port by each call to ServerSocket. An application can listen on more than one ServerSocket by using Threads.

  16. #16
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Connection between 2 clients

    Maybe I expressed wrong.
    if a server application listens a port(54321),then any other application can not listen that port at the same computer.
    that is what I want to say

    So if application has server & client side at the same time, then launching only server at the same computer may give bind JVM exception(port is in use).
    Client application has already binded port so server can not listen same port.Of course server application can listen different port

  17. #17
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Connection between 2 clients

    Quote Originally Posted by Grkn View Post
    Just brief explanation:

    To test your code you don't need to put a server.
    Okay! I have completed this part of the project and it works with 127.0.0.1.
    Now I need to adjust the code so that the program gets the IP from somewhere on the net. Do I need a domain, a free one is good enough?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    What parts of the project do you have now? What part(s) are you working on next?

  19. #19
    EvilMonkey is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Connection between 2 clients

    Well, I have made the communication part and I can send text between the 2 applications, but I am using the 127.0.0.1 IP for my socket. Now I need to make them be able connect to each other if the apps are on different PCs and I don't know the IPs to write them in my socket.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Connection between 2 clients

    Is this the problem that was discussed in posts #2 and #6?

Page 1 of 2 12 LastLast

Similar Threads

  1. Server/clients connection periodically
    By akis3110 in forum New To Java
    Replies: 4
    Last Post: 06-28-2011, 04:03 PM
  2. communication between clients UDP
    By maribo in forum Networking
    Replies: 1
    Last Post: 06-16-2010, 02:17 PM
  3. ip address of all clients
    By guneet singh in forum Networking
    Replies: 4
    Last Post: 03-01-2009, 03:01 PM
  4. Replies: 4
    Last Post: 02-02-2009, 05:05 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
  •