呵呵,就是说ipaddr就是默认你的ip地址啊,会不会别的机端口不行啊

解决方案 »

  1.   

    我这里也行。楼上的,因该不是端口的问题,如果是端口的问题不是抛出NullPointerException
      

  2.   

    服务器端
    package sockettest;import java.io.*;
    import java.net.*;public class EchoServer {
        public EchoServer() {
        }    public static void main(String[] args) {
            try {
                ServerSocket s = new ServerSocket(80);
                Socket incoming = s.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);
                out.println("ok,type 'BYE' to exit");            boolean done = false;
                while (!done) {
                    String line = in.readLine();
                    if (line == null) {
                        done = true;
                    } else {
                        out.println("echo:" + line);
                        if (line.trim().equals("BYE")) {
                            done = true;
                        }
                    }
                }
                incoming.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }}
      

  3.   

    客户端
    package sockettest;import java.io.*;
    import java.net.*;public class EchoClient {
        public EchoClient() {
        }    public static void main(String[] args){
            try{
                Socket s = new Socket("172.168.0.14",8189);
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter out = new PrintWriter(s.getOutputStream(),true);
                boolean more = true;
                while(more){
                    String line = in.readLine();
                    if(line == null){
                        more = false;
                    }else{
                        System.out.println(line);
                        out.println("BYE");
                    }
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
      

  4.   

    开了服务器,然后用telnet就可以直接测试了。输入"BYE"就退出了。
      

  5.   

    为什么不直接指定为Localhost呀?
      

  6.   

    String ipaddr=null;
    Socket socket=new Socket(ipaddr,6633);那台机器的%windir%/system32/drivers/etc下的Host文件有没有给localhost制定IP
    new Socket(null,port)参数为NULL不一定就是Localhost,参数为null是指采用环路IP
    也就是从127.0.0.1开始搜索可用IP