您的位置:程序门 -> java -> j2se / 基础类



急~~~~~f关于java网络编程中多客户端对服务端的无法访问?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


急~~~~~f关于java网络编程中多客户端对服务端的无法访问?[已结贴,结贴人:dasoulong]
发表于:2007-02-09 18:36:32 楼主
//server.java    
//////////////////////////////////////    
package     javanet;    
import     java.net.*;    
import     java.io.*;    
 
public     class     server     {    
//                 create     a     server    
              private     serversocket     server;    
              private     int     clientcount=0;    
              /**     creates     a     new     instance     of     server     */    
              public     server()     {    
                              try     {    
                                              while(true){    
                                              server=new     serversocket(8086);    
                                              system.out.println( "正在等待客户的连接: ");    
                                              socket     connecttoclient=server.accept();    
                                              clientcount++;    
                                              system.out.println( "客户 "+clientcount+ "已经连接上,服务器应答就绪: ");    
                                              handleaclient     thread=new     handleaclient(connecttoclient);    
                                              thread.start();        
                                              }    
                              }    
                              catch(bindexception     e){    
                                              system.out.println( "端口被占用 ");    
                              }    
                              catch     (ioexception     ex)     {    
                                              ex.printstacktrace();    
                              }    
                                 
              }    
                 
              /**    
                  *     @param     args     the     command     line     arguments    
                  */    
              public     static     void     main(string[]     args)     {    
                              //     todo     code     application     logic     here    
                              server     server=new     server();    
              }    
                 
}    
///////////////////////////////////////////////////    
//client.java    
package     javanet;    
import     java.net.*;    
import     java.io.*;    
 
public     class     client     {    
              private     socket     contenttoserver;    
              /**     creates     a     new     instance     of     client     */    
              public     client()     {    
                              try     {    
                                                  system.out.println( "请求服务器: ");    
                                                  contenttoserver=new     socket( "localhost ",8086);    
                                                  system.out.println( "请求成功,正成发送数据: ");    
                                                  datainputstream     disformserver=new     datainputstream(contenttoserver.getinputstream());    
                                                  dataoutputstream     dostoserver=new     dataoutputstream(contenttoserver.getoutputstream());    
                                                  bufferedreader     br=new     bufferedreader(new     inputstreamreader(system.in));    
                                                  double     r=0;    
                                                  while(r> =0){    
                                                  r=double.parsedouble(br.readline());    
                                                  dostoserver.writedouble(r);    
                                                  double     area=disformserver.readdouble();    
                                                  system.out.println( "from     server     is     "+area);    
                                                                     
                                                  }    
                              }     catch     (unknownhostexception     ex)     {    
                                              ex.printstacktrace();    
                              }        
                              catch(connectexception     e){    
                                              system.out.println( "连接超时,找不到服务器。 ");    
                                 
                              }    
                              catch     (ioexception     ex)     {    
                                              ex.printstacktrace();    
                              }    
                                 
                 
              }    
              public     static     void     main(string[]     args){    
                              client     client=new     client();    
              }    
                 
}    
/////////////////////////////////////////////////////////    
//handleaclient.java    
 
package     javanet;    
  import     java.io.*;    
  import     java.net.*;    
class     handleaclient     extends     thread{    
              private     socket     connecttoclient;    
              public     handleaclient(socket     socket){    
                              connecttoclient=socket;    
}    
              public     void     run(){    
                              try{    
                              datainputstream     disformclient=new     datainputstream(connecttoclient.getinputstream());    
                              dataoutputstream     dostoclient=new     dataoutputstream(connecttoclient.getoutputstream());    
                              inetaddress     clientinetaddress=connecttoclient.getinetaddress();    
                              double     r=0;    
                             
                              while(r> =0){    
                                              system.out.println( "连接的客户机是: "+clientinetaddress.gethostname());    
                                              r=disformclient.readdouble();                                                
                                              double     area=math.pi*r*r;                                                    
                                              dostoclient.writedouble(area);    
                                              thread.sleep(100);    
              }    
                              }    
                              catch(ioexception     e){    
                                              system.err.println(e);    
                              }    
                              catch(interruptedexception     e){    
                                              system.err.println(e);    
                              }    
              }    
}    
运行之后,服务端总是端口被占,但能运行第一个客户,以后的就没什么反应了。    
高手们给个答案。急~~~~~~    
发表于:2007-02-10 00:09:101楼 得分:12
server=new     serversocket(8086);     写在while(true)里面   不报错才怪!
而且while(true)还是写在构造函数里,算你狠~


快速检索

最新资讯
热门点击