- AndroidManifest.xml - 아래 구문을 추가
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
- 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="com.example.riu.chatclient.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="IP"
android:textColor="#000000"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ip_EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP 입력"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Port" android:textColor="#000000"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="1"/>
<EditText
android:id="@+id/port_EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PORT 입력"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="닉네임" android:textColor="#000000"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="2"/>
<EditText
android:id="@+id/port_NameText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="닉네임 입력"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/connect_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="연결" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/showText_TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:textColor="#000000"/>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/editText_massage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:ems="10"
android:hint="채팅 메시지 입력" />
<Button
android:id="@+id/Button_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="전송" />
</LinearLayout>
</LinearLayout>
- MainActivity.java -
package com.example.riu.chatclient;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity {
TextView showText;
Button connectBtn;
Button Button_send;
EditText ip_EditText;
EditText port_EditText;
EditText port_NameText;
EditText editText_massage;
Handler msghandler;
SocketClient client;
ReceiveThread receive;
SendThread send;
Socket socket;
String nickname ="";
LinkedList<SocketClient> threadList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ip_EditText = (EditText) findViewById(R.id.ip_EditText);
port_EditText = (EditText) findViewById(R.id.port_EditText);
connectBtn = (Button) findViewById(R.id.connect_Button);
showText = (TextView) findViewById(R.id.showText_TextView);
editText_massage = (EditText) findViewById(R.id.editText_massage);
Button_send = (Button) findViewById(R.id.Button_send);
port_NameText = (EditText)findViewById(R.id.port_NameText);
threadList = new LinkedList<MainActivity.SocketClient>();
this.setTitle("채팅서버 클라이언트_안드로이드");
// ReceiveThread를통해서 받은 메세지를 Handler로 MainThread에서 처리(외부Thread에서는 UI변경이불가)
msghandler = new Handler() {
@Override
public void handleMessage(Message hdmsg) {
if (hdmsg.what == 1111) {
showText.append(hdmsg.obj.toString() + "\n");
}
}
};
// 연결버튼 클릭 이벤트
connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Client 연결부
nickname = port_NameText.getText().toString();
client = new SocketClient(ip_EditText.getText().toString(),
port_EditText.getText().toString());
threadList.add(client);
client.start();
}
});
//전송 버튼 클릭 이벤트
Button_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//SendThread 시작
if (editText_massage.getText().toString() != null) {
send = new SendThread(socket);
send.start();
//시작후 edittext 초기화
editText_massage.setText("");
}
}
});
}
class SocketClient extends Thread {
boolean threadAlive;
String ip;
String port;
DataOutputStream output = null;
public SocketClient(String ip, String port) {
threadAlive = true;
this.ip = ip;
this.port = port;
}
@Override
public void run() {
try {
// 연결후 바로 ReceiveThread 시작
socket = new Socket(ip, Integer.parseInt(port));
output = new DataOutputStream(socket.getOutputStream());
receive = new ReceiveThread(socket);
receive.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ReceiveThread extends Thread {
private Socket sock = null;
DataInputStream input;
public ReceiveThread(Socket socket) {
this.sock = socket;
try{
input = new DataInputStream(sock.getInputStream());
}catch(Exception e){
}
}
// 메세지 수신후 Handler로 전달
public void run() {
try {
while (input != null) {
String msg;
int count = input.available();
byte[] rcv = new byte[count];
input.read(rcv);
msg = new String(rcv);
if (count > 0) {
Log.d(ACTIVITY_SERVICE, "test :" +msg);
Message hdmsg = msghandler.obtainMessage();
hdmsg.what = 1111;
hdmsg.obj = msg;
msghandler.sendMessage(hdmsg);
Log.d(ACTIVITY_SERVICE,hdmsg.obj.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class SendThread extends Thread {
Socket socket;
String sendtmp= editText_massage.getText().toString();
String sendmsg = "["+nickname+"] " + sendtmp +"\n";
DataOutputStream output;
public SendThread(Socket socket) {
this.socket = socket;
try {
output = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
}
}
public void run() {
try {
// 메세지 전송부
Log.d(ACTIVITY_SERVICE, "11111");
if (output != null) {
if (sendmsg != null) {
output.write(sendmsg.getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException npe) {
npe.printStackTrace();
}
}
}
}