Home arrow static arrow Java Programming [Archive] - Datagrams Over the Internet
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Datagrams Over the Internet
This topic has 7 replies on 1 page.

Posts:27
Registered: 7/27/04
Datagrams Over the Internet  
Aug 3, 2004 10:41 AM



 
I made a chat program that uses datagrams. It works fine when i run it by myself but when i get someone else to recieve the messages from another computer it doesn't work. Why do my datagrams work over my own computer but not over the internet.
 

Posts:19,725
Registered: 9/26/01
Re: Datagrams Over the Internet  
Aug 3, 2004 10:43 AM (reply 1 of 7)



 
I made a chat program that uses datagrams. It works
fine when i run it by myself but when i get someone
else to recieve the messages from another computer it
doesn't work. Why do my datagrams work over my own
computer but not over the internet.

Reading your mind, scanning your code...
Sorry, can't tell.
 

Posts:27
Registered: 7/27/04
Re: Datagrams Over the Internet  
Aug 3, 2004 10:47 AM (reply 2 of 7)



 
[code]
import java.net.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class CHAT2 extends JFrame implements Runnable, ActionListener {
Thread Client = new Thread(this, "Client");
DatagramSocket ds;
public int buffer_size = 1024;
public byte buffer[] = new byte[buffer_size];

public final int PORT = 3677;

JTextField jt;
JTextArea jto;
public CHAT2() {
Thread.currentThread().setName("Server");
setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
ds = new DatagramSocket(PORT);
}catch(SocketException ec) {

}
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(2,2));
setTitle("MChat");
setSize(400,400);
setResizable(false);
jt = new JTextField();
jto = new JTextArea();
jto.setFont(new Font("Arial", Font.BOLD, 12));
jto.setForeground(Color.BLUE);
jto.setEditable(false);
//jt.setSize(100,20);
//jt.setLocation(10,50);
JButton b = new JButton("Send Message");
//b.setSize(100,40);
//b.setLocation(10,100);
getContentPane().setLayout(new BorderLayout());
//getContentPane().add(b);
jp.add(jt);
jp.add(b);
getContentPane().add(jto, BorderLayout.CENTER);
getContentPane().add(jp, BorderLayout.SOUTH);
b.addActionListener(this);
Client.start();
}

public void run() {
try {
for(;;) {
buffer = new byte[buffer_size];
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
try {
ds.receive(p);
}catch(IOException ec) {

}
String temp = new String(p.getData(), 0, p.getLength());
System.out.println(temp);
if(jto.getText().length() > 1) jto.setText(jto.getText() + "\n" + temp);
else jto.setText(temp);
Thread.sleep(1);
}
}
}catch(InterruptedException ec) {

}
}
public void actionPerformed(ActionEvent ae) {
send(jt.getText());
}
public void send(String e) {
int c = 0;
while(c < e.length()) {
buffer[c] = (byte) (e.charAt(c) );
c++;
}
try {
ds.send(new DatagramPacket(buffer,c, InetAddress.getLocalHost(), PORT));
}catch(IOException ec) {

}
}
public static void main(String args[]) {
System.out.println(new Date().getDay());
CHAT2 ch = new CHAT2();
ch.setVisible(true);
}
 

Posts:19,725
Registered: 9/26/01
Re: Datagrams Over the Internet  
Aug 3, 2004 10:51 AM (reply 3 of 7)



 
ds.send(new DatagramPacket(buffer,c, InetAddress.getLocalHost(), PORT));
It's only sending data to localhost, not to some other computer listening on that port.
 

Posts:14,142
Registered: 99-04-02
Re: Datagrams Over the Internet  
Aug 3, 2004 10:53 AM (reply 4 of 7)



 
Most likely causes:

1) TTL expires - The packet has a Time To Live, which is the number of hops it is allowed to make, after which time it is dropped by the network routers. So if the distance from point A to point B goes thru too many points (routers) in between (you can't control the path), then the packets could be getting dropped.

2) Firewalls - Maybe either side (point A or point B) has a firewall set up to block all incoming data, or all incoming data at least on the port you are using, or possibly just UDP packets on that port. If you have a Linksys or D-Link router for a cable or DSL connection, you probably have to open up some ports.

3) Programmer Error - You aren't really sending or aren't reading correctly or aren't using the correct ports or something along those lines.
 

Posts:316
Registered: 4/7/04
Re: Datagrams Over the Internet  
Aug 3, 2004 10:54 AM (reply 5 of 7)



 
I didn't go through your entire code but this line looks like a possible culprit:
   ds.send(new DatagramPacket(buffer,c, InetAddress.getLocalHost(), PORT));

Also, if your are running this as an applet/JWS, you will face certain security issues.
 

Posts:14,142
Registered: 99-04-02
Re: Datagrams Over the Internet  
Aug 3, 2004 10:54 AM (reply 6 of 7)



 
or that... didn't see the code before I posted my comments.. although they are still possible problems for you.
 

Posts:316
Registered: 4/7/04
Re: Datagrams Over the Internet  
Aug 3, 2004 10:54 AM (reply 7 of 7)



 
I didn't go through your entire code but this line looks like a possible culprit:
   ds.send(new DatagramPacket(buffer,c, InetAddress.getLocalHost(), PORT));

Also, if your are running this as an applet/JWS, you will face certain security issues.
 
This topic has 7 replies on 1 page.