标签:else [] sys style int 客户端 测试 col color
1 public static void main(String[] args) throws IOException { 2 System.out.println("服务器开启监听"); 3 ServerSocket server=new ServerSocket(6666); 4 Socket client = server.accept(); 5 System.out.println("连接成功"); 6 while(true){//循环接收 7 byte[] data=new byte[1024]; 8 int count = client.getInputStream().read(data); 9 String msg= new String(data,0,count,"utf-8"); 10 if("exit".equals(msg)) 11 break; 12 else 13 System.out.println(msg); 14 } 15 client.close(); 16 server.close(); 17 }
1 public static void main(String[] args) throws UnknownHostException, IOException { 2 System.out.println("客户端开始访问"); 3 Socket client = new Socket("1.1.1.1", 6666);//参数为需要ServerSocket服务器的:IP 和 端口号,端口号是ServerSocket服务器设的端口号
4 Scanner scanner = new Scanner(System.in);
5 while(true){//循环发送
6 System.out.println("请输入:");
7 String x=scanner.next();
8 byte[] data=x.getBytes("utf-8");
9 client.getOutputStream().write(data);
10 if("end".equals(x))
11 break;
12 }
13 client.close();
14 }
建两个有main方法的类,把代码分别放入类中。可以在cmd中用dos命令启动两个类,也可以用tomcate启动。(注:先启动服务器端的main方法再启动用户端的代码)
标签:else [] sys style int 客户端 测试 col color
原文地址:http://www.cnblogs.com/janesyf/p/7846854.html