Java 网络编程基础学习小结

1、ip地址和端口

ip地址标识了主机在网络中的位置,通过ip地址可以对主机进行访问(如果允许的话)

端口:通过程序的指定端口,可以与指定的程序进行数据交互

2、URL

一个完整的URL由协议名、主机名(主机IP)、端口号和文件路径四部分组成:

协议名(protocol):指明获取资源所使用的传输协议,如http、ftp等,使用冒号(:)来将它与其他部分相隔离。

主机名(host):指定获取资源的域名,此部分由左边的双斜线(//)和右边的单斜线(/)或可选冒号(:)限制。

端口(port):指定服务的端口号,是可选的参数,由主机名左边的冒号(:)和右边的斜线(/)限制。

文件路径(file):指定访问的文件名及路径。


3、InetAdress 类 (五个方法的使用)

①public static InetAddress getByName(String host) throws    UnknownHostException

该静态方法InetAddress.getByName(host),将需要寻找的主机名作为参数,使用DNS查找主机的IP地址。返回一个包含主机名和IP地址的InetAddress对象。


②public static InetAddress[ ] getAllByName(String host) throws  UnknownHostException 
有些计算机具有一个以上的Internet地址,若给定一个主机名,该静态方法InetAddress.getAllByName(host),将会返回一个包含了与该主机名相对应的所有地址的InetAddress对象数组。


③public static InetAddress getLocalHost( ) throws  UnknownHostException
若执行静态方法InetAddress.getLocalHost( ),它将返回一个包含本地计算机的主机名和IP地址的InetAddress对象。

④public String getHostName( )
返回一个String,该字符串包含了具有该InetAddress对象表示的IP地址的主机名称。 如果安全管理程序不允许这种操作,则返回原样的IP地址。


 ⑤public String getHostAddress( ) 
 返回一个String,该字符串包含了点分格式的IP地址。

4、URL编程

主要是 InetAddress的使用, URL对象的使用, URLConnection的使用, 下面java代码,简单的写了一下这三个类的常见应用。

package java2exam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Network {
	public static void main(String[] args) {
		NetWorkHelper n = new NetWorkHelper();
		// n.InetAddressTest();
		n.urlTest();
		n.urlConnectionTest();
	}
}

class NetWorkHelper {
	/**
	 * 
	 * //重点就是 InetAddress 需要通过静态方法来获取对象, 不能直接new
	 * 
	 */
	void InetAddressTest() {
		try {
			InetAddress i = InetAddress.getLocalHost();
			System.out.println("主机名:" + i.getHostName());
			System.out.println("主机地址:" + i.getHostAddress());

			InetAddress i2 = InetAddress.getByName("www.lfork.top");
			System.out.println("主机名:" + i2.getHostName());
			System.out.println("主机地址:" + i2.getHostAddress());

			InetAddress i3[] = InetAddress.getAllByName("www.google.cn");
			for (InetAddress inet : i3) {
				System.out.println("主机名:" + inet.getHostName());
				System.out.println("主机地址:" + inet.getHostAddress());
			}

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * //重点 ①构造方法 ②openStream()方法 返回一个InputStream流 ③openConnection方法
	 */
	void urlTest() {
		try {
			URL url = new URL("http://www.lfork.top/WebTask/java.html");
			InputStreamReader in = new InputStreamReader(url.openStream());
			BufferedReader reader = new BufferedReader(in);
			String str = reader.readLine();
			int line = 0;
			while (str != null) {
				line++;
				System.out.println(line + ":\t" + str);
				str = reader.readLine();
			}
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 重点:①使用URLConnection 可以进行双向通信 
	 * ②使用URL 的openConnection方法来获取一个URLConnection连接
	 * ③URLConnection是以HTTP协议为中心的类,其中很多方法只有在处理HTTP的URL时才起作用
	 * ④ getOutputStream()  和 getInputStream()
	 * 
	 */
	void urlConnectionTest() {
		try {
			URL url = new URL("http://www.lfork.top/WebTask/java.html");
			URLConnection uc = url.openConnection();

			InputStreamReader in = new InputStreamReader(uc.getInputStream());
			BufferedReader reader = new BufferedReader(in);
			String str = reader.readLine();
			int line = 0;
			while (str != null) {
				line++;
				System.out.println(line + ":\t" + str);
				str = reader.readLine();
			}

			Date d = new Date(uc.getLastModified());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); // kk表示 24小时制

			sdf.format(d);
			System.out.println(sdf.format(d));

			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

5、TCP Socket编程  

主要还是如何使用,简单测试代码如下
客户端
package java2exam;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class SocketClient {
	public static void main(String[] args) {
		try {
			Socket s = new Socket("localhost", 6000);
			OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream(), "utf-8");
			out.write("heiheiehie");
			out.close();
			s.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}





  
  

服务端

package java2exam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
	public static void main(String[] args) {
		try {
			ServerSocket server = new ServerSocket(6000);
			Socket socket = server.accept();
			InputStreamReader in = new InputStreamReader(socket.getInputStream());
			BufferedReader reader = new BufferedReader(in);
			System.out.println(reader.readLine());
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


6、UDP 编程

测试代码如下
客户端
package java2exam;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {
	public static void main(String[] args) {
		try {
			String data = "hahahhaha";
			byte buf[] = data.getBytes("utf-8");	//保证不乱码
			DatagramSocket client = new DatagramSocket();
			InetAddress address = InetAddress.getByName("localhost");
			DatagramPacket p = new DatagramPacket(buf, 0, buf.length, address, 6000);
			client.send(p);
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}

服务端
package java2exam;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServer {
	public static void main(String[] args) {
		try {
			DatagramSocket server = new DatagramSocket(6000);
			byte buf[] = new byte[60000];
			DatagramPacket p = new DatagramPacket(buf, buf.length);
			server.receive(p); 
			String data = new String(p.getData(), 0, p.getData().length, "utf-8");
			System.out.println(data);
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值