小妹不才,查了很多WEB推技术的资料,可是始终没搞明白如何持续往IE客户端输出数据,恳求高手指点。 我现在只能完成一次输出,具体过程如下:
1.客户端IE输入指定网址,假设访问端口8080;
2.服务器端通过监听8080端口,一旦发现有连接,即创建一个socket,并建立一个线程。
3.在线程中创建该socket得输出与输入对象,在socket.close前可以成功通过输出对象往IE输出数据,但无法持续输出;若使用线程.sleep,在客户端中展现也是先一直等待,直到sleep时间结束后一次输出所有记录,而不是分次持续输出。 附上我的测试代码,请各位达人一定教教我啊,为了这个简单的东西郁闷了好久。先谢过~
//类ChatServer 主服务,服务器端监控类import java.io.*;
import java.net.*;
import java.util.Date;
public class ChatServer{ public static void main(String[] args) throws Exception{
(new ChatServer()).monitor();
} public void monitor() throws Exception {
int port = 8080;
int i=1;
ServerSocket srvSocket = new ServerSocket(port);
System.out.println("Listenning at " + port + " ...");
System.out.println("Accepting connection...");
while (true) {
try {
Socket socket = srvSocket.accept();
long session_id = new Date().getTime();
System.out.println("new socket:"+i++);
Thread t = new ThreadedChatHandle(socket);
t.start();
} catch (IOException e) {
System.out.println("Exception occurs when accept(): " + e.getMessage());
}
}
}}//类ThreadedChatHandle 线程,处理类
import java.io.*;
import java.net.*;public class ThreadedChatHandle extends Thread{ private Socket socket; private BufferedReader in; private PrintStream os ; //private PrintWriter out; protected String ip;
InetAddress ia ;
int port = 8080; public ThreadedChatHandle(Socket psocket) {
this.socket = psocket;
ia = socket.getInetAddress();
//ip = ia.getHostAddress();
try {
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
//out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
// socket.getOutputStream())), true);
os = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
} catch (IOException e) {
System.out.println(e.getMessage());
}
setDaemon(true);
} public void run() {
int i=0;
// 读取客户头端信息
try {
String line = in.readLine();
while (line != null && !line.equals("")) {
System.out.println("line:"+line);
line = in.readLine();
}
String webMessage = "HTTP/1.0 200 ok\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 100\r\n" +
"\r\n" +
"<html><body>\r\n";
os.write(webMessage.getBytes());
} catch (Exception e) {
System.out.print("header:"+e.toString());
}
while (true) {
try {
i++;
//out.println("用out变量输出的"+i);
//out.flush();
//socket = new Socket(ia,port);
//os = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
String str = "aaa"+i +"\r\n";
System.out.println(str);
System.out.println(os.toString());
os.flush();
os.write(str.getBytes());
os.flush();
// 不close,上面的内容就不输出
os.close();
socket.close();
sleep(6000);
// close以后socket就断开,所以下面的就不会输出
socket = new Socket(ia,port);
os = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
os.write("bbb\r\n".getBytes());
os.flush();
os.close();
socket.close();
System.out.println("out end!");
} catch (Exception e) {
try {
System.out.print("1--:"+e.toString());
socket.close();
} catch (Exception ee) {
System.out.println("2--:"+ee.toString());
}
break;
}
}
try {
socket.close();
} catch (Exception e) {
}
}}
1.客户端IE输入指定网址,假设访问端口8080;
2.服务器端通过监听8080端口,一旦发现有连接,即创建一个socket,并建立一个线程。
3.在线程中创建该socket得输出与输入对象,在socket.close前可以成功通过输出对象往IE输出数据,但无法持续输出;若使用线程.sleep,在客户端中展现也是先一直等待,直到sleep时间结束后一次输出所有记录,而不是分次持续输出。 附上我的测试代码,请各位达人一定教教我啊,为了这个简单的东西郁闷了好久。先谢过~
//类ChatServer 主服务,服务器端监控类import java.io.*;
import java.net.*;
import java.util.Date;
public class ChatServer{ public static void main(String[] args) throws Exception{
(new ChatServer()).monitor();
} public void monitor() throws Exception {
int port = 8080;
int i=1;
ServerSocket srvSocket = new ServerSocket(port);
System.out.println("Listenning at " + port + " ...");
System.out.println("Accepting connection...");
while (true) {
try {
Socket socket = srvSocket.accept();
long session_id = new Date().getTime();
System.out.println("new socket:"+i++);
Thread t = new ThreadedChatHandle(socket);
t.start();
} catch (IOException e) {
System.out.println("Exception occurs when accept(): " + e.getMessage());
}
}
}}//类ThreadedChatHandle 线程,处理类
import java.io.*;
import java.net.*;public class ThreadedChatHandle extends Thread{ private Socket socket; private BufferedReader in; private PrintStream os ; //private PrintWriter out; protected String ip;
InetAddress ia ;
int port = 8080; public ThreadedChatHandle(Socket psocket) {
this.socket = psocket;
ia = socket.getInetAddress();
//ip = ia.getHostAddress();
try {
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
//out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
// socket.getOutputStream())), true);
os = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
} catch (IOException e) {
System.out.println(e.getMessage());
}
setDaemon(true);
} public void run() {
int i=0;
// 读取客户头端信息
try {
String line = in.readLine();
while (line != null && !line.equals("")) {
System.out.println("line:"+line);
line = in.readLine();
}
String webMessage = "HTTP/1.0 200 ok\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 100\r\n" +
"\r\n" +
"<html><body>\r\n";
os.write(webMessage.getBytes());
} catch (Exception e) {
System.out.print("header:"+e.toString());
}
while (true) {
try {
i++;
//out.println("用out变量输出的"+i);
//out.flush();
//socket = new Socket(ia,port);
//os = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
String str = "aaa"+i +"\r\n";
System.out.println(str);
System.out.println(os.toString());
os.flush();
os.write(str.getBytes());
os.flush();
// 不close,上面的内容就不输出
os.close();
socket.close();
sleep(6000);
// close以后socket就断开,所以下面的就不会输出
socket = new Socket(ia,port);
os = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
os.write("bbb\r\n".getBytes());
os.flush();
os.close();
socket.close();
System.out.println("out end!");
} catch (Exception e) {
try {
System.out.print("1--:"+e.toString());
socket.close();
} catch (Exception ee) {
System.out.println("2--:"+ee.toString());
}
break;
}
}
try {
socket.close();
} catch (Exception e) {
}
}}
阻塞 非阻塞
你的系统 是 什么???
不知有帮助吗?
用其他的 没有 这个问题。
while(true) {
Thread.sleeep(x);
out.println(something);
}
这样一直等待下去,客户端关闭时会产生异常跳出While循环。
不过这个方法会导致服务器线程数随着客户机的上升的
Thread.sleeep(x);
out.println(something);
}
这个试过,但是这样的话客户端一行都不会输出,就一直在sleep等待。
我的测试代码貌似socket是通过PrintStream或者PrintWriter对象close后才把数据往客户端输出,不close的话,socket不知道何时该推。
可是输出对象close以后socket也会close,所以就不能进行第二次输出。
如:
while(true) {
os.write(str.getBytes());
os.flush();
}
这样会循环输出,但是客户端没有停顿。但是如果把代码修改成
while(true) {
os.write(str.getBytes());
os.flush();
sleep(6000);
}
那客户端就一直处于下载状态,一次也不会输出,好像说是不可以用sleep,应该用wait方法的,
可是用了wait以后,输出一次后,就会报错,错误如下:
java.lang.IllegalMonitorStateException: current thread not owner
wait(5000);
}