A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 老衲玩IT 中级黑马   /  2013-3-7 15:47  /  1557 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
import java.net.*;
class TcpClient{
     public static void main(String []args)throws Exception{//声明异常
         Socket s =new Socket("127.0.0.1",10003);
         OutputStream out =s.getOutputStream();
         out.write("TCP 我来了!".getBytes());
         s.close();
     }
}
public class TcpServer{
    public static void main(String []args)throws Exception{//此处static写错,再声明异常就OK了
        ServerSocket ss =new ServerSocket(10003);
        Socket s=ss.accept();
        String ip =s.getInetAddress().getHostAddress();
        System.out.println("ip:"+ip+"connected");
        InputStream in =s.getInputStream();
       byte []buf =new byte[1024];
       System.out.println(new String(buf,0,in.read(buf)));
       s.close();
       ss.close();
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马