我用Socket传文件,写了个程序,Server是个死循环,为了便于监控,想让Server当监听到一个连接后,让监听暂停.....其间传文件(启动另外一个线程),文件传完之后,再让监听恢复(从另外一台机器传文件),如此反复.....
用什么方法呀?请邦助!!
附上源码:
import java.io.*;
import java.net.*;
public class server extends Thread{
protected ServerSocket S;
public static face myface = new face("server");
public server(){
try{
S = new ServerSocket(8899);
}catch(IOException e){
myface.flyOutputStream("Creat ServerSocket Err...");
}
start();
}
public void run() {
try {
int count=0;
while(true){
myface.flyOutputStream("监听中...");
Socket C = S.accept();
count++;
myface.flyOutputStream("This is the " + count + "'st client connection!");
Connection c = new Connection(C);
}
}catch(IOException e){
myface.flyOutputStream(" Err...");
}
}
public static void main(String[] args) {
myface.display();
new server();
}
}
class Connection extends Thread {
..........
}
用什么方法呀?请邦助!!
附上源码:
import java.io.*;
import java.net.*;
public class server extends Thread{
protected ServerSocket S;
public static face myface = new face("server");
public server(){
try{
S = new ServerSocket(8899);
}catch(IOException e){
myface.flyOutputStream("Creat ServerSocket Err...");
}
start();
}
public void run() {
try {
int count=0;
while(true){
myface.flyOutputStream("监听中...");
Socket C = S.accept();
count++;
myface.flyOutputStream("This is the " + count + "'st client connection!");
Connection c = new Connection(C);
}
}catch(IOException e){
myface.flyOutputStream(" Err...");
}
}
public static void main(String[] args) {
myface.display();
new server();
}
}
class Connection extends Thread {
..........
}
while(true){
try {
Socket s=ss.accept();
new MyThread(s).start();//每监听到一个连接就开一个线程进行传文件,也不用让主线程暂停,因为accept是阻塞的
} catch (IOException e) {
e.printStackTrace();
}
}