Bonjour,
J'ai fait deux codes un serveur coté Android Java et l'autre client en C, j'aimerai réaliser une communication entre les deux mais je ne sais pas comment faire, je cherche sur le net mais je ne trouve pas vraiment ce qui m'intéresse.
Voilà mon code en Java :
et le code en C:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 package com.example.server_android; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; public class Server_Android extends Activity{ private final static int SERVER_PORT = 1234; //private final static int CLIENT_PORT = 10001; private final static String SERVERIP = "127.0.0.1"; InetAddress clientAddr = null; DatagramSocket socket = null; DatagramPacket packetOut = null; DatagramPacket packetIn = null; byte[] DataIn; byte[] DataOut; TextView text; Toast toast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_server_android); //text = (EditText)findViewById(R.id.editText1); text =(TextView)findViewById(R.id.textView2); text.setText(""); DataIn = new byte[1024]; DataOut = new byte[1024]; /* Thread for receiving Data from CLient */ new Thread(new Receiver()).start(); try { Thread.sleep(500); }catch(InterruptedException e){ Log.e("UDP", "UDP receive failed!"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.server__android, menu); return true; } public class Receiver implements Runnable{ public void run(){ try { while(true){ socket = new DatagramSocket(SERVER_PORT); text.setText(SERVERIP + "\n"); InetAddress clientAdress = InetAddress.getByName(SERVERIP); text.append("Adresse Client:" + clientAdress + "\n"); packetIn = new DatagramPacket(DataOut,DataOut.length); Log.d("UDP", "preparing packet"); Log.d("DatagramPacket", "" + packetIn); text.append("Packet :" + packetIn + "\n"); socket.receive(packetIn); Log.d("UDP", "Packet receveid"); String message = new String(packetIn.getData()); Log.d("UDP", "le message reçu"); Log.d("Message : ", "***" + message); text.append("Message : " + message + "\n"); } }catch(Exception e){ Log.e("UDP", "Receiver error", e); // toast.makeText(this, "Receviere Error", toast.LENGTH_LONG); // toast.setGravity(Gravity.CENTER, 0, 0); // toast.show(); } } } }
De l'aide svp.
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 //---------------------------------------------------------------------------- #include "biblio.h" int main(int argc, char **argv) { //_________Seting_Destination_IP_adress____________________ in_addr_t ipAddress= inet_addr("127.0.0.1"); //_________Seting_Destination_port____________________ int portNumber = 1234; //---- create UDP socket ---- int udpSocket=socket(PF_INET,SOCK_DGRAM,0); if(udpSocket==-1) { perror("socket"); exit(1); } struct sockaddr_in toAddr,serverAddr; int lenght = sizeof(toAddr); int size = sizeof(serverAddr); // ... allowing broadcast (optional) int on=1; if(setsockopt(udpSocket,SOL_SOCKET,SO_BROADCAST,&on,sizeof(int))==-1) { perror("setsockopt"); exit(1); } int s; for(;;) { //---- read next line on standard input ---- char msg[0x100]; char *buffer = malloc(100); if(!fgets(msg,0x100,stdin)) { break; } //---- send message to the specified destination/port ---- bzero(&toAddr,lenght); toAddr.sin_family=AF_INET; toAddr.sin_port=htons(portNumber); toAddr.sin_addr.s_addr=ipAddress; s = sendto(udpSocket,msg,strlen(msg),0,(struct sockaddr *)&toAddr,lenght); if( s == -1) { perror("sendto"); exit(1); } } //---- close UDP socket ---- close(udpSocket); return 0; }
Merci
Partager