码迷,mamicode.com
首页 > 其他好文 > 详细

聊天软件项目TCP升级版

时间:2016-04-10 01:17:34      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

 1 //聊天软件项目TCP升级版
 2 import java.io.*;
 3 import java.net.*;
 4 class TcpClient2 
 5 {
 6     public static void main(String[] args)throws Exception 
 7     {
 8         Socket s = new Socket("192.168.1.254",10004);
 9         OutputStream out = s.getOutputStream();
10         out.write("服务端,你好".getBytes());
11         InputStream in = s.getInputStream();
12         byte[] buf = new byte[1024];
13         int len = in.read(buf);
14         System.out.println(new String(buf,0,len));
15         s.close();
16     }
17 }
18 
19 
20 class TcpServer2
21 {
22     public static void main(String[] args) throws Exception
23     {
24         ServerSocket ss = new ServerSocket(10004);
25         Socket s = ss.accept();
26         String ip = s.getInetAddress().getHostAddress();
27         System.out.println(ip+"....connected");
28         InputStream in = s.getInputStream();
29         byte[] buf = new byte[1024];
30         int len = in.read(buf);
31         System.out.println(new String(buf,0,len));
32         OutputStream out = s.getOutputStream();
33         Thread.sleep(10000);
34         out.write("哥们收到,你也好".getBytes());
35         s.close();
36         ss.close();
37     }
38 }

 

聊天软件项目TCP升级版

标签:

原文地址:http://www.cnblogs.com/hellochennan/p/5373198.html

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