J
j2me
Unregistered / Unconfirmed
GUEST, unregistred user!
在public class Listener extends Thread {...} 中
1>生成ServerSocket侦听对象
ServerSocket listener = new ServerSocket(this.port,maxConn);
2> 侦听线程运行函数 (线程优先级为7)
public void run() {
while(!stop_flag) {
try {
client = listener.accept();
System.out.println(this.service);
addConnection(client,runService);
} catch (Exception e) {
e.printStackTrace();
Date today = new Date();
System.out.println(today);
}
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3>收到连接后唤醒工作线程队列
synchronized (pools) {
pools.add(pools.size(), socket);
pools.notify();
}
4>工作线程工作(线程优先级为5)
public void run() {
while (true) {
synchronized (pools) {
while (pools.isEmpty()) {
try {
pools.wait();
} catch (InterruptedException e) {
log.println(e.getMessage());
}
}
//获得第一个有效的Socket连接
socket = (Socket) pools.remove(0);
//加入服务连接
server.addConnections(socket);
//当前连接数增加
checkOut++;
}
synchronized (this) {
handleConnection(socket, s);
}
}
}
5>具体工作函数
public void handleConnection(Socket socket, Service s) {
try {
socket.setSoTimeout(20000);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
//调用相应的应用服务
//读取数据,进行处理,并输出数据.最后 in.close ;
out.close;
s.serve(in, out);
} catch (IOException e) {
e.printStackTrace();
Date today = new Date();
System.out.println(today + "
--- handleConnection"
}
//取消服务中的socket连接,并且关闭soclet
server.removeConnections(socket);
//当前连接数减少
checkOut--;
}
}
出现奇怪现象:
StringBuffer buf = new StringBuffer();
try {
//连接能够成功
socket = new Socket("服务器的IP", iport);
//in,out也能正常获得
in = socket.getInputStream();
out = new PrintWriter(socket.getOutputStream(), true);
long timeStart = System.currentTimeMillis();
//数据输出也正确
out.print(ls_message);
out.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//出现错误,客户端就停在这了
line = reader.readLine();
// 读取第一行
out.close();
in.close();
socket.close();
} catch (IOException e)
{
e.printStackTrace();
}
侦听线程:
public void run() {
while(!stop_flag) {
try {
System.out.println("
begin
###########################"
//客户端连接正常,但是线程就停在这不往下运行了
client = listener.accept();
//不打印提示信息
System.out.println(this.service);
addConnection(client,runService);
} catch (Exception e) {
e.printStackTrace();
Date today = new Date();
System.out.println(today);
}
}
请大家指点一下!!!
1>生成ServerSocket侦听对象
ServerSocket listener = new ServerSocket(this.port,maxConn);
2> 侦听线程运行函数 (线程优先级为7)
public void run() {
while(!stop_flag) {
try {
client = listener.accept();
System.out.println(this.service);
addConnection(client,runService);
} catch (Exception e) {
e.printStackTrace();
Date today = new Date();
System.out.println(today);
}
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3>收到连接后唤醒工作线程队列
synchronized (pools) {
pools.add(pools.size(), socket);
pools.notify();
}
4>工作线程工作(线程优先级为5)
public void run() {
while (true) {
synchronized (pools) {
while (pools.isEmpty()) {
try {
pools.wait();
} catch (InterruptedException e) {
log.println(e.getMessage());
}
}
//获得第一个有效的Socket连接
socket = (Socket) pools.remove(0);
//加入服务连接
server.addConnections(socket);
//当前连接数增加
checkOut++;
}
synchronized (this) {
handleConnection(socket, s);
}
}
}
5>具体工作函数
public void handleConnection(Socket socket, Service s) {
try {
socket.setSoTimeout(20000);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
//调用相应的应用服务
//读取数据,进行处理,并输出数据.最后 in.close ;
out.close;
s.serve(in, out);
} catch (IOException e) {
e.printStackTrace();
Date today = new Date();
System.out.println(today + "
--- handleConnection"
}
//取消服务中的socket连接,并且关闭soclet
server.removeConnections(socket);
//当前连接数减少
checkOut--;
}
}
出现奇怪现象:
StringBuffer buf = new StringBuffer();
try {
//连接能够成功
socket = new Socket("服务器的IP", iport);
//in,out也能正常获得
in = socket.getInputStream();
out = new PrintWriter(socket.getOutputStream(), true);
long timeStart = System.currentTimeMillis();
//数据输出也正确
out.print(ls_message);
out.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//出现错误,客户端就停在这了
line = reader.readLine();
// 读取第一行
out.close();
in.close();
socket.close();
} catch (IOException e)
{
e.printStackTrace();
}
侦听线程:
public void run() {
while(!stop_flag) {
try {
System.out.println("
begin
###########################"
//客户端连接正常,但是线程就停在这不往下运行了
client = listener.accept();
//不打印提示信息
System.out.println(this.service);
addConnection(client,runService);
} catch (Exception e) {
e.printStackTrace();
Date today = new Date();
System.out.println(today);
}
}
请大家指点一下!!!