I am setting up an chat app. I have a server, written in java with socket, that connects perfectly to an example code in java. But when I try the exact same code with android studio over the app it doesnt work for some reason.
I have tried to run it with a code outside of the app and it worked. I have got the user permissions on so it can connect to the internet. I have tried to connect to the server on the console and it worked.
The client:
class Client implements Runnable{
int freePort;
@Override
public void run() {
connect();
}
private void connect(){
System.out.println("Connecting");
try {
Socket socket = new Socket("localhost",1336);
System.out.println("Connected");
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
freePort = dataInputStream.readInt();
System.out.println(freePort);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The server:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class DeclarationServer {
private int numberOfClients = 0;
DeclarationServer(){
try {
ServerSocket serverSocket = new ServerSocket(1336);
while (true) {
System.out.println(serverSocket.getLocalPort() + "__" + serverSocket.getLocalSocketAddress());
Socket socket = serverSocket.accept();
System.out.println(socket.getLocalSocketAddress());
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
dataInputStream.readUTF();
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeInt(1339+numberOfClients);
dataOutputStream.flush();
dataOutputStream.close();
numberOfClients++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
The error message:
I/System.out: Connecting
D/ActivityThread: add activity client record, r= ActivityRecord{5f45e99 token=android.os.BinderProxy@712539a {com.example.birke.testapp/com.example.birke.testapp.MainActivity}} token= android.os.BinderProxy@712539a
D/ZrHung.AppEyeUiProbe: notify runnable to start.
W/System.err: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 1336) from /:: (port 32940): connect failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.connect(IoBridge.java:137)
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:391)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:231)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:213)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at java.net.Socket.connect(Socket.java:570)
at java.net.Socket.<init>(Socket.java:450)
at java.net.Socket.<init>(Socket.java:218)
W/System.err: at com.example.birke.testapp.Client.connect(MainActivity.java:151)
at com.example.birke.testapp.Client.run(MainActivity.java:145)
at java.lang.Thread.run(Thread.java:784)
Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
at libcore.io.Linux.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:118)
at libcore.io.IoBridge.connectErrno(IoBridge.java:151)
at libcore.io.IoBridge.connect(IoBridge.java:129)
... 12 more