0

I have been trying to learn networking in java and have begun by making a simple client/server program with a simple GUI. The Server simply has a JTextArea and the client has a JTextField, JTextArea and a JButton. The way it is meant to work is that the user on the client side types a message in the JTextField and clicks the JButton(Send). This invokes a sendData method which takes the message the Client typed, puts it in a DatagramPacket and sends the packet to the Server on a predefined port. The Server on startup creates a thread which loops continuously allowing it to passively listen for packets using the DatagramSocket.recieve(DatagramPacket) method. If a packet is recived, it updates the JTextArea of the gui with the data in the packet. However, it seems as though the packets are not reaching the server. I have tested this on a LAN network and it IS working. However, when used over the internet the packets seem to be getting lost. I have tried with two different people who have had their ports forwarded and are sure that they are forwarded. The Client and Server both consist of 3 simple classes. Any help would be greatly appreciated. Sorry for the long description and code dump. Note: I am using UDP for this just because I want to learn it and I know TCP can be better for things like a chat.

Client class of Client:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class Client {

DatagramSocket socket;
Panel panel;

public Client(Panel panel) {
    this.panel = panel;
    try {
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
        panel.textArea.setText("Socket could not be created.");     
    }       

}   

public void sendData(byte[] data, InetAddress ipAddress){
    DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress,     27015);
    try {
        socket.send(packet);
        panel.textArea.setText("Client: Package being sent to server...");
    } catch (IOException e) {
        e.printStackTrace();
        panel.textArea.setText("Package could not be sent");
    }

}   

}

Panel class of Client:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;



public class Panel extends JPanel implements ActionListener {

JButton send;
JTextField textField;
JTextField textArea;
Client client;
InetAddress ipAddress;

public Panel(){


    this.setVisible(true);      

    send = new JButton("Send");
    send.addActionListener(this);
    send.setVisible(true);

    textField = new JTextField();
    textField.setVisible(true);
    Dimension dim = new Dimension(300,20);
    textField.setPreferredSize(dim);

    textArea = new JTextField();
    Dimension dim2 = new Dimension(300,100);
    textArea.setPreferredSize(dim2);

    this.add(textField);
    this.add(send);
    this.add(textArea, FlowLayout.LEFT);

    client = new Client(this);



}
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == send){
        System.out.println("Send was pressed");

        String message = textField.getText();
        try {
            ipAddress = InetAddress.getByName("localhost");
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
            textArea.setText("Hostname could not be resolved");
        }
        client.sendData(message.getBytes(), ipAddress);
        textArea.setText("Client: " + message);
    }

}



}

ServerThread class of the Server:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JPanel;


public class ServerThread extends Thread{

private DatagramSocket socket;
Panel panel;

public ServerThread(Panel panel) {
    this.panel = panel;
    try {
        socket = new DatagramSocket(27015);
        panel.setTextArea("The server has begun listening on port 3659...");
    } catch (SocketException e) {
        e.printStackTrace();
        panel.setTextArea("Server cant open socket and listen on port 3659");
    }
}

public void run(){
    while(true){
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);

        try {
            socket.receive(packet);         // waits for packet to arrive
            panel.setTextArea("Server: A package was recieved from the Client...");
        } catch (IOException e) {
            e.printStackTrace();
            panel.setTextArea("Socket cant recieve packets");
        }

        String message = new String(packet.getData());
        panel.setTextArea("Client: " + message);

    }

}

}

Note: For both the Server and Client, the main class simply makes a new Jframe and a new instance of the panel class and sets that as its contents pane. I did not include the panel class for the Server as it does not include any significant code.

Thanks again.

1 Answer 1

1

Sounds like network security configurations prevents you from receiving UDP packets. You need to configure your network such that it accepts UDP packets on the specified ports. Also, you have to configure your firewall to accept UDP packets on the specified ports as well.

5
  • 1
    Hi, The network has been configured (The ports have been forwarded). But I forgot about the firewall. I will configure the firewall and try it again. Thanks
    – Sam
    Jul 18, 2013 at 14:32
  • Hi, I have configured both the firewall and the network and still no packets seem to be reaching the server. Any ideas?
    – Sam
    Jul 18, 2013 at 19:30
  • Anyone else have any ideas? Any help would be nice. Thanks
    – Sam
    Jul 22, 2013 at 19:18
  • thanks for that link. Turns out it was my friends network (running the server), which was not configured properly and not allowing packets through and after configuring properly, has been working perfectly fine. However, the next step for me is to find a way to make a chat program that didn't need port forwarding or any sort of network configuration. Coincidentally, I was researching on that particular topic when I realized someone else had replied on this topic and then I saw your link :D. Thanks again.
    – Sam
    Jul 29, 2013 at 13:11
  • You are welcome :) In fact, I have always wondered how could they do p2p without the need for port forwarding. A year or more ago, I looked for articles about the same thing...UDP/TCP without port forwarding and could not find anything useful. And now your topic came and it was a great chance for me to find such a great article. Actually, I would like to thank you for helping me with my next project ;) Jul 30, 2013 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.