Client
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private TextView mTextViewReplyFromServer;
private EditText mEditTextSendMessage;
final Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mEditTextSendMessage = findViewById(R.id.edt_send_message);
mTextViewReplyFromServer = findViewById(R.id.tv_reply_from_server);
findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(mEditTextSendMessage.getText().toString());
}
});
}
private void sendMessage(final String message) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// 对应好服务端 IP 地址 和 端口
Socket socket = new Socket("172.27.118.2", 9102);
// 由输出流得到 Writer 并写信息
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.println(message);
printWriter.flush();
// 由输入流得到 Reader 并读信息
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
final String read = bufferedReader.readLine();
System.out.println("read string from server: " + read);
updateUI(read);
// 依次关闭
printWriter.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
private void updateUI(final String data) {
// Can't create handler inside thread that has not called Looper.prepare()
mHandler.post(new Runnable() {
@Override
public void run() {
String current = mTextViewReplyFromServer.getText().toString();
if (data.trim().length() != 0) {
mTextViewReplyFromServer.setText(current + "\nFrom Server : " + data);
}
}
});
}
}
Server
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mButtonStartReceiving;
private Button mButtonStopReceiving;
private TextView mTextViewDataFromClient;
private boolean isEnd = false;
final Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mButtonStartReceiving = findViewById(R.id.btn_start_receiving);
mButtonStopReceiving = findViewById(R.id.btn_stop_receiving);
mTextViewDataFromClient = findViewById(R.id.tv_data_from_client);
mButtonStartReceiving.setOnClickListener(this);
mButtonStopReceiving.setOnClickListener(this);
}
private void startServerSocket() {
Thread thread = new Thread(new Runnable() {
private String stringData = null;
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(9102);
while (!isEnd) {
//Server is waiting for client here, if needed
Socket socket = serverSocket.accept();
// 由输入流得到 Reader 并读信息
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
final String read = bufferedReader.readLine();
System.out.println("read string from client: " + read);
// 由输出流得到 Writer 并写信息
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.println("FROM SERVER - " + read.toUpperCase());
printWriter.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateUI(read);
if (read.equalsIgnoreCase("STOP")) {
isEnd = true;
printWriter.close();
socket.close();
break;
}
// 依次关闭
printWriter.close();
socket.close();
}
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
private void updateUI(final String data) {
// Can't create handler inside thread that has not called Looper.prepare()
mHandler.post(new Runnable() {
@Override
public void run() {
String current = mTextViewDataFromClient.getText().toString();
if (data.trim().length() != 0) {
mTextViewDataFromClient.setText(current + "\n" + "From Client : " + data);
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start_receiving:
startServerSocket();
mButtonStartReceiving.setEnabled(false);
mButtonStopReceiving.setEnabled(true);
break;
case R.id.btn_stop_receiving:
//stopping server socket logic you can add yourself
mButtonStartReceiving.setEnabled(true);
mButtonStopReceiving.setEnabled(false);
break;
default:
break;
}
}
}
网友评论