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();
}
}
}想让该客户端不停的发送数据,如何才能改写成多线程的?并且能控制发送的线程数 谢谢
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();
}
}
}想让该客户端不停的发送数据,如何才能改写成多线程的?并且能控制发送的线程数 谢谢
只有当你在发送数据后,又要处理一个长事务,影响了下一轮的数据发送,那才需要多线程。