Hi,

I'm hoping someone out there 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 an online 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****

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****

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();
                }
            }
        });

    }
}

Any pointers, advice or help is greatly appreciated as what I'm seeing looks correct, but there's obviously something incorrect. Thanking you in advance!!!

Gary

In Android you cannot test against localhost (127.0.0.1). However you can test against IP address of your local wifi at home. So find out what is IP of your PC (mostlikely something like 192.168.0.1) make sure you can communicate with port 2001

Hi Peter,
Thank you for your reply, when I change the ip to that of my pc, the app still crashes. When I run it on my device, I change the ip to the devices ip, when I click send the app doesn't crash, but it doesn't print to the console either.

Can you see any other discrepancies in my code that could be causing this problem? I'm sorry to be asking, unfortunately I'm very new to android.

Thanks for your help!

I will have to test the code. However this sort task would be normaly delegated to web server and you would simple post to URL with coordinates and let server to extract data and storem them in DB

Like apache or similar?

Apache HTTPD is the main server on which you can run Java server like Tomcat, Glassfish, JBoss etc to run Java web service, or just deploy PHP. the main part is that you create request like http://wwww.somewebsite.com/coordinates?param1=xyz&param2=abc. Server will react to POST request on http://wwww.somewebsite.com/coordinates and extract param 1 and param2 and store them in DB.
Sopick up web framework/technology you are most familiar with so you can write that bit of code for server side extraction

you've lost me, I'm new to this, I'm trawling the web and I can't seem to come up with anything other than google cloud storage...if you could point me in the right direction I'd appreciate it

Lets try, can yuo code either in PHP / .NET / Ruby or did any Java web application?

I can code in php, not expert, i'd imagine intermediate skills, I have taught myself

Are you able to create page "coordinates" that will listen for POST reques and then from request extract provided parameters? (Not sure how this works in PHP, but in Java you would setup doPost method and then inside would extract parameters, there is method that will return you all names of the parameters and another method where you provide name of parameter and get back value)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.