package com.example.tristan.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private Socket client;
int serverPort = 8888;
String serverIP = "192.168.1.6";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fctClient();
}
public void fctClient() {
try {
Toast toast = Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT);
InetAddress addr = InetAddress.getByName(serverIP);
SocketAddress socketAddress = new InetSocketAddress(addr,serverPort);
//SocketAddress socketAddress = new InetSocketAddress(serverIP, serverPort);
client = new Socket();
toast.show();
client.connect(socketAddress);
toast.show();
toast.show();`enter code here`
client.close();
} catch (UnknownHostException e) {
Toast toast = Toast.makeText(getApplicationContext(), "KO1", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
} catch (IOException e) {
Toast toast = Toast.makeText(getApplicationContext(), "KO2", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
}
}
} Hello, I am trying to connect a client on Android on a Python server using a socket. The Python server is working well but the Android code fails to connect the server passing through a IOException. Is anyone able to tell me my mistake? The code fails on line "client.connect(socketAddress);". This let me think that the wrong command is used to generate socketAddress. I found the following link which met the same problem but his solution doesn't work for me.
(Java/Android) Client-side Socket throwing IOException
Thanking you in advance, TL