建立本地socket连接,客户端向服务器发送个字符串,服务器接收并返回int port=134;
        Socket s;
        try {
            s = new Socket("10.26.71.25", port);//建立本地socket连接
            OutputStream out=s.getOutputStream();
            DataOutputStream dout=new DataOutputStream(out);
            dout.writeUTF("tom");               //写入输入流
            InputStream in =s.getInputStream();
            DataInputStream din=new DataInputStream(in);
            String string=din.readUTF();        //从输入流中读数据
            System.out.println(string);
            in.close();
            out.close();
            s.close();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }无法建立连接...我在学校里,用锐捷上网,每台机器有个固定IP地址
s = new Socket("10.26.71.25", port);10.26.71.25是我的内网IP地址,这样写好像无法建立连接,参数应该写什么呢谢各位了...

解决方案 »

  1.   

    是本机连接可以直接使用s=new Socket("localhost",port);并且Port尽量设置大一些,不让回和计算机上的一些服务端口重复,就连接不上了!
      

  2.   

    端口大点试试比如PORT = 4000
      

  3.   

    楼上的正解。1024一下端口都是需要root权限才能启动的(linux下)。自定义socket程序一般使用1024以上的端口。
    另外,java写socket一般不需要这么复杂,建议使用rmi或者开源的rpc的实现。用java而不会利用java社区开源资源是非常可悲的。
    程序员专属T恤:http://t4coder.us
      

  4.   

    楼上的方法都试过了,可还是不行...
     s = new Socket("localhost",3456);
     s = new Socket(InetAddress.getLocalHost(), 5678);
    等等 试了很多端口号还是错误un:
    2010-5-9 18:30:21 socketInfo.Main main
    严重: null
    java.net.ConnectException: Connection refused: connect
            at java.net.PlainSocketImpl.socketConnect(Native Method)