博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中TCP传输协议
阅读量:6970 次
发布时间:2019-06-27

本文共 3529 字,大约阅读时间需要 11 分钟。

class TcpClient {    public static void main(String[] args) throws Exception {        //创建client的socket服务,指定目的主机和port        Socket s = new Socket("192.168.1.10",10003);        //为了发送数据。获取socket流中的输出流        java.io.OutputStream out = s.getOutputStream();        out.write("hello tcp".getBytes());        s.close();    }}class TcpServer {    public static void main(String[] args) throws Exception {        //建立服务端socket服务,并监听一个port        ServerSocket ss = new ServerSocket(10003);        //通过accept方法获取连接过来的client对象        Socket s = ss.accept();        String ip = s.getInetAddress().getHostAddress();        //获取client发送过来的数据。使用client对象的读取流来读取数据        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len = in.read(buf);        System.out.println(new String(buf,0,len));        //关闭client        s.close();    }}class TcpClient2 {    public static void main(String[] args) throws Exception {        //建立socket服务。指定连接的主机和port        Socket s = new Socket("192.168.1.10",10004);        //获取socket流中的输出流。将数据写入该流,通过网络传送给服务端        OutputStream out = (OutputStream) s.getOutputStream();        out.write("hello Tcp".getBytes());        //获取socket流中的输入流。将服务端反馈的数据获取到,并打印        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len = in.read(buf);        System.out.println(new String(buf,0,len));        s.close();    }}class TcpServer2 {    public static void main(String[] args) throws Exception {        ServerSocket ss = new ServerSocket(10004);        Socket s = ss.accept();        //服务端收信息        InputStream in = s.getInputStream();        String ip = s.getInetAddress().getHostAddress();        byte[] buf = new byte[1024];        int len = in.read(buf);        System.out.println(new String(buf,0,len));        //服务端发信息        OutputStream out = (OutputStream) s.getOutputStream();        out.write("have receive".getBytes());        s.close();        ss.close();    }}/*需求:建立一个文本转换服务器client给服务端发送文本,服务端会将文本转成大写再返还给client*/class TcpClient3 {    public static void main(String[] args) throws Exception {        Socket s = new Socket("192.168.1.4",10005);        //定义读取键盘数据的流对象,读取键盘录入文本        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));        //获取socket流中的输出流。通过网络传送到服务端        BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));        //定义一个socket读取流,读取服务端返回的大写信息        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));        String line = null;        while((line=bufr.readLine())!=null) {            if("over".equals(line))                break;            bufOut.write(line);            bufOut.newLine();//结束标记            bufOut.flush();            String str = bufIn.readLine();            System.out.println("server:"+str);        }        bufr.close();        s.close();        }}class TcpServer3 {    public static void main(String[] args) throws Exception {        ServerSocket ss = new ServerSocket(10005);        Socket s = ss.accept();        //读取socket读取流中的数据        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));        //socket输出流,将大写文本写入到socket输出流。并发送给client         BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));        String line = null;        while((line=bufIn.readLine())!=null) {            bufOut.write(line.toUpperCase());            bufOut.newLine();//结束标记            bufOut.flush();        }        s.close();        ss.close();    }}

转载地址:http://sjasl.baihongyu.com/

你可能感兴趣的文章
PGA
查看>>
用H5上传文件
查看>>
Atom替换换行符
查看>>
ASP.NET Core 开源GitServer 实现自己的GitHub
查看>>
今天周五啦
查看>>
Linux SendMail发送邮件失败诊断案例(四)
查看>>
C# ManualResetEventSlim 实现
查看>>
JMockit常用操作
查看>>
NO.2 安装配置
查看>>
SpringMVC+hibernate4事务处理
查看>>
大型网站架构演化历程
查看>>
oracle如何查询哪个表数据量大
查看>>
python os用法笔记
查看>>
IntelliJ IDEA强制更新Maven的包
查看>>
[osg][原]自定义osgGA漫游器
查看>>
python文本 单独处理每个字符的方法汇总
查看>>
Android内核三大核心功能之一AMS内部原理
查看>>
JSX 语法
查看>>
Day8 Servlet
查看>>
iOS 集成Protobuf,转换proto文件
查看>>