Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sockets between android device and pc (same network)

I have been struggling with this for a while now. What I'm simply trying to do, is to create a socket connection between my android app and my java program on PC.

I have both tried UDP and TCP sockets and different kinds of IP's and port numbers.

So, how can I achieve this?

Here is my (current code) with a (attempted) TCP connection:

Code snip from the server side (PC java program):

try {

    DatagramSocket socket = new DatagramSocket(4466);
    byte[] buffer = new byte[2048];

    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    socket.receive(packet);

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

And the relevant snip from my android app (client):

try {

    InetAddress host = InetAddress.getByName("192.168.1.255");
    DatagramSocket socket = new DatagramSocket (null);
    byte[] buffer = new byte[2048];
    buffer = "hej hej".getBytes();

    DatagramPacket packet=new DatagramPacket (buffer, buffer.length, host, 4466);
    socket.send(packet);
    socket.close();

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

The IP address: 192.168.1.255 is supposed to be some kind of broadcast IP. But I have also tried different IP's like the IP for my PC (hard-coded in the android app), the 255.255.255.0, localhost and so on.

I would really appreciate it if anyone could help me out!

like image 237
Langkiller Avatar asked Feb 07 '14 12:02

Langkiller


People also ask

Can multiple sockets use the same port?

Thus the server can have many TCP sockets using the same local port, as long as each of the sockets on the port is connected to a different remote location.

How do I add a socket to my computer network?

The socket is created by the combination of the IP address and port number of the software. With this combination, the process knows the system address and address of the application where data is to be sent. : is used to separate IP address and port number. For eg: 192.168.

How do network sockets work?

Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events.

What is socket connection Android?

Socket programming is a method of communicating between two devices connected to the same network. Two sockets, one on the client and one on the server, interact. An IP address plus a port make up a socket's address. Over the specified port, the server application begins to listen to clients.


2 Answers

I just created simple demo in Android and Desktop application which is connected via Socket Connection and its like a Chat Application. Might be that will help you a lot. Please check below link for more clarification.

Android Client Connected with Socket

like image 199
PrashantAdesara Avatar answered Nov 06 '22 03:11

PrashantAdesara


You need the public address of your computer (search for something like what's my IP) and make sure there's no firewall blocking the port. The 192.168.***.* address is not public (it's a LAN address), and the emulator (or an actual phone) won't able to see it.

like image 43
Kayaman Avatar answered Nov 06 '22 05:11

Kayaman