[Socket] Impossible de se connecter?
Bonjour à tous!
Je suis en train de travailler sur un petit projet et j'ai besoin d'avoir une connexion client vers un serveur.
J'ai donc créé une petite fonction test nommée ping.
Elle à pour objectif d'envoyer un message au serveur.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
private static Socket sRecup;
private static InetAddress host;
private static int port;
public static boolean connexion(){
try {
sRecup = new Socket(host, port);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(str);
} catch (UnknownHostException e) {
System.out.println("1-Marche po");
return connected=false;
} catch (IOException e) {
System.out.println("2-Marche po");
return connected=false;
} catch (NetworkOnMainThreadException e) {
System.out.println("3-Marche po");
return connected=false;
}
return connected=true;
}
public static void setConnexion(String _host, int _port)
{
try { host=InetAddress.getByName(_host); }
catch (UnknownHostException e) { }
port=_port;
}
public static boolean ping()
{
setConnexion("192.168.7.1", 80);
if(connexion())
{
byte[] buffer = {'c', 'o', 'u', 'c', 'o', 'u'};
//String tmp="coucou";
try {
sRecup.getOutputStream().write(buffer, 0, buffer.length);
}
catch (IOException e) { return false; }
return true;
}
else
return false;
} |
Voici le MANIFEST :
Code:
<uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.INTERNET" />
Le soucis semble être à la connexion. J'ai regardé pas mal de tutos mais rien, je ne trouve pas de solution. Peut être pourriez-vous m'aider?