* MainActivity.java

public class MainActivity extends AppCompatActivity {

    EditText input1;
    TextView output1;

    int port = 5001;

    Handler handler = new Handler();

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

        input1 = findViewById(R.id.input1);
        output1 = findViewById(R.id.output1);

        Button sendButton = findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String data = input1.getText().toString();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        send(data); // data 변수 접근을 위해 final 선언
                    }
                }).start();
            }
        });

        Button startServerButton = findViewById(R.id.startServerButton);
        startServerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        startServer();
                    }
                }).start();
            }
        });
    }

    public void startServer() {
        try {
            ServerSocket server = new ServerSocket(port);

            while (true) {
                Socket socket = server.accept();
                InetAddress clientHost = socket.getInetAddress();
                int clientPort = socket.getPort();
                println("클라이언트 연결됨 : " + clientHost + ", " + clientPort);

                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                String input = (String) inputStream.readObject();
                println("데이터 받음 : " + input);

                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                outputStream.writeObject(input + " from server.");
                outputStream.flush();
                println("데이터 보냄");

                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void println(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                output1.append(data + "\n");
            }
        });
    }

    public void send(String data) {
        try {
            Socket socket = new Socket("localhost", port);

            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(data);
            outputStream.flush();

            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            String input = (String) inputStream.readObject();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

* activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#80D8FF"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:textSize="30sp"
            android:layout_marginBottom="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="클라이언트" />

        <EditText
            android:id="@+id/input1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="전송" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FFFF8D"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:textSize="30sp"
            android:layout_marginBottom="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="서버" />

        <Button
            android:id="@+id/startServerButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="서버 시작" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/output1"
                    android:textSize="20sp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            
        </ScrollView>

    </LinearLayout>

</LinearLayout>

* AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

'android' 카테고리의 다른 글

Volley 사용법  (0) 2020.02.04
HttpURLConnection 사용법  (0) 2020.02.04
AsyncTask 사용법  (0) 2020.02.04
Thread 사용법  (0) 2020.02.04
키패드 닫기  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,