Java 网络编程实例

import java.net.*;
public class Chap22_1 {
 public static void main(String args[])
 {
//1.获取本机电脑IP和主机信息
//  try
//  {
//  InetAddress addr=InetAddress.getLocalHost();
//  System.out.println(addr.getHostAddress());
//  System.out.println(addr.getHostName());
//  System.out.println(addr);
//  }
//  catch(UnknownHostException e)
//  {
//   System.out.println("无法取得IP地址"); 
//  }
//  
// }


//2.获取网络主机和IP地址
// public static void main(String args[])
// { 
//  try
//  {
//  InetAddress addr;
//  addr=InetAddress.getByName("www.baidu.com");
//  System.out.println(addr);
//  }
//  catch(UnknownHostException e)
//  {
//   System.out.println("无法取得IP地址");
//  }


 
//3.使用URL类
  try
  {
 URL u=new URL("http://www.baidu.com");
 
 System.out.println("通信协议名称为"+u.getProtocol());
 
 System.out.println("host name "+u.getHost());
 
 System.out.println("端口为 "+u.getPort());
  }
  catch(MalformedURLException e)
  {
   System.out.println("发生了" +e+ "异常");
  }
 
//4.读取URL中的文件内容
//  String str;
//
//  try
//  {
//     URL u=new URL("file:/c:\\java\\poem.txt");
//
//     Object obj=u.getContent();    // 取得URL的内容
//     InputStreamReader isr=new InputStreamReader((InputStream) obj);
//     BufferedReader br=new BufferedReader(isr);
//
//     while((str=br.readLine())!=null)
//        System.out.println(str);
//     br.close();
//  }
//  catch(IOException e)
//  {
//     System.out.println("发生了"+e+"异常");
//  }
//
 
//5.URLConnection类
//  try
//  {
//     URL u1=new URL("http://www.drmaster.com.tw");
//     URL u2=new URL("file:/c:\\java\\poem.txt");
//     URL u3=new URL("file:/c:\\java\\pic0.jpg");
//
//     URLConnection uc1=u1.openConnection();
//     URLConnection uc2=u2.openConnection();
//     URLConnection uc3=u3.openConnection();
//
//     System.out.print("主网页的大小为 " + uc1.getContentLength());
//     System.out.println(",  类型为 " + uc1.getContentType());
//     System.out.print("poem.txt的大小为 " + uc2.getContentLength());
//     System.out.println(",  类型为 " + uc2.getContentType());
//     System.out.print("pic0.jpg的大小为  " + uc3.getContentLength());
//     System.out.println(",  类型为 " + uc3.getContentType());
//  }
//  catch(IOException e)
//  {
//     System.out.println("发生了"+e+"异常");
//  }
 
//6.Server端的服务程序
 
//  try
//  {
//     ServerSocket svs=new ServerSocket(2525);
//
//     System.out.println("等候客户端的请求中...");
//     Socket s=svs.accept();   //等候客户端的请求
//     System.out.println("客户端已和本机连上线...");
//
//     OutputStream out=s.getOutputStream();   // 取得输出数据流
//     String str="Honor shows the man.";
//     System.out.println("数据正在传送中...");
//     out.write(str.getBytes()); // 将字符串转成Byte数组,再写入数据
//     out.close();   //  关闭输出数据流
//     s.close();  //  关闭socket
//     System.out.println("数据传送完毕...");
//  }
//  catch(Exception e)
//  {
//     System.out.println("发生了"+e+"异常");
//  }
 
 
  //7.Client端的服务程序
  byte buff[]=new byte[1024];    // 建立byte类型的数组
  try
  {
     System.out.println("正在与服务器建立联机...");
     Socket s=new Socket("127.0.0.1",2525);    // 建立socket对象
     System.out.println("已经与服务器取得联机...");
     InputStream in=s.getInputStream();     //  建立输入数据流
     int n=in.read(buff); //  从数据流读入数据
     System.out.print("从服务端收到: ");
     System.out.println (new String(buff,0,n));// 打印读入的内容
     in.close();
     s.close();
  }
  catch(Exception e)
  {
     System.out.println("发生了"+e+"异常");
  }
}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值