黑马程序员技术交流社区

标题: 记录:Tcp传输 [打印本页]

作者: 不怕黑人    时间: 2015-7-13 16:52
标题: 记录:Tcp传输
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();
        }
}


作者: 拾起落叶    时间: 2015-7-13 17:03
tcp里用到了几个以前学的课程,其实这块要是能熟练掌握,证明以前学的课程也差不多




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2