package qzjiami.chatapp;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private ServerSocket serverSocket = null;
private TextView tw2;
private EditText et;
private Button btn;
private InputStream inputStream;
String data ,ip;
StringBuffer stringBuffer = new StringBuffer();
Socket sockets;
private OutputStream outputStream = null;
public Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what){
case 1:
tw2.setText(msg.obj.toString());
break;
case 2:
tw2.setText(msg.obj.toString());
break;
}
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tw2 = (TextView) findViewById(R.id.textView2);
et = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.button);
ip = "192.168.102.177";
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data = et.getText().toString();
if(data == null){
Toast.makeText(MainActivity.this,"please input Sending Data", Toast.LENGTH_SHORT).show();
}else {
//在后面加上 '\0' ,是为了在服务端方便我们去解析;
data = data + '\0';
}
Thread thread = new Thread(){
@Override
public void run() {
super.run();
try {
sockets = new Socket(ip,8000);
outputStream = sockets.getOutputStream();
outputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread.start();
}
});
receiveData();
}
/*
服务器端接收数据
需要注意以下一点:
服务器端应该是多线程的,因为一个服务器可能会有多个客户端连接在服务器上;
*/
private void receiveData() {
Thread thread = new Thread(){
@Override
public void run() {
super.run();
/*
指明端口号
*/
try {
serverSocket = new ServerSocket(8000);
}catch (IOException e){
e.printStackTrace();
}
GetIpAddress.getLocalIpAddress(serverSocket);
Message message_1 = handler.obtainMessage();
message_1.what = 1;
message_1.obj = "IP:" + GetIpAddress.getIP() + " PORT: " + GetIpAddress.getPort();
handler.sendMessage(message_1);
/*
循环启动服务
*/
boolean b = true;
while (b){
Socket socket = null;
try {
if (!serverSocket.isClosed()){
socket = serverSocket.accept();
inputStream = socket.getInputStream();
new ServerThread(socket,inputStream).start();
}else {
b = false;
socket.close();
return;
}
}catch (Exception e){
e.printStackTrace();
}
}
}
};
thread.start();
}
class ServerThread extends Thread{
private Socket socket;
private InputStream inputStream;
private StringBuffer stringBuffer = MainActivity.this.stringBuffer;
public ServerThread(Socket socket,InputStream inputStream){
this.socket = socket;
this.inputStream = inputStream;
}
@Override
public void run() {
int len;
byte[] bytes = new byte[20];
boolean isString = false;
try {
//在这里需要明白一下什么时候其会等于 -1,其在输入流关闭时才会等于 -1,
//并不是数据读完了,再去读才会等于-1,数据读完了,最结果也就是读不到数据为0而已;
while ((len = inputStream.read(bytes)) != -1) {
for(int i=0; i<len; i++){
if(bytes[i] != '\0'){
stringBuffer.append((char)bytes[i]);
}else {
isString = true;
break;
}
}
if(isString){
Message message_2 = handler.obtainMessage();
message_2.what = 2;
message_2.obj = stringBuffer;
handler.sendMessage(message_2);
isString = false;
}
}
//当这个异常发生时,说明客户端那边的连接已经断开
} catch (IOException e) {
e.printStackTrace();
try {
inputStream.close();
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
try {
serverSocket.close();
if (outputStream != null){
outputStream.close();
sockets.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}