Reputation: 193
I'm having problems when creating a socket in android. I'm using LibGDX.
@Override
public void create() {
System.out.println("Enter IP adress.");
ip = "78.61.65.198";
System.out.println("Connecting...");
socket = connection(PORT, ip);
System.out.println("Setting up InputStream");
reader = reader(socket);
System.out.println("Setting up OutputStream");
output = output(socket);
while (socket.isConnected()) {
output.println("0;" + Gdx.input.getAccelerometerX() + ";" + Gdx.input.getAccelerometerY());
}
}
public static Socket connection(int port, String ip) {
try {
return new Socket(InetAddress.getByName(ip), port);
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("Unknown host...");
return null;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to connect...");
return null;
}
}
public static BufferedReader reader(Socket socket) {
try {
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
System.err.println("Couldn't get inputStream...");
e.printStackTrace();
return null;
}
}
public static PrintStream output(Socket socket) {
try {
return new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.err.println("Couldn't get outputStream");
return null;
}
}
Server:
@Override
public void create() {
System.out.println("Creating server...");
server = hostServer(PORT);
System.out.println("Waiting for Client 1...");
client1 = waitForConnections(server);
System.out.println("Setting up input/output for client 1...");
client1Input = reader(client1);
client1Output = output(client1);
setScreen(new gameScreen(this));
}
public static ServerSocket hostServer(int port){
try {
return new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to host server!");
return null;
}
}
public static BufferedReader reader(Socket socket){
try {
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to get input stream!");
return null;
}
}
public static PrintStream output(Socket socket){
try {
return new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to create output stream!");
return null;
}
}
public static Socket waitForConnections(ServerSocket server){
try {
return server.accept();
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to accept connection!");
return null;
}
}
It works when i do it with a desktop project, but when i do it with an android project, it doesn't work.
Let me know if anyone has similiar problems or have found a solution.
Upvotes: 1
Views: 2455
Reputation: 3867
Assuming both your PC and your Android device are connected to the same LAN (connected to the same router), you should try to use the server's internal IP address (usually looks like: 10.0.0.X or 192.168.X.X), which you can find by running the ipconfig
command on the command line in a Windows PC, or ifconfig
on Linux/MAC. If your PC and Android device are connected to different networks then you should use the server's external IP address like you did in your example. If it is the latter you should also forward the port your using (1234) to your PC in your router.
Upvotes: 1
Reputation: 1593
When the server is startet on your pc, you cannot connect from another device than your pc as long as you try to connect to localhost/127.0.0.1. Only when running the app on your pc as desktop application the connection will work since client and server are on the same host. For other clients you need to connect to the local LAN IP address of your PC. When you try to connect to localhost/127.0.0.1 from your phone it is looking for a server running on the phone and this is not the case.
Upvotes: 0
Reputation: 85
Is the client network connection estabished in an UI thread/activity? If so, network connection should established in a separate thread, eg. AsynTask for short operations and services for longer streaming operations. http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask
Upvotes: -1