마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

wifi read 1번만 되는 현상 ....

0 추천

송신은 계속되는데 수신이 한번만 이루어 지고 죽는건지 알수가없네요 ㅠㅠ

상대 wifi측은 정상작동합니다.

아래와 같이 ReadThread를 만들었는데 송신후 수신을 받을려고 하는데 1번째 동작에만 송신후 수신이 되고, 이후에는 수신만 됩니다....

아래 코드에 어떻게 수정해야하는지 확인부탁드립니다.

public class MainActivity extends AppCompatActivity {
    OutputStream os;
    InputStream is;
    Socket socket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        final netthread thread = new netthread();
        thread.start();

        send_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String stringdata="1234568"+send_data.getText().toString();
                Log.d("보낼데이터:",stringdata);

                thread.send();

            }
        });
     }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try{
            if(os !=null) {
                os.flush();
            }
            if(socket !=null) {
                socket.close();
            }
        }catch(IllegalArgumentException e){} catch (IOException e) {
            e.printStackTrace();
        }
    }

class netthread extends Thread{

        public netthread(){
            try{
                Log.d("들어오나?","보자");
            }catch (Exception e){
            }
        }

        public void run(){
            request();
        }

        private void request(){

            try {
                Log.d("소켓통신:","소켓");
                //"192.168.4.1"
                socket = new Socket("192.168.0.13",3333);
                Log.d(TAG,"server connected");
                is=socket.getInputStream();
                os=socket.getOutputStream();
                new ReadThread(socket).start();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

public void send() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (os!=null) {
                        try {
                            String stringdata="12345678"+send_data.getText().toString();
                            Log.d("소켓보낼데이터:",stringdata);
                            byte[] data=stringdata.getBytes();
                            int cnt=data.length;
                            byte[] sendingdata=new byte[cnt+1];
                            for(int i=0;i<cnt;i++){
                                sendingdata[i]=data[i];
                            }
                            sendingdata[cnt]=0x0d;
                            os.write(sendingdata);
                            Log.d(TAG, stringdata);
                            send_data.setText("");

                            log_save(stringdata,"send");
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.d(TAG,"send IOE");
                        }
                    }
                }
            }).start();
        }

public class ReadThread extends Thread{

            public Socket socket;
            public ReadThread(Socket socket){
                this.socket = socket;
            }

            @Override
            public void run() {
                while (socket.isConnected()) {
                    byte[] buffer = new byte[128];
                    int count = 0;
                    if (socket != null) {
                        try {
                            String tmp;
                            count = is.read(buffer);
                            tmp = new String(buffer, 0, count, "utf-8");
                            Log.d("받은 데이터:",tmp);
                            read_data.setText("   받은데이터:"+tmp);
                            log_save(tmp,"read");
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.d(TAG, "ReadThread IOE");
                            break;
                        }
                    }
                }
            }
        }

 

쿠쿠부다스 (6,470 포인트) 님이 2019년 1월 30일 질문

1개의 답변

0 추천
blocking socket 방식이라.

readThread의 . count = is.read(buffer); 에서 데이터를 receive 하려고 대기 하고 있어서,

os.write 가 동작 안되는 듯 합니다.

https://altongmon.tistory.com/279 를 참조하셔서 non-blocking socket 같은 것으로 바꾸세요.
익명사용자 님이 2019년 1월 30일 답변
...