package com.crazy.tcpdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Looper;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "tcp";
EditText mEdServerIp;
EditText mEdServerPort;
EditText mEdServerSendMessage;
ListView mLvServerMsg;
EditText mEdClientIp;
EditText mEdClientPort;
EditText mEdClientSendMessage;
ListView mLvClientMsg;
Button btn_start_server;
Button btn_server_send;
Button btn_server_clear_message;
Button btn_start_client;
Button btn_client_send;
Button btn_client_clear_message;
private List<String> mServerMessageList;
private List<String> mClientMessageList;
private ArrayAdapter<String> mServerMessageAdapter;
private ArrayAdapter<String> mClientMessageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//android.os.NetworkOnMainThreadException
//简单暴力,强制使用,代码修改简单(但是非常不推荐)
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
initView();
initData();
}
private void initView() {
mEdServerIp = findViewById(R.id.ed_server_ip);
mEdServerPort = findViewById(R.id.ed_server_port);
mEdServerSendMessage = findViewById(R.id.ed_server_send_message);
mLvServerMsg = findViewById(R.id.lv_server_msg);
mEdClientIp = findViewById(R.id.ed_client_ip);
mEdClientPort = findViewById(R.id.ed_client_port);
mEdClientSendMessage = findViewById(R.id.ed_client_send_message);
mLvClientMsg = findViewById(R.id.lv_client_msg);
btn_start_server = findViewById(R.id.btn_start_server);
btn_server_send = findViewById(R.id.btn_server_send);
btn_server_clear_message = findViewById(R.id.btn_server_clear_message);
btn_start_client = findViewById(R.id.btn_start_client);
btn_client_send = findViewById(R.id.btn_client_send);
btn_client_clear_message = findViewById(R.id.btn_client_clear_message);
btn_start_server.setOnClickListener(this);
btn_server_send.setOnClickListener(this);
btn_server_clear_message.setOnClickListener(this);
btn_start_client.setOnClickListener(this);
btn_client_send.setOnClickListener(this);
btn_client_clear_message.setOnClickListener(this);
}
private void initData() {
String serverIp = getLocalIpAddress();
Log.e(TAG, "initData serverIp=" + serverIp);
mEdServerIp.setText(serverIp);
mEdClientIp.setText(serverIp);
mServerMessageList = new ArrayList<>();
mClientMessageList = new ArrayList<>();
mServerMessageAdapter = new ArrayAdapter<>(this, R.layout.item_log, R.id.item_log, mServerMessageList);
mClientMessageAdapter = new ArrayAdapter<>(this, R.layout.item_log, R.id.item_log, mClientMessageList);
mLvServerMsg.setAdapter(mServerMessageAdapter);
mLvClientMsg.setAdapter(mClientMessageAdapter);
}
@Override
public void onBackPressed() {
super.onBackPressed();
// 杀死该应用进程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
private void getServerPort() {
mServerPort = Integer.parseInt(mEdServerPort.getText().toString().trim());
}
private void getServerSendMessage() {
mServerSendMessage = mEdServerSendMessage.getText().toString().trim();
}
private void getClientPort() {
mClientPort = Integer.parseInt(mEdClientPort.getText().toString().trim());
}
private void getClientIp() {
mClientIp = mEdClientIp.getText().toString().trim();
}
private void getClientSendMessage() {
mClientSendMessage = mEdClientSendMessage.getText().toString().trim();
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_start_server:
getServerPort();
new ServerSocketThread().start();
break;
case R.id.btn_server_send:
getServerSendMessage();
serverSendMessage();
break;
case R.id.btn_server_clear_message:
mServerMessageList.clear();
mServerMessageAdapter.notifyDataSetChanged();
break;
case R.id.btn_start_client:
getClientIp();
getClientPort();
new ClientSocketThread().start();
break;
case R.id.btn_client_send:
getClientSendMessage();
clientSendMessage();
break;
case R.id.btn_client_clear_message:
mClientMessageList.clear();
mClientMessageAdapter.notifyDataSetChanged();
break;
}
}
//=================================================================================
// Server
//=================================================================================
private int mServerPort;
private String mServerSendMessage;
private List<Socket> mSocketList = new ArrayList<>();
private List<ServerReceiveRunnable> mServerReceiveRunnableList = new ArrayList<>();
class ServerSocketThread extends Thread {
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(mServerPort);
String logStart = getTime()
+ " 服务器已启动...";
logServer(logStart);
while (true) {
Socket socket = serverSocket.accept();
boolean isContain = false;
int index = 0;
for (Socket s : mSocketList) {
if (s.getInetAddress().equals(socket.getInetAddress())) {
isContain = true;
break;
}
index++;
}
if (isContain) {
Log.d(TAG, "isContain");
mServerReceiveRunnableList.remove(index).close();
mSocketList.remove(index);
}
mSocketList.add(socket);
String logAccept = getTime()
+ " 服务端地址:" + socket.getLocalAddress()
+ " 客户端地址:" + socket.getInetAddress()
+ " 新的客户端接入,当前客户端数:" + mSocketList.size();
Crazy程序猿2020
- 粉丝: 532
- 资源: 31
最新资源
- 毕设和企业适用springboot社交应用平台类及金融数据分析平台源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及交通信息平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及用户数据分析平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及用户体验优化平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及用户行为分析平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及运动管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及智能化系统源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及社交媒体平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及人工智能客服平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及社交游戏平台源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及跨平台销售系统源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及民生服务平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及生活服务平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及食品配送管理平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及社区服务平台源码+论文+视频.zip
- 毕设和企业适用springboot社交应用平台类及无人驾驶系统源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页