-1

the class of client in android application (using emulator localhost), sending a string to my java application (serveur) :

   public class Client_socket  extends MainActivity{
     private static Socket socket;

        public static void lance()
        {
            try
            {
                String host = "localhost";
                int port = 25000;
                InetAddress address = InetAddress.getByName(host);
                socket = new Socket("127.0.0.3", port);

                //Send the message to the server
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);

                String nom = "premiere";

                String sendMessage = ism + "\n";
                bw.write(sendMessage);
                bw.flush();
                System.out.println("Message sent to the server : "+sendMessage);

                //Get the return message from the server
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String message = br.readLine();
                System.out.println("Message received from the server : " +message);
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
            finally
            {
                //Closing the socket
                try
                {
                    socket.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }

}

the class of serveur in java application how recive my string : what the ip and port can i use?, and how can i get my ip?

public class Server
{

    private static Socket socket;

    public static void main(String[] args)
    {
        try
        {

            int port = 25000;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server Started and listening to the port 25000");

            //Server is running always. This is done using this while(true) loop
            while(true)
            {
                //Reading the message from the client
                socket = serverSocket.accept();
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String nom = br.readLine();
                System.out.println("Message received from client is "+nom);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch(Exception e){}
        }
    }
}
  • 1
    Hey, what about investing a minute to find the solution, instead of asking the answer? – A.S. Mar 13 '15 at 10:43
  • Is the server code running on the same machine on which you are using the emulator? – Prerak Sola Mar 13 '15 at 11:57
  • oui prerak sola j'étulise un emulateur pour l'application android et eclips pour java application ,j'ai resolu le probleme il me manquer : new Thread(new Runnable() { @Override public void run() { lance(); // call your network method here } }).start(); et l'adresse ip de mon pc que j'ai récupérer avec ipconfig – Sofiane Belhadj Kacem Mar 14 '15 at 11:17

1 Answers1

1

Make sure you execute network operations in a background thread:

new Thread(new Runnable() {
        @Override
        public void run() {
            lance(); // call your network method here
        }
    }).start();
Dean Wild
  • 5,886
  • 3
  • 38
  • 45