android server client communication issue

GaryD

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

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

GaryD

New Member
Anyone???! I'd really appreciate help here as I'm stuck and I don't have the e lefties required to sort this myself:sign0163:
 
Upvote 0

DSDominno

Member
Licensed User
Longtime User
How do the server look up the IP-address of the client

Hello
(Sorry for my bad English)

I have build a TCP server application (ServerSocket and AsyncStreams) that run very well with multiple clients in the same time.

But How do the server to retrieve the IP-address of the connected clients when the event sockServeurWeb_NewConnection fire ?

because i want to block connection for IP non authorized.

Thank you for yours responses
 
Upvote 0
Top