0
votes

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

1 Answers

1
votes

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.