#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Android server client communication issue


    Hi,

    I'd just like to say firstly that I was told to move my thread to this forum...

    secondly...

    I'm hoping someone here can help me, I am trying to create a basic application that transmits GPS co ordinates of the device to a server. I am a complete noobie to socket programming (well, I covered the bare basics in college this year).

    As a stepping stone, I am following a tutorial, that creates the server, and the android client, it is designed to send an input from the user to the server and the server prints it on the console.

    I'm becoming quite familar with how to set up the basics of the server and the client i.e. opening ports etc. The problem with this is, when hit send, the app crashes, when tested on the emulator, there is obviously an issue, but I can't see it.

    When I test with device, and hit send, there is nothing printed to the console. I've attached both the client and server code below, the ip in the code below is for the emulator, I change this to my device ip with testing with it.

    ****SERVER****
    Code:
    public class Additional_Server {  
        private static ServerSocket serverSocket;  
        private static Socket clientSocket;  
        private static InputStreamReader inputStreamReader;  
        private static BufferedReader bufferedReader;  
        private static String message;  
        
      public static void main(String[] args) {  
            try {  
                serverSocket = new ServerSocket(2001); // Server socket  
            } catch (IOException e) {  
                System.out.println("Could not listen on port: 2001");  
            }  
            System.out.println("Server started. Listening to the port 2001");  
            while (true) {  
                try {  
                    clientSocket = serverSocket.accept(); // accept the client connection  
                    inputStreamReader = new InputStreamReader(  
                            clientSocket.getInputStream());  
                    bufferedReader = new BufferedReader(inputStreamReader); // get the client message  
                    message = bufferedReader.readLine();  
                    System.out.println(message);  
                    inputStreamReader.close();  
                    clientSocket.close();  
                } catch (IOException ex) {  
                    System.out.println("Problem in message reading");  
                }  
            }  
        }  
    }
    ****CLIENT****

    Code:
    import android.os.Bundle;  
    import android.app.Activity;  
    import java.io.IOException;  
    import java.io.PrintWriter;  
    import java.net.Socket;  
    import java.net.UnknownHostException;  
    import android.view.View;  
    import android.widget.Button;  
    import android.widget.EditText;  
      
    public class TCPclient extends Activity {  
        private Socket client;  
        private PrintWriter printwriter;  
        private EditText textField;  
        private Button button;  
        private String messsage;  
         
     @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_tcpclient);  
            textField = (EditText) findViewById(R.id.Msg); // reference to the text field  
            button = (Button) findViewById(R.id.bSend); // reference to the send button  
            // Button press event listener  
            button.setOnClickListener(new View.OnClickListener() {  
                public void onClick(View v) {  
                    messsage = textField.getText().toString(); // get the text message on the text field  
                    textField.setText(""); // Reset the text field to blank  
                    try {  
                        client = new Socket("127.0.0.1", 2001); // connect to server  
                        printwriter = new PrintWriter(client.getOutputStream(),  
                                true);  
                        printwriter.write(messsage); // write the message to output stream  
                        printwriter.flush();  
                        printwriter.close();  
                        client.close(); // closing the connection  
                    } catch (UnknownHostException e) {  
                        e.printStackTrace();  
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                }  
            });  
        }  
    }
    I really would appreciate any help, I'm pulling my hair out here. Any specifics would be great. Thanks in advance!!!

    I have cross posted this in
    http://androidforums.com/application-development/679212-android-server-client-communication-issue.html#post5456517

    but only because I am desperate, I've been trying to get this going for ages now...so any help is really appreciated
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Can anyone help on this??

    I've simplified this and I still can't get it working..

    SERVER

    Code:
    public class Additional_Server {  
      
        private static String message;  
      
        public static void main(String[] args) throws Exception {  
      
              
                Socket s;  
                ServerSocket ss = new ServerSocket(2001);  
                  
                  
            System.out.println("Server started. Listening to the port 2001");  
      
              
              
            while (true) {  
                try {  
                    System.out.println("Server: waiting for connection ..");  
                      
                    s = ss.accept();  
                    InputStream in = s.getInputStream();  
                    Scanner r = new Scanner(in);  
                    OutputStream o = s.getOutputStream();  
                    PrintWriter p = new PrintWriter(o);  
      
                    String inputLine;  
                    inputLine = r.nextLine();  
                    p.println("Hello " + inputLine + " from Gary");  
                    p.close();  
                  
                  
              
                } catch (IOException ex) {  
                    System.out.println("Problem in message reading");  
                }  
            }  
      
        }  
    }
    CLIENT

    Code:
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.OutputStream;  
    import java.io.PrintWriter;  
    import java.net.Socket;  
    import java.net.UnknownHostException;  
    import java.util.Scanner;  
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.widget.Button;  
    import android.widget.TextView;  
      
    public class MainActivity extends Activity {  
      
        private Socket s;  
        private PrintWriter p;  
        TextView display;  
        Button test;  
      
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            test = (Button) findViewById(R.id.test);  
            display = (TextView) findViewById(R.id.Sdisplay);  
      
            test.setOnClickListener(new View.OnClickListener() {  
      
                public void onClick(View v) {  
                    // TODO Auto-generated method stub  
      
                    try {  
                        s = new Socket("192.168.1.*", 2001); // connect to server  
                        OutputStream o = s.getOutputStream();  
                        PrintWriter p = new PrintWriter(o);  
                        InputStream in = s.getInputStream();  
                        Scanner r = new Scanner(in);  
      
                        p.println("Gary");  
                        p.flush();  
                          
                          
                    } catch (UnknownHostException e) {  
                        e.printStackTrace();  
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                }  
            });  
      
        }  
    }

    I'm just trying to get the printout to the console, but nothing is happening. I'm trying to do a project on this for college, we haven't done android programming or socket programming before...

    What really bothers me here is that it looks right and I can't seem to see what the issue is.

    I really would appreciate some of your thoughts or even what you might do in this situation?

    I have been stuck on this hurdle for weeks now, and normally I don't mind plugging away, but time is running out for me...

IMN logo majestic logo threadwatch logo seochat tools logo