码迷,mamicode.com
首页 > 编程语言 > 详细

java中tcp小例子

时间:2015-07-27 00:19:29      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

服务端:

ServerSocket service = new ServerSocket(7777);
		Socket socket = service.accept();
		InputStream input = socket.getInputStream();
		OutputStream out = socket.getOutputStream();
		BufferedInputStream bufinput = new BufferedInputStream(input);
		BufferedOutputStream bufout = new BufferedOutputStream(out);
		byte [] buf = new byte[1024];
		int len = 0;
		
		StringBuffer sb = new StringBuffer();
        String message = new String(buf,0,bufinput.read(buf));
        System.out.println(message);
        
        out.write("服务端:\n小客你好啊!".getBytes());

客户端:

Socket client = new Socket("127.0.0.1",7777);
        OutputStream out = client.getOutputStream();
        InputStream input = client.getInputStream();
        BufferedOutputStream bufout = new BufferedOutputStream(out);
        BufferedInputStream bufinput = new BufferedInputStream(input);
        bufout.write("客户端:\n你好".getBytes());
        bufout.flush();
        byte [] buf = new byte [1024];
        String message = new String(buf,0,bufinput.read(buf));
        System.out.println(message);


版权声明:本文为博主原创文章,未经博主允许不得转载。

java中tcp小例子

标签:

原文地址:http://blog.csdn.net/cs_12/article/details/47074939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!