Reputation: 31
I have a very simple Java server that runs on my laptop. On the other side i have my Tablet with also a very simple client to conect to my server but it is not working. Both are running on the same wireless network.
Sample of the Server ServerSocket serverSocket = null; Socket socket = null; DataInputStream dataInputStream = null; DataOutputStream dataOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
dataOutputStream.writeUTF("Hello!");
Simple client code:
mysock = new Socket(server_adress, port_number);
Ive tried setting the manifest WIFI permissions. Tried shutting down the computer firewalls. Is there something i should know regarding how the wireless conection works that might be blocking the conection?
Thanks in advance
Upvotes: 3
Views: 4100
Reputation: 928
You need to set permission for INTERNET.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Client side:
String adresaServer = "192.168.4.120";
int PORT = 8095;
Socket socket = null;
DataInputStream in = null;
try {
socket = new Socket();
SocketAddress adr = new InetSocketAddress(adresaServer, PORT);
socket.connect(adr, 1500);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
out.flush();
} catch (SocketTimeoutException e) {
System.err.println(" Error at CONNECTINGG: \n" + e);
} catch (UnknownHostException e) {
System.err.println(" Serverul nu poate fi gasit \n" + e);
System.exit(1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.writeFloat(sensors.getValueGyroZ());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Server Side:
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
serverSocket.setSoTimeout(1600000);
System.out.println(" Waiting a client ... ");
Socket socket = serverSocket.accept();
int i = 0;
DataOutputStream out = new DataOutputStream(
socket.getOutputStream());
while (true) {
DataInputStream in = new DataInputStream(
socket.getInputStream());
System.out.println(String.valueOf(in.readFloat()));
}
} catch (IOException e) {
System.err.println(" Eroare IO \n" + e);
} finally {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 1