java socket 长连接,服务器端代码该怎样写?
最近做项目,碰到了些问题,问题如下:客户端发送到服务器端信息后,服务器端和客户端不断开连接,以后该客户端不向服务器端发送信息即可不定时的接受到服务器端发来的信息
请教各位大侠们,我该如何实现该功能,代码该如何写????能否附上代码,谢谢帮忙!!!

解决方案 »

  1.   

    //建立连接
     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() ,不知是你需要的么 供参考
      

  2.   

    楼上的不对,应该是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) {

    }
    }
    }
    }