简体   繁体   中英

Socket chat programming

I have an issue with my program playing with sockets. I made two threads Receive and Send, made two using AsyncTask in android and two using Thread in computer. My problem here is in android when stop the Receive Thread, Send Thread works just fine and sends the message but when i uncomment Receive, Send doesn't work until i stop the server. Code :

This code only receives messages but it doesn't allow me to Send

public class Chat extends Activity {

    Socket socket;
    Button send;
    PrintStream ps;
    Scanner scanner;
    EditText message;
    TextView chatField;

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

        socket = SocketWrapper.getSocket();

        send = (Button) findViewById(R.id.send);
        message = (EditText) findViewById(R.id.message);
        chatField = (TextView) findViewById(R.id.chatField);


        try {
            ps = new PrintStream(socket.getOutputStream());
            scanner = new Scanner(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Send().execute();
            }
        });

        new Receive().execute();

    }

    private class Send extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... params) {
            ps.println(message.getText().toString());
            publishProgress();
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            chatField.append("You : "+message.getText().toString()+"\n");
            message.setText("");
            super.onProgressUpdate(values);
        }
    }

    private class Receive extends AsyncTask<String,Void,String>{

        String str;
        @Override
        protected String doInBackground(String... params) {
            while(scanner.hasNext()){
                str = scanner.nextLine();
                publishProgress();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            chatField.append("Server : "+str+"\n");
            super.onProgressUpdate(values);
        }
    }
}

But when i comment new Receive().execute(); the Send Thread works just fine.

There's no exception thrown. I'm confused.

If you are running on Android v3.0 at least, you can run only one AsyncTask at a time. When uncommenting, since Receive is running, you cannot run send after.

One alternative is to use a Thread instead (for Receive for instance).

See details in this question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM