logcat:java.net.bindexception address already in use. at libcore.io.iobridge.blind
console: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source
i want to send my file apk from server java to client android in the localhost main activity in my android application:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
49
50
51
52
53
54
55
56
57
58
59 public class MainActivity extends Activity implements OnClickListener { public static String ism; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.but); btn.setOnClickListener(this); addButtonClickListner(); } public void addButtonClickListner() { Button btnNavigator = (Button)findViewById(R.id.button1); Button btn3 = (Button) findViewById(R.id.efface); btn3.setOnClickListener(new OnClickListener() { //le button effacer l'historique l'historique @Override public void onClick(View v) { Initial_historique.lance(); } }); btnNavigator.setOnClickListener(new OnClickListener() { //button pour recevoire le .apk appel socket @Override public void onClick(View arg) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { try { SocketFromServer.soc(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); //appel au thread pour recevoire } }); } }
my class socket in the android application:
my main in java application:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public class SocketFromServer { public static void soc() throws IOException { Socket sock = new ServerSocket(9001).accept(); Commun.transfert( sock.getInputStream(), new FileOutputStream("/data/data/com.so.and/And1.apk"), true); sock.close(); } }
my class socket in my java application:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 public class Serveur { public static void main(String[] args) throws IOException, InterruptedException { SocketToClient.socket(); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class SocketToClient { public static void socket() throws IOException { Socket sock = new Socket("10.0.2.2",9001); Commun.transfert( new FileInputStream("C:/Users/admin/Desktop/ws/serveur/And.apk"), sock.getOutputStream(), true); sock.close(); } } public class Commun extends SocketToClient { public static void transfert(InputStream in, OutputStream out, boolean closeOnExit) throws IOException { byte buf[] = new byte[1024]; int n; while((n=in.read(buf))!=-1) out.write(buf,0,n); if (closeOnExit) { in.close(); out.close(); } } }
Partager