Bonsoir,
J'ai un vrai problème. depuis 2 jours je cherches, j'ai trouvé plein de solutions mais aucune d'entre elles ne fonctionne.
je souhaite programmer une application sur Android et une application Java sur mon PC et pouvoir communiquer entre eux via les sockets mais rien ne se passe dans l'application Java (serveur) et l'application Cliient sur android se plante.
j'ai ajouté les permissions d'utilisation d internet.
ciode coté serveur (application sur PC)
Code:import java.io.DataInputStream; import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String args[]) { // declaration section: // declare a server socket and a client socket for the server // declare an input and an output stream ServerSocket echoServer = null; String line; DataInputStream is; PrintStream os; Socket clientSocket = null; // Try to open a server socket on port 11000 // Note that we can't choose a port less than 1023 if we are not // privileged users (root) try { echoServer = new ServerSocket(11000); } catch (IOException e) { System.out.println(e); } // Create a socket object from the ServerSocket to listen and accept // connections. // Open input and output streams try { clientSocket = echoServer.accept(); is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); // As long as we receive data, echo that data back to the client. while (true) { line = is.readLine(); os.println(line); } } catch (IOException e) { System.out.println(e); } } }
Le code cotée Client (android)
merci pour votre reponsesCode:package com.sdz.tcpclient; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.os.Build; public class MainActivity extends ActionBarActivity implements OnClickListener{ Button b; EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b = (Button) findViewById(R.id.button1); b.setOnClickListener(this); et = (EditText) findViewById (R.id.editText1); et.setText("test1"); } public void client() { // declaration section: // s: our client socket // os: output stream // is: input stream Socket s = null; DataOutputStream os = null; DataInputStream is = null; // Initialization section: // Try to open a socket on port 25 // Try to open input and output streams try { s = new Socket("192.168.1.64", 11000); os = new DataOutputStream(s.getOutputStream()); is = new DataInputStream(s.getInputStream()); et.setText("connected"); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } // If everything has been initialized then we want to write some data // to the socket we have opened a connection to on port 25 if (s != null && os != null && is != null) { try { // The capital string before each colon has a special meaning to SMTP // you may want to read the SMTP specification, RFC1822/3 os.writeBytes("HELO\n"); os.writeBytes("QUIT"); et.setText("sent"); // keep on reading from/to the socket till we receive the "Ok" from SMTP, // once we received that then we want to break. String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); if (responseLine.indexOf("IT") != -1) { break; } } // clean up: // close the output stream // close the input stream // close the socket os.close(); is.close(); s.close(); et.setText("finish"); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } @Override public void onClick(View v) { // TODO Auto-generated method stub client(); } }
-----