Java 学习 ——网络与通信

URL:

URL是统一资源定位符(Uniform Resource Locator)的简称.它表示互联网上某一资源的地址.浏览器通过给定的URL可以找到相应的文件或其他资源.在某些情况下,URL中除IP地址以外的部分可以省略.例如:在浏览器地址栏输入java.sun.com,浏览器会默认使用http协议及相应的端口号,并使用Web服务器提供的默认的文件.URL的一般例子,: http://java.sum.com , ftp:192.168.10.233 http://java.sum.com/javase/downloads/index.jsp. 
java,使用java.net中的URL类可以创建代表互联网上某一具体资源的URL对象.通过此对象,利用相关的方法,可以轻松的进行网络资源的存取
2.1 URL类 
java.net 包提供URL,URL对象表示URL地址
(1) URL类的构造方法 
URL类提供多种不同的构造方法,用于以不同形式创建URL对象
public URL(String spec) throws MalformedURLException 
public URL(URL context, String spec) throws MalformedURLException 
public URL(String protocol, String host, String file) throws MalformedURLException 
public URL(String protocol, String host, int port, String file) throws MalformedURLException 
其中,参数spec是由协议名,主机名,端口号,文件名组成的字符串.参数context是已建立的URL对象,参数protocol是协议名,参数host是主机名,参数file是文件名,对数port是端口号
下面通过各种构造方法创建URL对象,分别以不同的方式提供URL地址的各部分信息 
URL myURL1=new URL(“http://www.tju.edu.cn:80/”); 
URL myURL2=new URL(“myURL1”,”support/fap.html”); 
URL myURL3=new URL(“http”,”www.tju.edu.cn”,”index.html”); 
URL myURL4=new URL(“http”,”www.tju.edu.cn”,80,”index.html”); 
上面的myURL2地址是由myURL1地址和用相对路径表示的文件名会成的,代表的URL地址是:http://www.tju.edu.cn:80/support/fap.html 
(2)获取URL对象的属性 
一个URL对象中包括各种属性,属性不能被改变,但可以通过下面的方法获取属性 
public String getProtocol()   //获取URL的协议名 
public String getHost() //获取URL 的主机名 
public int getPort() //获取URL的端口号 
public String getPath() //获取URL的文件路径 
public String getFile() //获取URL的文件名 
public String getRef() //获取URL在文件中的相对位置 
public String getQuery() //获取URL的查询名 
2.2 利用URL访问网上资源 
一个URL对象对应一个网址,,生成URL对象后,就可以调用URL对象的openStream()方法读取网址中的信息.openStream()方法的原型如下
public final InputSream openStream() 
调用openStream()方法获取的是一个InputSream输入流对象,通过read()方法只能从这个输入流中逐字节读取数据,也就是从URL网址中逐字节读取信息,为了能方便地从URL读取信息,通常将原始的InputSream输入流转变为其他类型的输入流,BufferedReader,


实例1:

import java.io.*;
import java.net.*;

public class URL1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try{
			
			URL u1=new URL("http://www.qq.com/");
			
			System.out.println(u1.getProtocol());
			
			System.out.println(u1.getHost());
			
			System.out.println(u1.getPort());
			
			System.out.println(u1.getFile());
		}
		catch(MalformedURLException e){
			
			System.out.println(e);
		}
		catch(IOException ee){
			
			System.out.println(ee);
		}
		catch(Exception eee){
			
			System.out.println(eee);
		}
	}

}


实例2:

import java.io.*;
import java.net.*;

public class URLT {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try{
			
			URL u1=new URL("http://www.hao123.com/");
			
			BufferedReader br=new BufferedReader(new InputStreamReader(u1.openStream()));
			
			String s;
			
			while((s=br.readLine())!=null){
				
				System.out.println(s);
			}
			
			br.close();
		}
		catch(MalformedURLException e){
			
			System.out.println(e);
		}
		catch(IOException ee){
			
			System.out.println(ee);
		}
		catch(Exception eee){
			
			System.out.println(eee);
		}
	}

}


Socket:

客户:

import java.io.*;
import java.net.*;

public class Kehu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try{
			
			Socket s=new Socket("127.0.0.1", 2007);
			
			InputStream is=s.getInputStream();
			
			OutputStream os=s.getOutputStream();
			
			PrintStream ps=new PrintStream(os);
			
			ps.println("hello");
			
			DataInputStream dis=new DataInputStream(is);
			
			String request=dis.readLine();
			
			System.out.println(request);
			
			s.close();
		}
		catch(ConnectException e){
			
			System.out.println(e);
		}
		catch(IOException ee){
			
			System.out.println(ee);
		}
		catch(Exception eee){
			
			System.out.println(eee);
		}
	}

}


服务:

import java.net.*;
import java.io.*;

public class Fuwu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try{
			
			ServerSocket ss=new ServerSocket(2007);
			while(true){
				
				Socket s=ss.accept();
				InputStream is=s.getInputStream();
				OutputStream os=s.getOutputStream();
				
				DataInputStream dis=new DataInputStream(is);
				
				String request=dis.readLine();
				PrintStream ps=new PrintStream(os);
				
				ps.println("good bye");
				
				s.close();
				ss.close();
			}
		}
		catch(IOException e){
				
			System.out.println(e);
		}
		catch(Exception ee){
				
			System.out.println(ee);
		}
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值