#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'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
    anyone??
  4. #3
  5. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0
    You should moved this tread in programming section.

    <edit note>De-linked link drop attempt. Poster has been warned about this behavior twice already. As the ump may say, "STRIIIIKE 3 and you're out"</edit>
    Last edited by Scorpions4ever; January 26th, 2013 at 02:19 PM.

IMN logo majestic logo threadwatch logo seochat tools logo