I'm making an instant messenger program and I want to allow clients to connect to each other privately(peer-to-peer) using their IP addresses. The only method I know at the moment is a client/server method where the server needs to be online for the client to work. How do I allow for the client to be connected to another client?
e.g.
public class Messenger extends JFrame{
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
private String ipAddress = "127.0.0.1"; //would be replaced with the other users ip address
public Messenger(){
//GUI is set up here
//event handler for the JTextField in which the user type their own messages
//event handler will send the message on the output stream
//JTextField is setEditable(false) until the connection is made and streams are setup
}
public void startRunning(){
//setup socket
connection = new Socket(InetAddress.getByName(IpAddress), 6789); //a port would be opened by all other clients to allow for the connection
//setup streams
try{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
whileChatting(); //method that recieves messages and prints them out on the screen
}
}
private void whileChatting(){
String message = "";
do{
try{
message = (String) input.readObject();
//print message on screen
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
}while(!message.equals("ENDChat"));
}
public static void main(String args[]){
Messenger im = new Messenger(); //call constructor
im.startRunning(); //set up connection
}
}