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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不怕黑人 中级黑马   /  2015-7-13 16:52  /  433 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.net.*;
import java.io.*;
class ClientDemo1
{
        public static void main(String[] args) throws Exception
        {
                Socket s=new Socket("192.168.1.100",10003);
                OutputStream out=s.getOutputStream();
                out.write("客户端发送数据".getBytes());
                InputStream is=s.getInputStream();
                byte[] buf=new byte[1024];
                int len=is.read(buf);
                String data=new String(buf,0,len);
                System.out.println(data);
                s.close();
        }
}
class ServerDemo1
{
        public static void main(String[] args) throws Exception
        {
                ServerSocket ss=new ServerSocket(10003);
                Socket s=ss.accept();
                String ip=s.getInetAddress().getHostAddress();
                InputStream is=s.getInputStream();
                byte[] buf=new byte[1024];
                int len=is.read(buf);
                String data=new String(buf,0,len);
                System.out.println(ip+"|||"+data);
                OutputStream out=s.getOutputStream();
                out.write("服务器发送数据".getBytes());
                s.close();
                ss.close();
        }
}

1 个回复

倒序浏览
tcp里用到了几个以前学的课程,其实这块要是能熟练掌握,证明以前学的课程也差不多
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马