the_prole
the_prole

Reputation: 8985

How to receive a TCP packet over the internet

I have a TCP server app running as a separate module in Android Studio. It's listening for a remote TCP packet. The computer the app is running on is currently connected to my local area network.

TcpServer server = new TcpServer();
server.listenForPacket();

Here is TcpServer

public class TcpServer {

    private void listenForPacket(){

            try{
                ServerSocket welcomeSocket = 
                    new ServerSocket(Constants.LOCAL_PORT);
                Socket connectionSocket = 
                    welcomeSocket.accept();

                // Pauses thread until packet is received
                BufferedReader packetBuffer =
                        new BufferedReader(
                                new InputStreamReader(
                                connectionSocket.getInputStream()));

                System.out.print("Packet received");

            } catch (IOException e){
                e.printStackTrace();
            }
    }
}

I also have a seperate app on my phone running a TCP client. The phone has wifi turned off, and should send a packet to the server through its data line, and ultimatly over the internet.

TcpClient client = new TcpClient();
client.sendPacket();

Here is TcpClient

public class TcpClient {

    private void sendTcpPacket(){

        try {

            InetAddress remoteInetAddress =
                    InetAddress.getByName(Constants.PUBLIC_IP_ADDRESS);
            InetAddress localInetAddress =
                    InetAddress.getByName(Constants.LOCAL_IP_ADDRESS);

            int remotePort = Constants.FORWARDING_PORT;
            int localPort = Constants.LOCAL_PORT;

            Socket socket =
                    new Socket(remoteInetAddress, 
                        remotePort, localInetAddress, localPort);

            DataOutputStream dataOutputStream =
                    new DataOutputStream(socket.getOutputStream());

            byte[] packet = new byte[1];
            packet[0] = (byte) 255;

            dataOutputStream.write(packet, 0, packet.length);


        } catch (UnknownHostException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

The server is however, not receiving the packet sent by the client.

Now, I'm assuming my variables are correct, or are they?

InetAddress remoteInetAddress =
        InetAddress.getByName(Constants.PUBLIC_IP_ADDRESS);
InetAddress localInetAddress =
        InetAddress.getByName(Constants.LOCAL_IP_ADDRESS);

int remotePort = Constants.FORWARDING_PORT;
int localPort = Constants.LOCAL_PORT;

I've also set my forwarding port to forward to the local ip address.

Not sure why the packet isn't coming through. Any idea why?

Upvotes: 1

Views: 901

Answers (1)

user207421
user207421

Reputation: 311054

// Pauses thread until packet is received

No it doesn't.

BufferedReader packetBuffer =
    new BufferedReader(
        new InputStreamReader(
            connectionSocket.getInputStream()));

This just creates a BufferedReader. It doesn't do any I/O. If you want to read, you have to call one of the read() methods, or maybe readLine() if you were sending lines, which you aren't.

Also you aren't closing any sockets. And as you are using a DataOutputStream to send, you should use an InputStream to receive; or else keep the BufferedReader to receive and use a Writer to send.

Upvotes: 1

Related Questions