您的位置:首页 > 理论基础 > 计算机网络

Java 网络编程实例

2016-07-12 16:39 375 查看
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+"异常");

  }

}

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: