概述
使用socket建立长连接
长连接与短链接区别在于是否每次请求交互后断开,不断开则是长连接:
服务端代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServerBin {
public static void main(String[] args) {
MyServerBin myServerBin=new MyServerBin();
myServerBin.startAction();
}
public void startAction(){
ServerSocket serverSocket=null;
try {
serverSocket=new ServerSocket(7878);
while(true){
Socket socket=serverSocket.accept();
new Thread(new MyRuns(socket)).start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (serverSocket!=null) {
serverSocket.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
class MyRuns implements Runnable{
Socket socket;
BufferedReader reader;
BufferedWriter writer;
public MyRuns(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String lineString="";
while( !(lineString=reader.readLine()).equals("bye") ){
System.out.println("客户端:"+socket.hashCode()+"==="+lineString);
writer.write("服务器返回:"+lineString+"n");
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader!=null) {
reader.close();
}
if (writer!=null) {
writer.close();
}
if (socket!=null) {
socket.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
}
客户端代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class MyClickBin {
public static void main(String[] args) {
MyClickBin myClickBin=new MyClickBin();
myClickBin.startAction();
}
void readSocketInfo(BufferedReader reader){
new Thread(new MyRuns(reader)).start();
}
class MyRuns implements Runnable{
BufferedReader reader;
public MyRuns(BufferedReader reader) {
super();
this.reader = reader;
}
@Override
public void run() {
try {
String lineString="";
while( (lineString = reader.readLine())!=null ){
System.out.println(lineString);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void startAction(){
Socket socket=null;
BufferedReader reader=null;
BufferedWriter writer=null;
BufferedReader reader2=null;
try {
socket=new Socket("127.0.0.1", 7878);
reader = new BufferedReader(new InputStreamReader(System.in));
reader2=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
readSocketInfo(reader2);
String lineString="";
while(!(lineString=reader.readLine()).equals("exit")){
writer.write(lineString+"n");
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader!=null) {
reader.close();
}
if (writer!=null) {
writer.close();
}
if (socket!=null) {
socket.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
最后
以上就是爱笑飞鸟为你收集整理的使用socket建立长连接的全部内容,希望文章能够帮你解决使用socket建立长连接所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复