第五章 网络编程

package com.demo_;

import java.net.InetAddress;
public class Dept {
 public static void main(String[] args) {
  try {
   System.out.println("---淘宝的主服务器地址为---");
   InetAddress add = InetAddress.getByName("www.taobao.com");
   System.out.println(add);
   System.out.println("---淘宝的所有服务器地址---");
   InetAddress [] address = InetAddress.getAllByName("www.taobao.com");
   for (int i = 0; i < address.length; i++) {
    System.out.println(address[i+1]);
   }
  } catch (Exception e) {
   
  }
 }
}

public class Server { 
   
     public static void main(String[] args) { 
         try { 
             ServerSocket serverSocket = new ServerSocket(8800); 
             Socket socket = serverSocket.accept(); 
             InputStream is = socket.getInputStream(); // 读取流 
             BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
             String info = ""; 
             StringBuffer sb = new StringBuffer(); 
             while ((info = br.readLine()) != null) { 
                 System.out.println("我是服务器,客户端登录信息为:" + info); 
                 sb.append(info); 
             } 
             socket.shutdownInput(); // 先关闭输入流 
             OutputStream os = null; 
             String sum = sb.toString(); 
             if (sum.equals("用户1") || sum.equals("用户2") || sum.equals("用户3")) { // 判断用户名 
                 info = "欢迎你,登录成功!"; 
                 os = socket.getOutputStream(); 
                 os.write(info.getBytes()); 
                 System.out.println("存在该用户,用户登录成功"); 
             } else { 
                 info = "对不起,没有该用户,登录失败"; 
                 os = socket.getOutputStream(); 
                 os.write(info.getBytes()); 
                 System.out.println("对不起,没有该用户,已通知客户端登录失败"); 
             } 
             os.close(); // 关流 
             br.close(); 
             is.close(); 
             socket.close(); 
             serverSocket.close(); 
         } catch (IOException e) { 
             e.printStackTrace(); 
         } 
  
     } 
  
 } 
  
 /*
  * 客户端
  */ 
 public class Analog { 
  
     public static void main(String[] args) { 
          try {   
                 Socket socket=new Socket("localhost",8800);   
                 Scanner input=new Scanner(System.in);   
                 System.out.print("请输入用户名:");   
                 String info=input.next();   
                 OutputStream os=socket.getOutputStream();   
                 byte [] infos=info.getBytes();   
                 os.write(infos);   
                 //接收   
                 socket.shutdownOutput();//先关闭输出流   
        
                 InputStream is=socket.getInputStream();   
                 BufferedReader br=new BufferedReader(new InputStreamReader(is));   
                 String reply;   
                 while ((reply=br.readLine())!=null){   
                     System.out.println("我是客户端,服务器的响应为:"+reply);   
                 }   
                 br.close();   
                 is.close();   
                 os.close();   
                 socket.close();   
             } catch (IOException e) {   
                 e.printStackTrace();   
             }   
     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值