概述
1.android端
先加相关权限:
<uses-permission android:name="android.permission.INTERNET" />
public class MainActivity extends AppCompatActivity {
TextView xianshi;
DataOutputStream out = null;
DataInputStream in = null;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
xianshi.setText(ShuJu.shuju);
if(msg.what == 1){
startListenserver(in); //长连接,接收心跳包
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xianshi = (TextView) findViewById(R.id.xianshi);
Log.i("mydate" , "kaishi1");
startsocket();
Log.i("mydate" , "kaishi2");
}
private void startsocket() {
new Thread(new Runnable() {
Socket socket = null;
@Override
public void run() {
try {
socket = new Socket("192.168.31.223" , 9898);
Log.i("mydate", "连接服务器成功");
ShuJu.shuju += "连接服务器" + "n";
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
//startListenserver(in); //长连接,接收心跳包
handler.sendEmptyMessage(1);
} catch (IOException e) {
e.printStackTrace();
}/*
finally {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
Log.i("mydate" , "出错4");
}
}*/
}
}).start();
}
private void startListenserver(final DataInputStream in) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String response = "";
while((response = in.readUTF()) != null) { //一直从服务器接受消息
System.out.println(response);
final String finalResponse = response;
ShuJu.shuju += finalResponse + "n";
handler.sendEmptyMessage(2);
}
} catch (IOException e) {
Log.i("mydate" , "出错3");
}
}
}).start();
}
public void fasong(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try{
out.writeUTF("一条消息");
out.flush();
} catch(Exception e){
Log.i("mydate" , "出错1");
}
}
}).start();
}
}
2.服务端
public class SocketServer {
public static void main(String[] args) {
SocketServer socketServer = new SocketServer();
socketServer.startServer();
}
public void startServer() {
try {
ServerSocket serverSocket = new ServerSocket(9898);
while (true) { //一直等待客户端连接
Socket socket = serverSocket.accept();
System.out.println(socket.hashCode() + "-" + "接入服务器");
manageConnection(socket);
}
} catch (Exception e) {
System.out.println("出错1");
}
}
public void manageConnection(Socket socketk) {
new Thread(new Runnable() {
Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
@Override
public void run() {
try {
socket = socketk;
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
new java.util.Timer().schedule(new TimerTask() {//定时发送
@Override
public void run() {
try {
System.out.println(socketk.hashCode() + "向客户端发送心跳包");
out.writeUTF(socketk.hashCode() + "心跳包");
out.flush(); // 一定要这行语句表示,此次发送完全完成了,不然客户端会一直等待
} catch (IOException e) {
System.out.println("出错4");
}
}
}, 3000, 3000);
String receivedMsg;
while ((receivedMsg = in.readUTF()) != null) {
System.out.println(socketk.hashCode() + "-" + receivedMsg);
out.writeUTF(socketk.hashCode() + "这是服务器回复的消息" + "n");
out.flush();
}
} catch (Exception e) {
System.out.println("出错2");
} finally {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
System.out.println("出错3");
}
}
}
}).start();
}
}
最后
以上就是专一羽毛为你收集整理的Socket心跳包的实现的全部内容,希望文章能够帮你解决Socket心跳包的实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复