[챕터 2-9-1] 소켓
2020. 2. 19. 08:38ㆍAndroid/Android 챕터 2-9
반응형
결과화면
서버 시작 버튼 클릭하여 서버 실행 후
클라이언트 메세지 입력 후 전송
프로젝트명 : MySocket
하위의 각 레이아웃
레이아웃 높이 : 0dp
레이아웃 무게 : 1
각 텍스트뷰
- 레이아웃 마진 바틈 : 30dp
클라이언트 입력상자 id 부여 : input1
전송버튼 id 부여 : sendButton
서버 텍스트뷰 id 부여 : output1
서버 시작 버튼 id 부여 : startServerButton
MainActivity.java
더보기
package org.minokuma.mysocket;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
EditText input1;
TextView output1;
@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);
}
}).start();
}
});
}
public void send(String data){
int port = 5001;
try {
Socket socket = new Socket("localhost", port);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(data);
objectOutputStream.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
String input = (String) objectInputStream.readObject();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
MainActivity.java
더보기
package org.minokuma.mysocket;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
EditText input1;
TextView output1;
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);
}
}).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(){
int port = 5001;
try {
ServerSocket serverSocket = new ServerSocket(port);
while(true){
Socket socket = serverSocket.accept();
InetAddress inetClientAddress = socket.getLocalAddress();
int clientPort = socket.getPort();
println("연결된 클라이언트 정보 : " + inetClientAddress + ", " + clientPort);
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
String input = (String) objectInputStream.readObject();
println("받은 데이터 : " + input);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(input + " from server.");
objectOutputStream.flush();
println("데이터 보냄");
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException 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){
int port = 5001;
try {
Socket socket = new Socket("localhost", port);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(data);
objectOutputStream.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
String input = (String) objectInputStream.readObject();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
AndroidManifest.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.minokuma.mysocket">
<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>
반응형
'Android > Android 챕터 2-9' 카테고리의 다른 글
[챕터 2-9-3] 볼리 ★★★ (0) | 2020.02.19 |
---|---|
[챕터 2-9-2] HTTP (0) | 2020.02.19 |