import java.io.*;
import java.net.*;
public class Client 
     {
    public static void main(String args[]) throws Exception {
    
        int HTTP_PORT = 8189;
        String HTTP_IP = "127.0.0.1";
        try{
        Socket socket = new Socket(HTTP_IP, HTTP_PORT);
        BufferedWriter out
                = new BufferedWriter(new OutputStreamWriter(socket.
                getOutputStream()));
        BufferedReader in
                = new BufferedReader(new InputStreamReader(socket.
                getInputStream()));
        
        out.write("POST / HTTP/1.1");
        out.newLine();
        out.write("User-Agent: MMS 2Simulator");
        out.newLine();
        out.write("Host:"+HTTP_IP+" "+HTTP_PORT);
        out.newLine();
        out.write("Connection:Keep-alive");
        out.newLine();
        out.write("Content-Length: 24957");
        out.newLine();
        out.write("Cache-Control: no-cache");
        out.newLine();
        out.write(" ");
        out.newLine();
       
        //读文件
        File read = new File("e://socket//aa.txt");
        BufferedReader br = new BufferedReader(new FileReader(read));
       
        String temp = null;
        temp = new String();
       while(true)
        {
          temp = br.readLine();
          if (temp == null) break; 
          out.write(temp);
          out.newLine();
        } 
     
        br.close();
        out.flush();
        
        String line;
        StringBuffer sb = new StringBuffer();
        while ((line = in.readLine()) != null) {
            sb.append(line+"\r\n");
     
        }
        out.close();
        in.close();
        socket.close();
        
     //   System.out.println(sb.toString());
        }catch(IOException e)
        {
         System.out.println(e);
    // e.printStackTrace();
        }
        
    }
}想让该客户端不停的发送数据,如何才能改写成多线程的?并且能控制发送的线程数 谢谢

解决方案 »

  1.   

    这个问题用多线程我看不出什么优势,如果只想让客户端不停的发数据,简单的写个WHILE循环不就OK了?写成多线程,你还要做共享互拆的工作,实际在同一个时刻,也只有一个线程能发送数据,其它线程都得等着,貌似多线程,但是没有带来额外的好处。
    只有当你在发送数据后,又要处理一个长事务,影响了下一轮的数据发送,那才需要多线程。
      

  2.   

    arbiter(同济流氓) 说的有道理啊。 我刚才也在想,搞个多线程来发数据好像没什么意义,非常感谢
      

  3.   

    以前写过一个基于移动梦网的多线程的,等我回家找找再贴给你吧!大致原理加了一个继承Thread的方法,然后再加一个线程sleep()的方法控制发送的频率.