Results 1 to 20 of 26
Thread: Connection between 2 clients
- 11-16-2011, 12:34 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
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!
- 11-16-2011, 04:56 PM #2
Re: Connection between 2 clients
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.I won't be able to know the IPs beforehand.
Once one PC has the address of the other, it can connect using a Socket to connect to the other's ServerSocket.
- 11-16-2011, 07:30 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
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
- 11-16-2011, 09:30 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
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 :(
- 11-16-2011, 09:31 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
Re: Connection between 2 clients
In Internet.
- 11-16-2011, 09:36 PM #6
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.
- 11-16-2011, 10:07 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
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.
- 11-16-2011, 11:07 PM #8
Re: Connection between 2 clients
That sounds like a way it could be done.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
- 11-16-2011, 11:48 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
- 11-16-2011, 11:58 PM #10
Re: Connection between 2 clients
What does SQL have to do with this project? Have you given up on the project?
- 11-17-2011, 12:06 AM #11
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
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();
- 11-17-2011, 12:07 AM #12
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
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. :)
- 11-17-2011, 12:12 AM #13
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.
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.no idea how to make the switch of IPs
- 11-17-2011, 12:38 AM #14
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
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.
- 11-17-2011, 12:54 AM #15
Re: Connection between 2 clients
Not quite. Its one port by each call to ServerSocket. An application can listen on more than one ServerSocket by using Threads..Exactly 1 port can only be listened by 1 server application.
- 11-17-2011, 02:17 AM #16
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
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
- 11-22-2011, 12:04 AM #17
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
- 11-22-2011, 12:40 AM #18
Re: Connection between 2 clients
What parts of the project do you have now? What part(s) are you working on next?
- 11-22-2011, 12:58 PM #19
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
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.
- 11-22-2011, 01:03 PM #20
Similar Threads
-
Server/clients connection periodically
By akis3110 in forum New To JavaReplies: 4Last Post: 06-28-2011, 03:03 PM -
communication between clients UDP
By maribo in forum NetworkingReplies: 1Last Post: 06-16-2010, 01:17 PM -
ip address of all clients
By guneet singh in forum NetworkingReplies: 4Last Post: 03-01-2009, 02:01 PM -
how to make server Wait for clients further requests after establishing connection
By nadia in forum NetworkingReplies: 4Last Post: 02-02-2009, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks