File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Networked Programs that work across countries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Networked Programs that work across countries" Watch "Networked Programs that work across countries" New topic
Author

Networked Programs that work across countries

Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
How do I make networked programs work across countries or at least in the same city. I just want to know how I can make networked programs work outside my network.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
Writing networked applications that are on the same network isn't fundamentally different from ones that run at opposite ends of the earth. What kind of communication pattern are you envisioning - one-to-one, one-to-many/many-to-one or many-to-many?

Keep in mind that firewalls in between communicating applications may block some of the ports you intend to use.


Ping & DNS - my free Android networking tools app
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
many-to-many

Its a chat program
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
For chat, socket communication is probably best. You will have problems getting through firewalls that are not open for whatever port you end up using.

You'll need to think about how chat client A will know about chat client B (i.e, its IP address). Some form of central control seems necessary for this.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
I have made the chat program using sockets, it works perfectly within my home network but does not work outside. I am trying to find out if I can make the same thing work across countries.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
"does not work" is not a very useful problem description. It gives us nothing on which base our advice. How does it not work? Are there error messages? TellTheDetails

Have you made sure there is no firewall in between that blocks connections?
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Yes, there is defintely a firewall between them, I think there are 2, the windows firewall and the norton internet security firewall. I used port 6000 but I dont think that the firewall is stopping it because it works perfectly in my home network...Do you think that I should use port 80, since firewalls wont think the internet port is suspicious?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
You shouldn't use any port that is well-defined for some other protocol. Any port below 1024 may anyway cause problems for Unix-ish OSes (where permissions may not allow those ports to be used).
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

If you are running into commercial firewalls, such as used in corporate environments, you can only reliably use two ports: 80 and 443.

Nearly all corporate IT network folks block all other ports.

While you can have your program use any port over 1000, that's unlikely to work in a corporate space.

And many consumer firewalls also prevent access to other than a small subset of ports., typically something like 25, 53, 80, 110, 443, and a few others.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
let me try those ports, but im just curious, if 80 is the internet port, what is 443?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
80 is the default for HTTP, 443 is the default for HTTPS.

You might want to bookmark http://www.faqs.org/rfcs/rfc1700.html
[ August 25, 2008: Message edited by: Ulf Dittmer ]
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
The thing is that I connected to an other unsecured network ad tried to acsess the program on my other computer (on port 80) but it still failed
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
What does "it failed" mean? What software -if any- was running on port 80 of that machine? TellTheDetails
[ August 25, 2008: Message edited by: Ulf Dittmer ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Do you know what NAT is, and what it does? Do you know how to configure port forwarding on your home router, or why you would want to? Do you understand the difference between peer-to-peer protocols and client-server protocols? This is all well-understood material; you don't necessarily need to be working to solve any problems, but rather just to learn about the problem space some more. Do some reading about NAT and router configuration, and you may come to understand the issues here much better.


[Jess in Action][AskingGoodQuestions]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

BTW, this seems to me to be more of a general networking, or sockets question, rather than one about classic distributed Java using RMI, Corba, etc.

Perhaps a move is in order?
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Im not sure if there are any programs running on port 80...How do I find out?
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
I think that Firefox was running on port 80 at that time
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
Firefox is a client, not a server, so it wouldn't be running on any port. You can use any web browser to check whether there is anything running on port 80 by pointing it to "http://x.y.z.w/", substituting the IP address of the machine for "x.y.z.w".
Mario Minutolo
Greenhorn

Joined: Aug 06, 2008
Posts: 23
Originally posted by Devasia Manuel:
How do I make networked programs work across countries or at least in the same city. I just want to know how I can make networked programs work outside my network.


The same way you make local network programs (more or less).
Probably your problem is not a java problem, but an IP problem.

If your address is a private one, which most cerainly is, you simply cannot be reached from outside.

The same applies to the other side of the communication (if it has a private address, cannot be reached).

You need to develop a chat server a publish it.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
and when I say "It failes", I means that I got a:

java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at FileTransfer_Client.Client.connect(Client.java:119)
at FileTransfer_Client.Client.main(Client.java:113)
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
and just so that it may help anyone, I will post my code for the Client and Server (Note: My programs are made in Netbeans so ignore th GUI part):
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Server:


package Server;

import java.net.InetAddress;
import javax.swing.UIManager;


public class Main extends javax.swing.JFrame {

static Main s=new Main();

String ipAddress;

public Main() {
initComponents();
}



// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
ipAddress_textfield = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
port_textfield = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
username_textfield = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("TalkALot Server");

jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setLineWrap(true);
jTextArea1.setRows(5);
jTextArea1.setText("To enable unlimited talking with TalkALot, \nyou first must set up a server where \nclients can connect to...Simply enter the \ndetails and click Start!!");
jScrollPane1.setViewportView(jTextArea1);

jLabel1.setText("IP Address: ");

ipAddress_textfield.setEditable(false);

jButton1.setText("Start!!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startButtonListener(evt);
}
});

