最近刚学到网络,有些原理想问问,这是我写的一个程序
服务端:
package net.con2;
import java.io.*;
import java.net.*;
import java.util.*;
public class net2{
public static void main(String[] args) throws IOException{
System.out.println("server starting....");
ServerSocket server =new ServerSocket(8000);//创建一个服务端的监听
while(true){
Socket sk=server.accept(); //创建接收,当client发送时,不为null,否则为null
System.out.println("accepting connect ....");
new ServerThread(sk).start();
}
}
}
class ServerThread extends Thread{
private Socket sk;
ServerThread(Socket sk){this.sk=sk;}
public void run(){
BufferedReader in=null;
PrintWriter out =null;
try{
InputStreamReader isr=new InputStreamReader(sk.getInputStream());
in=new BufferedReader(isr); //创建输入
out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())),true);
//创建输出
while(true){
String cmd =in.readLine(); // 接受客户端的请求
System.out.println(cmd);
if(cmd==null) break;
cmd=cmd.toUpperCase();
if(cmd.startsWith("BYE")){
out.println("Bye"); //如果客户端的请求是BYE,则跳出while
break;
}else{
//否则...
out.println("你好,我是服务器");
}
}
}catch(IOException e){
System.out.println(e);
}finally{
System.out.println("closing connection....");
try{
if(in!=null) in.close();
if(out!=null) out.close();
if(sk!=null) sk.close();
}catch(IOException e){
System.out.println("close err "+e);
}
}
}
}
客户端:
package net.con2;
import java.io.*;
import java.net.*;
class client extends Thread{
public static int count =0;
public client(InetAddress addr){
count++;
BufferedReader in =null;
Socket sk=null;
PrintWriter out1=null ;
try{
sk= new Socket(addr,8000);//创建连接
in=new BufferedReader (new InputStreamReader(sk.getInputStream()));
//输入缓存
out1=new PrintWriter(new BufferedWriter (new OutputStreamWriter(sk.getOutputStream())),true);
//输出缓存
System.out.println("count: "+count);
out1.println("hello:"); //向服务端发送
System.out.println(in.readLine()); //由服务端接收
out1.println("BYE");
System.out.println(in.readLine());
}catch(IOException e){
System.out.println(e);
}finally{
out1.println("end");
try{
if(in!=null) in.close();
if(out1!=null) out1.close();
if(sk!=null) sk.close();
}catch(IOException e){
System.out.println(e);
}
}
}
}
public class send{
public static void main(String[] args) throws IOException,InterruptedException {
InetAddress addr =InetAddress.getLocalHost();
for(int i=0;i<10;i++){
new client(addr); //用for循环创建10个client
Thread.currentThread().sleep(1000);//休眠一秒
}
}
}
我想问的是为甚么服务端的红色部分写成这样下面两种时,是多线程客户端发送数剧,服务端只出现一次accepting connect ....,然后客户端和服务端都没反应。
while(true){
Socket sk=server.accept(); //创建接收,当client发送时,不为null,否则为null
System.out.println("accepting connect ....");
//new ServerThread(sk).start();
Socket sk=server.accept(); //创建接收,当client发送时,不为null,否则为null
while(true){
System.out.println("accepting connect ....");
new ServerThread(sk).start();
还有,在正常的写法中:
public class net2{
public static void main(String[] args) throws IOException{
System.out.println("server starting....");
ServerSocket server =new ServerSocket(8000);//创建一个服务端的监听
while(true){
Socket sk=server.accept(); //创建接收,当client发送时,不为null,否则为null
System.out.println("accepting connect ....");
new ServerThread(sk).start();
}
}
}
客户端即使甚么也不干,难道服务端不会执行到while循环而死循环吗?
[此贴子已经被作者于2007-2-22 18:30:37编辑过]