cracq
cracq

Reputation: 91

how to work with sockets, activities and views?

I'm new to programming in android and I need help to build an application. I want two phones to connect to each other, one being the client and the other being the server. I want the client to have 2 stages. In the first the user would input the ip of the server y click on a button to stablish the connection. In the second one the user would input a message and click on a button to send it to the server.

The code shown bellow is to send the string "message" to the server but as I was saying I want the user to be able to input the string. I don't know how to tackle his issue, do I need a second activity to be called once the connection is established in the activity I show bellow? In that case I wouldn't know how to pass a socket to another activity, I only know how to pass strings. Besides, I would need another button and therefore a new OnClickListener and I would still need to pass the socket to that function.

Without using a second activity I don't know how to make the second input field (the one where the user would input the message to send to the server) to show up once the connection is established. The views (layout.xml) for this activity are already associated to the same, I can't just clear the screen and create a new EditText field on the fly.

I hope I made myself clear.

Thanks in advance

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);

    serverIp = (EditText) findViewById(R.id.server_ip);
    connectPhones = (Button) findViewById(R.id.connect_phones);
    connectPhones.setOnClickListener(connectListener);       

}

private OnClickListener connectListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (!connected) {
            serverIpAddress = serverIp.getText().toString();
            if (!serverIpAddress.equals("")) {
                Thread cThread = new Thread(new ClientThread());
                cThread.start();
            }
        }
    }
};

public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
            connected = true;
            while (connected) {
                try {
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                .getOutputStream())), true);
                    **out.println("messageToSend");**
                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }
            }
            socket.close();
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}
}

Upvotes: 0

Views: 148

Answers (3)

Jambaaz
Jambaaz

Reputation: 2848

I don't think , you need a second activity to send message over socket.

Anyways , It depends on you design --

you can have two editTexts at a same but their visibility would be different.

 EditText serverIpAddressET;  
 EditText messageET ;  // Deafult visibility Gone

serverIpAddressET can be visible when user has not provided socket IP. Once connection is established , you can change the visibility of serverIpAddressET to the Gone and make Visible the messageET .

Upvotes: 1

brendosthoughts
brendosthoughts

Reputation: 1703

Not entirely sure what you're looking for here but if I understand correctly you are woundering how to send a message. Simply add

     message2send= (EditText) findViewById(R.id.user_message); 

and then where you have

         **out.println("messageToSend");**

replace with

         **out.println(message2send);** 

and add an extra edit text field to your layout.

It should be noted that the way you've gone about this is not ideal as the other phone (server) would have to be listening to the socket in order to receive the message which would cause additional data charges as well as wasting battery power (traditonal in mobile a server is between the 2 phones to store the message in case user if offline or phone is dead)

Hope this helps non the less!

Upvotes: 1

baboo
baboo

Reputation: 2023

Well a quick fix would be to hide the currently Displayed EditText and Button via

 view.setVisiblility(View.GONE)

and display the other editText and button via

 view.setVisibility(View.Visible)

and use same activity...

But a better solution would be to open the socket for communication in a service look in to android Service with Sockets... start the service in first activity and when connection is established shift too second activity and bind the activity with service so there can be communication btw activity and service...

Upvotes: 1

Related Questions