Connexion socket android-PC
Bonjour
je bloque depuis hier sur un problème,malgré une longue recherche sur le net et la mise en application de pas mal de conseil que j'ai trouvé, je voudrais tout dabord créer une simple connection via une socket entre une application android et un programme java tournant sur mon PC
il y a au total 3 classe
coté serveur :
_Classe serveur (programme java)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Adressage {
public static void main(String[] zero) throws IOException {
ServerSocket socketserver = new ServerSocket(8666) ;
Socket serverClient = socketserver.accept() ;
System.out.println("Connection etablie !! Courage on y est presque ");
}
} |
coté client :
_activité principale
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.example.videos;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AndroidClient extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t = new Thread(new TestThread());
t.start();
Log.v("Finish","finish");
}
} |
_classe pour mon thread en arrière plan
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
| package com.example.videos;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import android.util.Log;
public class TestThread implements Runnable{
public TestThread(){}
@Override
public void run() {
// TODO Auto-generated method stub
int timeout = 10000;
int port = 8666;
InetSocketAddress socketAdress = new InetSocketAddress("192.168.15.1",port);
Log.v("On est dans","le thread");
try {
Socket socket = new Socket();
socket.bind(null);
socket.connect(socketAdress, timeout);
} catch (IOException e) {
Log.e("PAO","IO Exception.", e);
}
}
} |
sachant que je n'ai pas oublié la condition
manifeste
Code:
<uses-permission android:name="android.permission.INTERNET" />
merci de votre aide !:D