Question

I am trying get a running example for WiFi Peer-to-Peer, where one user can send a simple string to the other user.
I can already get a List of available clients, but I am stuck at connecting to a Client:

02-20 09:59:12.789: E/TTT(4176): Host is unresolved: 5e:f8:a1:36:ee:64

socket.connect((new InetSocketAddress(host, 8888)), 5000);

I would guess I need the IP address and not the MAC address, but how do I get it? Or is it a different problem?

Solution:
I show both clients their own IP address, so you can enter the IP to connect to (the other´s IP) - it works that way, although it´s not a perfect solution.

No correct solution

OTHER TIPS

try this..

socket = new Socket();
        InetSocketAddress socketAddress = new InetSocketAddress(SERVER_IP, SERVERPORT);
        socket.connect(socketAddress);

You open a Server socket on one device and connect using client socket on the other then you open a socket on the client and connect from the server to the client and then they become equal(P2P).

You do need the IP address of the device you are connecting to and you need to set the port to the one you opened on the server. If doing this on a PC, be sure to set-up firewall correctly also this applies to Android too if you have a firewall app installed. When creating server socket you need to just set the port (You could add IP addresses here also but those are just for filtering the requests).

You need the IP address because TCP and UDP protocols rely on internet protocol (IP) in order to work. This article will explain this in good detail http://en.wikipedia.org/wiki/TCP/IP_model but you could also see any tutorial or explanation of network stack and OSI model.

On the server side you would write something like :

ServerSocket serverSocket = null;
Socket clientSocket = null;
try {
    serverSocket = new ServerSocket(8000);
    clientSocket = serverSocket.accept();

    ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
    ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());

    oos.writeObject("WELCOME");
    oos.flush();
} catch (Exception e) {
    e.printStackTrace();
}

DO NOT put this on the UI thread because you will get an exception. Instead, create AsyncTask or worker thread named Networking or something and do networking stuff there. Also, when connected you will probably create blocking methods yourself using while(isConnected) loops in order to listen for data from the socket.

On the client side just connect to this opened socket using something like this:

Socket socket;
try {
    socket = new Socket("IP.OF.HOST.DEVICE", 8000);
    ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
    ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
    System.out.println("Connected");
    System.out.println("Input: " + input.readObject());
    while (true) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String s = br.readLine();
        output.writeObject(s);
        output.flush();
    }
} catch (Exception e) {
    e.printStackTrace();
}
//Close resources when you don't need them especially sockets.
socket.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top