1. 실행 결과 화면

      


2. 구현

- SocketServer

시스템 리소스가 적으면 시스템이 액티비티를 강제로 종료시킬 수 있다.

이렇게 되면 서버가 종료가 되기 때문에 서버를 액티비티가 아닌 서비스로 구성한다. 


- SocketClient

핸들러를 이용하여 UI에 접근해, 서버에 받아온 데이터를 텍스트에 보여준다.



3. SocketServer Code

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="qlyh8.tistory.com.socketserver">
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <service
            android:name=".ServerService"
            android:enabled="true"
            android:exported="true"></service>
    </application>
 
</manifest>
cs


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package qlyh8.tistory.com.socketserver;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(new Intent(getApplicationContext(), ServerService.class));
            }
        });
    }
}
 
cs


ServerService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package qlyh8.tistory.com.socketserver;
 
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
 
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerService extends Service {
 
    public ServerService() {
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        ServerThread thread = new ServerThread();
        thread.start();
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
 
    class ServerThread extends Thread {
        int port = 5001;
        @Override
        public void run() {
            try {
                // 서버 소켓을 생성하고 5001번 포트를 연다.
                ServerSocket serverSocket = new ServerSocket(port);
                Log.d("asd""[Server] 서버 실행됨");
 
                while (true){
                    // 클라이언트 접속 전까지 대기 상태로 들어간다.
                    // 클라이언트 접속 시 소켓 객체가 리턴된다.
                    // 이 소켓 객체를 통해 클라이언트가 요청한 정보를 받을 수 있다.
                    Socket socket = serverSocket.accept();
 
                    // 들어오는 데이터를 읽는다.
                    ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream());
                    Object inObject = inStream.readObject();
                    Log.d("asd""[Server] 들어온 데이터: " + inObject);
 
                    // 클라이언트에게 데이터를 보낸다.
                    ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
                    outStream.writeObject(inObject + " World!");
                    outStream.flush();  // 버퍼에 남아있을 수 있는 outStream 을 버림
                    Log.d("asd""[Server] 클라이언트에 데이터 보냄");
 
                    socket.close(); // 소켓 연결 종료
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
 
cs



4. SocketClient Code

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="qlyh8.tistory.com.socketclient">
 
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
cs


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package qlyh8.tistory.com.socketclient;
 
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
 
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
 
public class MainActivity extends AppCompatActivity {
 
    TextView textView;
    Handler handler = new Handler();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView = findViewById(R.id.text_view);
 
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClientThread thread = new ClientThread();
                thread.start();
            }
        });
    }
 
    class ClientThread extends Thread {
        String host = "localhost";
        int port = 5001;
        @Override
        public void run() {
            try {
                // 호스트 정보와 포트 정보를 넣고 소켓을 생성한다.
                Socket socket = new Socket(host, port);
 
                // 요청을 위한 통로를 생성한다.
                ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
                outStream.writeObject("Hello");
                outStream.flush();
                Log.d("asd""[Client] 서버로 보냄");
 
                // 데이터가 들어오는 통로를 생성한다.
                ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream());
                final Object inObject = inStream.readObject();
                Log.d("asd""[Client] 받은 데이터: " + inObject);
 
                // 텍스트 변경
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("전달받은 값: " + inObject);
                    }
                });
 
                socket.close(); // 소켓 연결 종료
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
 
cs





출처: https://www.edwith.org/boostcourse-android/lecture/17088/

'Android' 카테고리의 다른 글

HTTP - 2. 웹으로 요청하기  (0) 2018.07.30
HTTP - 1. HTTP란  (0) 2018.07.30
소켓 (Socket) - 2. 소켓 사용하기 (1)  (0) 2018.07.25
소켓 (Socket) - 1. 소켓이란  (0) 2018.07.25
AsyncTask  (0) 2018.07.25

+ Recent posts