展开全部
socket一般调用accept方法等待连接:
public class ServerThread extends Thread{
ServicePanel servicePanel = null;
ServerSocket serverSocket = null;
Socket socket = null;
public ServerThread(ServicePanel servicePanel,ServerSocket serverSocket) {
this.servicePanel = servicePanel;
this.serverSocket = serverSocket;
}
public void run(){
while (true) {
try {
socket = serverSocket.accept();//等待连接
new ReciveFromClient(servicePanel,socket).start();
} catch (IOException e) {
// e.printStackTrace();
break;
}
}
}
}
至于收包,发包是以流的形式传送的:
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
一般包里面的内容也就是被我们封装在某个类中,这个类要实现序列化,即实现Serializable接口;
发送方法那些就要自己写了~
public class ServerThread extends Thread{
ServicePanel servicePanel = null;
ServerSocket serverSocket = null;
Socket socket = null;
public ServerThread(ServicePanel servicePanel,ServerSocket serverSocket) {
this.servicePanel = servicePanel;
this.serverSocket = serverSocket;
}
public void run(){
while (true) {
try {
socket = serverSocket.accept();//等待连接
new ReciveFromClient(servicePanel,socket).start();
} catch (IOException e) {
// e.printStackTrace();
break;
}
}
}
}
至于收包,发包是以流的形式传送的:
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
一般包里面的内容也就是被我们封装在某个类中,这个类要实现序列化,即实现Serializable接口;
发送方法那些就要自己写了~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询