我写了一个简单的socket通信程序,但在局域网中总是只能接收到null,请各位帮忙,在此谢过。 
/** 
  *   @(#)Socket_server.java 
  * 
  * 
  *   @author   Administrator 
  *   @version   1.00   2007/12/3 
  */ 
import   java.net.ServerSocket; 
import   java.net.Socket; 
import   java.net.*; 
import   java.io.*; 
public   class   Socket_server   { 
                
        /** 
          *   Creates   a   new   instance   of   <code> Socket_server </code> . 
          */ 
        public   Socket_server()   { 
        } 
        
        /** 
          *   @param   args   the   command   line   arguments 
          */ 
        public   static   void   main(String[]   args)   { 
                //   TODO   code   application   logic   here 
              try{ 
              ServerSocket   server_socker=new   ServerSocket(4500); 
                Socket   clientConn=server_socker.accept(); 
                BufferedReader   input=new   BufferedReader(new   InputStreamReader(clientConn.getInputStream())); 
              //   PrintStream   output=new   PrintStream(clientConn.getOutputStream(),true);   
            String   st=   input.readLine(); 
            System.out.println   (st);           
              server_socker.close(); 
              clientConn.close(); 
              input.close(); 
        } 
        catch(IOException   e){ 
        System.out.println   (e); 
        } 

} /////////////////////////////////////////////////////////////////
/** 
  *   @(#)SocketClint.java 
  * 
  * 
  *   @author   Administrator 
  *   @version   1.00   2007/12/3 
  */ 
import   java.net.Socket; 
import   java.net.ServerSocket; 
import   java.net.*; 
import   java.io.*; 
public   class   SocketClint   { 
                
        /** 
          *   Creates   a   new   instance   of   <code> SocketClint </code> . 
          */ 
        public   SocketClint()   { 
        } 
        
        /** 
          *   @param   args   the   command   line   arguments 
          */ 
        public   static   void   main(String[]   args)   { 
                //   TODO   code   application   logic   here 
                try{ 
                Socket   sock=new   Socket(InetAddress.getLocalHost(),4500); 
                OutputStreamWriter   s=new   OutputStreamWriter(sock.getOutputStream()); 
                //byte   a[]=new   byte[250]; 
                String   st="asdfasdaf"; 
                s.write(st);                     
                s.close();               
                sock.close(); 
                } 
                catch(Exception   e){System.out.println   (e);} 
        } 

这是客户端代码
在单机上测试的确没问题,但再网络中测试就只能接受到null,但访问计算机的IP是可以获取到的。 
请各位帮忙解决这个问题,谢谢。

解决方案 »

  1.   

    你的客户端永远在向自己的机器发包,看看你的那段话
    Socket       sock=new       Socket(InetAddress.getLocalHost(),4500);   
    LocalHost是指本地IP,你应该在别的机器上测试时将这边的IP设置成服务器端所在机器的IP
      

  2.   

    InetAddress.getByName("172.16.22.250")
      

  3.   

    InetAddress.getLocalHost()
    呵呵。
    socket server应该在一个循环中。
     while (true)
                {
           
                }
      

  4.   

    不同意1楼的观点。
    谁说本机不能接收本机发送的包?调试程序的时候都是这样的,难道你写C/S架构的东西,得需要2台电脑么?3楼说的有可能,放到一个循环中,那么你的服务端会一直保持监听这个端口。建议你运行的时,先启动服务端。另一种可能是,你发送的方法中,需要强制刷新输出流缓冲区:
    String st="asdfasdaf";   
    s.write(st);   
    尝试flush()一下。                                
      

  5.   

    抱歉!抱歉!!向1楼道歉,没看到LZ这句话:
    在单机上测试的确没问题,但再网络中测试就只能接受到null,但访问计算机的IP是可以获取到的。   
    请各位帮忙解决这个问题,谢谢。
    ”1楼说的没错。