聊天软件项目TCP升级版

 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 }

 

转载于:https://www.cnblogs.com/hellochennan/p/5373198.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值