总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
pw.close();
os.close();
br.close();
isr.close();
is.close();
socket.close();
serverSocket.close();
}
}
四、客户端
1、创建一个单例Socket管理类·
public class SocketManager {
private static final String TAG = “SocketManager”;
public static Socket clientSocket = null;
private int PORT; //端口号
private String IP;
/**
* 端口
*/
public static SocketManager instance = new SocketManager();
private SocketManager() {
clientSocket = connect();
}
public static SocketManager getInstance() {
return instance;
}
public static Socket getClientSocket() {
return clientSocket;
}
public static void setClientSocket(Socket socket){
clientSocket = socket;
}
/**
* 获取当前socket实例
*/
public Socket connect() {
new Thread(new Runnable() {
@Override
public void run() {
try {
clientSocket = new Socket(IP,PORT);
setClientSocket(clientSocket);
readSocketData(clientSocket);
//sendHeartBeat(clientSocket);
LogUtils.e(“TAG”,“连接成功…”);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return clientSocket;
}
/**
* 发送心跳包
* ThreadPoolManager.getSingleInstance().execute() 线程管理类
* */
public void sendHeartBeat(Socket socket){
ThreadPoolManager.getSingleInstance().execute(new SendData(socket));
}
/**
* 接收服务端数据
* */
public void readSocketData(Socket socket){
ThreadPoolManager.getSingleInstance().execute(new ReceiveData(socket));
}
/**
* 断开连接
*/
public void disConnect() throws IOException {
if (clientSocket != null) {
clientSocket.close();
clientSocket = null;
}
}
/**
* 释放单例, 及其所引用的资源
*/
public void release() throws IOException {
if (instance != null) {
if (clientSocket != null) {
clientSocket.close();
clientSocket = null;
}
instance = null;
}
}
}
2、创建一个SendData线程类来负责发送数据
public class SendData implements Runnable {
private Socket sendsocket;
private PrintWriter printWriter = null;
public SendData(Socket socket) {
this.sendsocket = socket;
}
public void run() {
Socket socket = SocketManager.getClientSocket();
try {
LogUtils.e(“TAG”, “发送的数据包:” + JsonUtil.getData());
sendHeartBeatPackage(socket, “要发送的数据”);//数据包
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* 发送数据包到服务端
*/
public int sendHeartBeatPackage(Socket socket1, String msg) {
int returnCode = 0;
if (socket1.isClosed() || socket1 == null) {
returnCode = 0;
}
try {
printWriter = new PrintWriter(socket1.getOutputStream());
printWriter.println(msg);
printWriter.flush();
socket1.getOutputStream().flush();
returnCode = 1;
} catch (Exception e) {
returnCode = 0;
}
return returnCode;
}
}
3、创建一个ReceiveData接收线程类
public class ReceiveData implements Runnable {
private Socket socket = null;
private byte[] buffer = new byte[1024];
private int bytes;
InputStream mmInStream = null;
public ReceiveData(Socket mSocket) {
this.socket = mSocket;
}
@Override
public void run() {
try {
mmInStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
if ((bytes = mmInStream.read(buffer)) > 0) {
byte[] buf_data = new byte[bytes];
for (int i = 0; i < bytes; i++) {
buf_data[i] = buffer[i];
}
String msg = new String(buf_data);
LogUtils.e(“TAG”, “来自服务端的数据:” + msg);
}
} catch (IOException e) {
try {
mmInStream.close();
LogUtils.e(“TAG”, “读取出现异常…”);
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}
}
}
五、未完待续。。。
最后总结我的面试经验
2021年的金三银四一眨眼就到了,对于很多人来说是跳槽的好机会,大厂面试远没有我们想的那么困难,摆好心态,做好准备,你也可以的。
另外,面试中遇到不会的问题不妨尝试讲讲自己的思路,因为有些问题不是考察我们的编程能力,而是逻辑思维表达能力;最后平时要进行自我分析与评价,做好职业规划,不断摸索,提高自己的编程能力和抽象思维能力。
BAT面试经验
实战系列:Spring全家桶+Redis等
其他相关的电子书:源码+调优
面试真题:
存中…(img-gZ6vXKZE-1715551849353)]
其他相关的电子书:源码+调优
[外链图片转存中…(img-41vYqqxt-1715551849353)]
面试真题:
[外链图片转存中…(img-17JxvyLf-1715551849354)]
[外链图片转存中…(img-bC35x6x1-1715551849354)]