java socket 长连接问题

我是幸福的小虫 2009-02-24 08:36:22
java socket 长连接,服务器端代码该怎样写?
最近做项目,碰到了些问题,问题如下:客户端发送到服务器端信息后,服务器端和客户端不断开连接,以后该客户端不向服务器端发送信息即可不定时的接受到服务器端发来的信息
请教各位大侠们,我该如何实现该功能,代码该如何写????能否附上代码,谢谢帮忙!!!
...全文
299 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fei80230901 2009-02-25
  • 打赏
  • 举报
回复
//建立连接
private URLConnection getServletConnection()
throws MalformedURLException, IOException {

// Connection Servlet

URL urlServlet = new URL(getCodeBase(), "http://127.0.0.1:8080/springapp/svg");
URLConnection con = urlServlet.openConnection();

// config

con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");



return con;
}

/**
* 发送接收数据.
*/
private HashMap <String,Realdata> onSendData() {

HashMap <String,Realdata> result=null;
try {
// get input data for sending

Map<String, String> input =new HashMap<String, String>();
input.put("station", getStation());
input.put("graphic", getGraphic());

// send data to the servlet
System.out.println("input"+input);
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();

// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
result= (HashMap <String,Realdata>) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
} catch (Exception ex) {
ex.printStackTrace();
}

return result;
}
我是使用TimerTask循环调用onSendData() ,不知是你需要的么 供参考
rumlee 2009-02-25
  • 打赏
  • 举报
回复
使用一个线程,始终循环获取对方发过来的信息。
yy00915132 2009-02-25
  • 打赏
  • 举报
回复
为什么不顺便描述下你的代码有什么问题呢!
  • 打赏
  • 举报
回复
楼上的不对,应该是socket连接的,客户端是手机(Java编写的),传输数据时用datainputstream和dataoutputstream,我的代码
好象有问题,麻烦各位大侠看看
package socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import javax.swing.JOptionPane;
import ui.Desktop;

public class IOServers implements Runnable {
boolean started = false;
ServerSocket socket = null;
public static Vector<ClientRun> userList = new Vector<ClientRun>();
public static int count = 0;
int port = 7392;

public void run() {
try {
socket = new ServerSocket(port);
started = true;
}
catch (BindException e) {
JOptionPane.showMessageDialog(Desktop.frame, "端口被其他程序占用中,程序即将退出!", "服务",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch (IOException e) {
JOptionPane.showMessageDialog(Desktop.frame, "创建ServerSocket失败,程序即将退出!", "服务",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
try {
while (started) {
Socket s = socket.accept();//监听端口
ClientRun cr = new ClientRun(s);//主线程只负责接收信息,每个客户端连接进来都会开始一个新线程
new Thread(cr).start();//把连接进来的Socket传到线程中
userList.add(cr);
}
}
catch (IOException e) {

}
}

class ClientRun implements Runnable {
@SuppressWarnings("unused")
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
boolean bConnect = false;

public ClientRun(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnect = true;
}
catch (IOException e) {

}
}

public void send(String str) {
try {
dos.writeUTF(str);
}
catch (IOException e) {

}
}

public String read() {
try {
return dis.readUTF();
}
catch (IOException e) {
return "";
}
}

public void run() {
try {
while (bConnect) {
String str = read();
send(str);
}
}
catch (Exception e) {

}
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