jLabel2.setText("Port Number: ");

port_textfield.setText("6000");

jLabel3.setText("Username: ");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(104, 104, 104)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(username_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(port_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ipAddress_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(167, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(157, 157, 157))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(ipAddress_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(port_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(username_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void startButtonListener(java.awt.event.ActionEvent evt) {

Variables.port=Integer.parseInt(port_textfield.getText());
Variables.username=username_textfield.getText();
s.setVisible(false);

Thread server=new Thread(new Server());
Thread client=new Thread(new Client());
server.start();
client.start();

}


public static void main(String args[]) {

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (Exception e){}

s.setVisible(true);
s.setIP();



}

void setIP(){

try{ipAddress=InetAddress.getLocalHost().toString();System.out.println(ipAddress);}catch (Exception e){}
String tokens[]=ipAddress.split("/");
Variables.ipAddress=tokens[1];
System.out.println("IP Address= "+tokens[1]);
Variables.username=tokens[0];
System.out.println("Username= "+tokens[0]);

ipAddress_textfield.setText(tokens[1]);
username_textfield.setText(tokens[0]);

}

// Variables declaration - do not modify
private javax.swing.JTextField ipAddress_textfield;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField port_textfield;
private javax.swing.JTextField username_textfield;
// End of variables declaration

}

package Server;

import java.io.*;
import java.net.*;
import java.util.*;

public class Server implements Runnable {

ArrayList<PrintWriter> clientOutputStreams;

String message;

public class ClientHandler implements Runnable {

BufferedReader reader;

Socket sock;


public ClientHandler(Socket clientSocket) {
try {
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);

} catch(Exception ex) {ex.printStackTrace();}
} // close constructor

public void run() {


try {

while ((message = reader.readLine()) != null) {

checkMessage();


System.out.println("read " + message);
tellEveryone(message);

} // close while
} catch(Exception ex) {ex.printStackTrace();}
} // close run
} // close inner class

void checkMessage(){

try{
if(message.contains("102594627847241004627836582")){

System.out.println("Message contains code");
String[] user=message.split("}");
System.out.println("Split Correctly");
System.out.println("Array length: "+user.length);
System.out.println("user[0]="+user[0]);
System.out.println("user[1]="+user[1]);
message=user[1]+" has joined the chat";
System.out.println("It is a username");
}
} catch (Exception e){
System.out.println("It is not a username");
e.printStackTrace();
}
}


public void run(){
main();
}

void main () {
go();
}

public void go() {

System.out.print("Server Started!!");

clientOutputStreams = new ArrayList<PrintWriter>();

try {
ServerSocket serverSock = new ServerSocket(Variables.port);

while(true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);

Thread t = new Thread(new ClientHandler(clientSocket));
t.start();


System.out.println("got a connection");
}
// now if I get here I have a connection

}catch(Exception ex) {
ex.printStackTrace();
}
}

public void tellEveryone(String message) {

Iterator it = clientOutputStreams.iterator();
while(it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch(Exception ex) {
ex.printStackTrace();
}

} // end while

} // close tellEveryone


}


package Server;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.*;
import java.io.*;
import java.io.BufferedWriter.*;
import static java.lang.System.*;
import java.util.Calendar.*;
import java.util.Date.*;
import java.util.Date;
import javax.swing.*;

public class Client extends javax.swing.JFrame implements WindowListener, Runnable {

Socket sock;

BufferedReader reader;

PrintWriter writer;

public Client() {

initComponents();

}

public void run(){
main();
}



// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
incoming = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
outgoing = new javax.swing.JTextArea();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
ipAddress = new javax.swing.JLabel();
port = new javax.swing.JLabel();
username = new javax.swing.JLabel();
send_button = new javax.swing.JButton();

setTitle("TalkALot");

incoming.setColumns(20);
incoming.setEditable(false);
incoming.setLineWrap(true);
incoming.setRows(5);
jScrollPane1.setViewportView(incoming);

outgoing.setColumns(20);
outgoing.setLineWrap(true);
outgoing.setRows(5);
jScrollPane2.setViewportView(outgoing);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Information"));

jLabel1.setText("IP Address: ");

jLabel2.setText("Port: ");

jLabel3.setText("Username: ");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(port, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
.addComponent(ipAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(ipAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
);

send_button.setText("Send");
send_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
send_button(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
.addComponent(send_button, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(send_button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void send_button(java.awt.event.ActionEvent evt) {

try {
writer.println(Variables.username+": "+outgoing.getText());
writer.flush();

} catch(Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();

}


void main() {

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch (Exception e){e.printStackTrace();}

Client c=new Client();

c.setVisible(true);
c.setInfo();
c.start();


}

void setInfo(){


ipAddress.setText(Variables.ipAddress);
username.setText(Variables.username);
port.setText(Variables.port+"");

}

void start(){
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

}

void setUpNetworking() {
try {
sock = new Socket(Variables.ipAddress, Variables.port);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);

writer = new PrintWriter(sock.getOutputStream());

System.out.println("networking established");

try {
writer.println("102594627847241004627836582"+"}"+Variables.username);
writer.flush();

} catch(Exception ex) {
ex.printStackTrace();
}
} catch(IOException ex) {
ex.printStackTrace();
}
} // close setUpNetworking

public class IncomingReader implements Runnable {
public void run() {
String message;
try {

while ((message = reader.readLine()) != null) {
System.out.println("read " + message);
incoming.append(message + "\n");

} // close while
} catch(Exception ex) {ex.printStackTrace();}
} // close run
} // close inner class

// Variables declaration - do not modify
private javax.swing.JTextArea incoming;
private javax.swing.JLabel ipAddress;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea outgoing;
private javax.swing.JLabel port;
private javax.swing.JButton send_button;
private javax.swing.JLabel username;
// End of variables declaration


public void windowOpened(WindowEvent e) {

}

public void windowClosing(WindowEvent e) {
writer.println(Variables.username+" has left the chat");
}

public void windowClosed(WindowEvent e) {

}

public void windowIconified(WindowEvent e) {

}

public void windowDeiconified(WindowEvent e) {

}

public void windowActivated(WindowEvent e) {

}

public void windowDeactivated(WindowEvent e) {

}

}
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Client:




package Client;


class Variables {

static String ipAddress;
static String port;
static String username;

Variables(String user, String ip, String port1){

username=user;
ipAddress=ip;
port=port1;

System.out.println(username+" "+ipAddress+" "+port);

}

}


package Client;

import java.net.*;
import java.io.*;
import java.io.BufferedWriter.*;
import static java.lang.System.*;
import java.util.Calendar.*;
import java.util.Date.*;
import java.util.Date;
import javax.swing.*;

public class Setup extends javax.swing.JFrame {

static Setup s=new Setup();

String port_text;

public Setup() {
initComponents();
}



// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
username = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
ipAddress = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
port = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
error = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("TalkALot Setup");
setAlwaysOnTop(true);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

jLabel1.setText("Please type in the following information to engange in a ");

jLabel2.setText("TalkALot conversation: ");

username.setText("other");

jLabel3.setText("Username: ");

jLabel4.setText("IP Address: ");

ipAddress.setText("127.0.0.1");

jLabel5.setText("Port:");

port.setText("6000");

jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ok_button_listener(evt);
}
});

error.setFont(new java.awt.Font("Tahoma", 0, 14));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2))
.addContainerGap())
.addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ipAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(50, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(130, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(error, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(112, 112, 112))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(ipAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(error, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

private void ok_button_listener(java.awt.event.ActionEvent evt) {

try{
port_text=port.getText();

}

catch (NumberFormatException r){r.printStackTrace(); error.setText("ERROR!!");}

new Variables(username.getText(), ipAddress.getText(), port_text);

Client c=new Client();
c.main();

s.setVisible(false);


}


public static void main(String args[]) {

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch (Exception e){}

s.setVisible(true);

}

// Variables declaration - do not modify
private javax.swing.JLabel error;
private javax.swing.JTextField ipAddress;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField port;
private javax.swing.JTextField username;
// End of variables declaration

}


package Client;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.*;
import java.io.*;
import java.io.BufferedWriter.*;
import static java.lang.System.*;
import java.util.Calendar.*;
import java.util.Date.*;
import java.util.Date;
import javax.swing.*;

public class Client extends javax.swing.JFrame implements WindowListener, Runnable {

Socket sock;

BufferedReader reader;

PrintWriter writer;

public Client() {

initComponents();

}

public void run(){
main();
}



// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
incoming = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
outgoing = new javax.swing.JTextArea();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
ipAddress = new javax.swing.JLabel();
port = new javax.swing.JLabel();
username = new javax.swing.JLabel();
send_button = new javax.swing.JButton();

setTitle("TalkALot");

incoming.setColumns(20);
incoming.setEditable(false);
incoming.setLineWrap(true);
incoming.setRows(5);
jScrollPane1.setViewportView(incoming);

outgoing.setColumns(20);
outgoing.setLineWrap(true);
outgoing.setRows(5);
jScrollPane2.setViewportView(outgoing);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Information"));

jLabel1.setText("IP Address: ");

jLabel2.setText("Port: ");

jLabel3.setText("Username: ");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(port, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
.addComponent(ipAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(ipAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
);

send_button.setText("Send");
send_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
send_button(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
.addComponent(send_button, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(send_button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void send_button(java.awt.event.ActionEvent evt) {

try {
writer.println(Variables.username+": "+outgoing.getText());
writer.flush();

} catch(Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();

}


void main() {

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch (Exception e){e.printStackTrace();}

Client c=new Client();

c.setVisible(true);
c.setInfo();
c.start();


}

void setInfo(){


ipAddress.setText(Variables.ipAddress);
username.setText(Variables.username);
port.setText(Variables.port+"");

}

void start(){
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

}

void setUpNetworking() {
try {
sock = new Socket(Variables.ipAddress, Integer.parseInt(Variables.port));
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);

writer = new PrintWriter(sock.getOutputStream());
writer.println(InetAddress.getLocalHost());

System.out.println("networking established");

try {
writer.println("102594627847241004627836582"+"}"+Variables.username);
writer.flush();

} catch(Exception ex) {
ex.printStackTrace();
}
} catch(IOException ex) {
ex.printStackTrace();
}
} // close setUpNetworking

public class IncomingReader implements Runnable {
public void run() {
String message;
try {

while ((message = reader.readLine()) != null) {
System.out.println("read " + message);
incoming.append(message + "\n");

} // close while
} catch(Exception ex) {ex.printStackTrace();}
} // close run
} // close inner class

// Variables declaration - do not modify
private javax.swing.JTextArea incoming;
private javax.swing.JLabel ipAddress;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea outgoing;
private javax.swing.JLabel port;
private javax.swing.JButton send_button;
private javax.swing.JLabel username;
// End of variables declaration


public void windowOpened(WindowEvent e) {

}

public void windowClosing(WindowEvent e) {
writer.println(Variables.username+" has left the chat");
}

public void windowClosed(WindowEvent e) {

}

public void windowIconified(WindowEvent e) {

}

public void windowDeiconified(WindowEvent e) {

}

public void windowActivated(WindowEvent e) {

}

public void windowDeactivated(WindowEvent e) {

}

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
Please edit your code to UseCodeTags. People are not going to try to make sense out of that much hard-to-read code.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Why did you ignore my post? I'm telling you: Google "NAT" (which is short for Network Address Translation) and read about it, and most likely you will immediately understand why your program isn't working from one home LAN to another.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
By the way, once you get past the networking issues, there's a bug waiting to happen in the code: Don't println to a Socket.

Moving to the Sockets forum since no distributed technologies are involved.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
ohh, sorry, I didnt see your post among all the other, ill check out Google NAT
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
NAT seems like exactly what I am looking for, I Googled it and went to wikipedia, but im not sure how to implement it into my code...
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Ive also done a bit of reasearch on IPv6, I've heard that's its for worldwide use, can it help me solve my problem here? And if so, how can I use it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
With all due respect, I think your knowledge of how TCP/IP networks work is still too limited to understand and/or tackle the issues that the problem at hand presents.

To recap just briefly: NAT isn't the solution to your problem, it's why the solution you outlined above can't work.

IP6 -like IP4- is a transport protocol. It operates at a lower level than the problems you're grappling with, and doesn't help here.

I'd suggest to read up on TCP/IP protocols (and socket networking) in detail, and in the end you'll probably agree that a pure P2P solution will not work; you'll need to resort to a server that mediates between the various clients.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Im still not sure when you mean a server that mediates betweens different clinets. Could you give a simple example + code example
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Most routers (which implements the NAT service) provides mechanisms to punch through the firewall. In most cases, it is just an admin page that allows you to route a port to a specific machine.

Programatically, some routers support the PnP protocol, which has definitions for routers to enable a program to route a port from the external address to the NAT'ed address.


Of course, all of this may be academic, as the OP needs to have a better grasp of basic networking and routing first.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Devasia Manuel:
Im still not sure when you mean a server that mediates betweens different clinets. Could you give a simple example + code example


This may get complicated, but I'll give it a try... Basically, a NAT'ed network will allow machines from inside the private network out of the network, but not machines from outside the network into the private network.

For machines outside the network to get into the private network, a port must be opened for it, which will route the packet to the correct machine. (see my previous post)

Now.... A common trick, for cases where this is not possible, is to have a "packet router" service also located outside of the network. The client outside of the network must know of its existence and negotiates a connection with it first. As for the actual service inside the network, it now needs to also act like a client, and connect to this external service -- to negotiate a connection first.


This server that Ulf speaks of, is the "packet router" server, that will take these two connections, and connect them together, as if they connected to each other in the first place.

As for the "simple example + code example", you can probably guess there is no "simple" example. You will need to modify your clients. You will need to modify you chat server. And you will need to write a "packet router" server, which connects all of this together.

Henry
[ August 30, 2008: Message edited by: Henry Wong ]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

I have to say, I agree with Ulf, the OP needs to do some basic learning on networks and how to make programs talk between two computers, say in the same room, before getting into this more complex stuff.

Its not just a question of getting a code fragment to cut and paste.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
I see now, sorry for changing the subject, but can anyone tell a good book (not too complicated) where I can learn and read all this stuff?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42275
    
  64
A very good book (in my opinion) is volume 1 of "Internetworking with TCP/IP" by Douglas Comer. It goes straight to the details, though, without much in the way of holding the reader's hand.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
ok, thanks, I'll borrow it from the library and read it.
T.A. Nguyen
Ranch Hand

Joined: Sep 02, 2008
Posts: 36

Start with a basic networking book and also the following sites have good sources:

and when you are getting sleepy, check out these projects on sourceforge.net (open sources, you might consider join them)


enjoy!


T.A. Nguyen
http://ta.cnci.org
http://www.linkedin.com/in/nguyenta
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Networked Programs that work across countries