java.util.Timer不成。
应该自己做线程,然后监视等待时间。

解决方案 »

  1.   

    用一个队列来保存发送的消息,每条消息记录下发送时的时间。
    线程一负责往socket发消息,同时把发出去的消息记录下时间戳放到队列中;
    线程二负责检查队列,发现有超时的消息还在队列中就重发。
      

  2.   

    import java.io.*;
    import java.net.*;
    import javax.swing.*;public class NSMTP extends JInternalFrame{
      public Socket socker;
      public BufferedReader inner;
      public PrintStream outer;
      public String smtpServer;
      public int mailPort = 25 ;
      public Mail mail;
      public NMailScript nmscript;
      private JTextArea jSessionEditor=new JTextArea();
      private JScrollPane jEditorScroll=new JScrollPane(jSessionEditor, 
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );  public NSMTP(String smtpServer, int mailPort, Mail mail, NMailScript nmscript){
        super("", false, true, false, true);
        setSize(400,200);
        this.smtpServer=smtpServer;
        this.mailPort=mailPort;
        this.mail=mail;
        this.nmscript=nmscript;
        setTitle(nmscript.smtpSessionTitle);
        this.getContentPane().add(jEditorScroll);    Session session=new Session();
        Thread controller= new Thread(session);
        controller.setPriority(Thread.MAX_PRIORITY);
        controller.start();
        controller = null;
      }  public class Session implements Runnable{
        public String response;    public Session(){
          try {
            socker=new Socket(smtpServer, mailPort, InetAddress.getLocalHost(), mailPort);
            inner=new BufferedReader(new InputStreamReader(socker.getInputStream()));
            outer=new PrintStream(socker.getOutputStream());
            response=(String)nmscript.message.get("M002");
          }catch (IOException ioe){ response=(String)nmscript.message.get("M001"); }
          jSessionEditor.append(response+"\n");
        }    public void sendMessage(String toSend){
          try {
            outer.println(toSend);
            outer.flush();
            response =inner.readLine();
          }catch(IOException e){}
        }    public int getFirstParameter(String msg){
          return Integer.parseInt(msg.substring(0, 3));
        }    public void run() {
          try { response=inner.readLine();}
            catch(IOException ioe){ response=(String)nmscript.message.get("M001");}
            catch(NullPointerException e){ response=(String)nmscript.message.get("M001");}
          jSessionEditor.append(response+"\n");      jSessionEditor.append("HELO " + smtpServer+"\n");
          sendMessage("HELO " + smtpServer);
          jSessionEditor.append(response+"\n");      sendMessage("AUTH LOGIN");
          jSessionEditor.append("进行身份验证……"+"\n");
          sendMessage("asdasfasfasf");
          sendMessage("asfasfasf");
          jSessionEditor.append(response+"\n");      jSessionEditor.append("MAIL FROM: " + mail.mailFrom+"\n");
          sendMessage("MAIL FROM: " + mail.mailFrom);
          jSessionEditor.append(response+"\n");
          jSessionEditor.append("RCPT TO: " + mail.mailTo +"\n");
          sendMessage("RCPT TO: " + mail.mailTo);
          jSessionEditor.append(response+"\n");      jSessionEditor.append("DATA\n");
          sendMessage("DATA");
          jSessionEditor.append(response+"\n");
          jSessionEditor.append("发送信件内容……"+"\n");
          sendMessage("Subject: " + mail.mailSubject + "\n" + mail.mailContent +
                      "\n" +"\n"+"\n"+
                      "\n"+"\n"+"\n"+"\n"+
                      "鹦鹉螺网络技术有限公司 www.nautilus.com.cn  邮件管理软件(共享版)\n"+".");      jSessionEditor.append(response+"\n");
          sendMessage("QUIT");
          jSessionEditor.append(response+"\n");
          jSessionEditor.append("信件发送完成!断开与服务器的连接……"+"\n");
          try { socker.close(); }
            catch (IOException ioe){}
        }
      }
    }
    一个例子,比较乱,希望对你有帮助
    等待——线程有个sleep(long m)方法,m是等待的毫秒数或微秒数,我记不清了
      

  3.   

    我发送只需发一条消息,然后发消息的程序不退出,等待10秒,
    10秒内收到返回过来的消息就退出,10秒内没收到,就重发该消息.如果我要发新的消息,不需要用现在已经发送消息的CLENT程序.(这是SOCKET的常识了)最好能给个简单的例子,上面的例子和我说的不是同一个问题啊.
      

  4.   

    老兄啊,怎么可能有简单的例子啊!你的问题不简单啊!
    基本实现:
       Hashtable  messagePool;   
       // messageid------message,消息池,用一个消息 ID来对应消息
       // message 应该包含 客户信息,消息信息,端口信息!
       sendMessage(Ingeger messageid)  
       //发送消息的函数,这就不用说了!
       Hashtable  sendMessagePool;
       //  messageid-----sendTime 已发送消息池, 消息 ID对应发送时间   线程1. 
          扫描 messagePool ,发现其不为空,调用 sendMessage 方法,
       并将 messageId ,以及发送时间 添加到 sendMessagePool 中!   线程2.
          读客户端的回送消息,从 sendMessagePool 中删除对应的已收到反馈消息!   线程3. 
          读 sendMessagePool ,发现消息已发送了超过10秒中没有反应,
          调用 sendMessage,重新发送 sendMessagePool 中的消息,并更新 发送时间!   看看我的思路是否可行,如果可行,编写代码!
       麻烦的是同步的问题!
       到时再说吧!
       
      

  5.   

    有这么难吗?
    我以前用UNIX下的C实现过同样的功能,好像代码很少的啊
    只是现在忘记了有哪位高手能帮我解决,1000分都可以给!
    不过,谢谢上面RE的同志,谢谢大家!难道大家写SOCKET程序就不用判断对方是否收到消息吗?