赞
踩
IP网络中每台主机都必须有一个惟一的IP地址;IP地址是一个逻辑地址;因特网上的IP地址具有全球唯一性;32位, 4个字节,常用点分十进制的格式表示,例如: 192.168.0.200
为进行网络中的数据交换(通信)而建立的规则、标准或约定。 (=语义+语法+规则)不同层具有各自不同的协议。
多种通信媒介——有线、无线……
不同种类的设备——通用、专用……
不同的操作系统——Unix、 Windows ……
不同的应用环境——固定、移动……
不同业务种类——分时、交互、实时……
宝贵的投资和积累——有形、无形……
用户业务的延续性——不允许出现大的跌宕起伏。
它们互相交织,形成了非常复杂的系统应用环境。
OSI(Open System Interconnection)参考模型将网络的不同功能划分为7层
• 通信实体的对等层之间不允许直接通信。
• 各层之间是严格单向依赖。
• 上层使用下层提供的服务 — Service user;
• 下层向上层提供服务 — Service provider
• 对等层实体之间虚拟通信。
• 下层向上层提供服务,实际通信在最底层完成
package JavaBase.network; import java.net.URL; public class Url1 { public static void main(String[] args) throws Exception { URL url = new URL("http://java.sun.com:80/docs/books/tutorial/index.html#DOWN"); String protocal = url.getProtocol(); String boot = url.getHost(); String file = url.getFile(); int port = url.getPort(); String ref = url.getRef(); System.out.println(protocal + "," + boot + "," + file + "," + port + "," + ref); } }
结果是:
http,java.sun.com,/docs/books/tutorial/index.html,80,DOWN
package JavaBase.network; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; public class UrlConnection1 { public static void main(String[] args) throws Exception{ URL url = new URL("https://www.infoq.com/"); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); //InputStream inputStream = url.openStream();等价与上俩行代码 OutputStream outputStream = new FileOutputStream("D:/infoq.txt"); byte[] buffer = new byte[2048]; int length = 0; while (-1 != (length = inputStream.read(buffer, 0, buffer.length))) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } }
结果是:
package JavaBase.network; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class UrlConnection3 { public static void main(String[] args) throws Exception{ URL url = new URL("http://www.qq.com"); //将字节流转为字符流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream())); String line = null; while (null != (line = bufferedReader.readLine())) { System.out.println(line); } bufferedReader.close(); } }
URL和URLConnection类对于希望建立与HTTP服务器的连接来获取信息的简单程序来说是非常好的
static InetAddress getLocalHost( ) throws UnknownHostException
static InetAddress getByName(String hostName) throws UnknownHostException
static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
getLocalHost( )仅返回象征本地主机的InetAddress对象。
getByName( )方法返回一个传给它的主机名的InetAddress。
如果这些方法不能解析主机名,它们引发一个UnknownHostException异常。
在Internet上,用一个名称来代表多个机器是常有的事。 getAllByName( )工厂方法返回代表由一个特殊名称分解的所有地址的InetAddresses类数组。在不能把名称分解成至少一个地址时,它将引发一个Unknown HostException异常。
package JavaBase.network; import java.net.InetAddress; public class InetAddressTest { public static void main(String[] args) throws Exception{ InetAddress address = InetAddress.getLocalHost(); System.out.println(address); address = InetAddress.getByName("www.sohu.com"); System.out.println(address); } }
Socket是连接运行在网络上的两个程序间的双向通讯的端点
package JavaBase.network; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServer { public static void main(String[] args) throws Exception{ /** * 服务器端,建立连接 * 先用端口号声明一个ServerSocket,如果写0系统随机分配空闲端口号 * 然后调用accept方法来获取连接,在没有客户端连接之前,服务器一直等待 * 接下来相当于IO通信了。 * 接受hello world返回welcome */ ServerSocket serverSocket = new ServerSocket(5000); Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); byte[] buffer = new byte[200]; int length = inputStream.read(buffer); System.out.println(new String(buffer, 0, length)); outputStream.write("welcome".getBytes()); inputStream.close(); outputStream.close(); socket.close(); } }
结果是:
hello world
package JavaBase.network; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TcpClient { public static void main(String[] args) throws Exception{ /** * 这个为客户端,要连接服务器 * 第一个参数为要链接的服务器,第二个为端口号 * 发送hello world,接受welcome */ Socket socket = new Socket("127.0.0.1", 5000); OutputStream outputStream = socket.getOutputStream(); InputStream inputStream = socket.getInputStream(); outputStream.write("hello world".getBytes()); byte[] buffer = new byte[200]; int length = inputStream.read(buffer); System.out.println(new String(buffer, 0, length)); inputStream.close(); outputStream.close(); socket.close(); } }
结果是:
welcome
一对一聊天程序的实现,服务器端和客户端分别有两个线程,一个读,一个写
服务器端
package JavaBase.network; import java.net.ServerSocket; import java.net.Socket; public class MainServer { public static void main(String[] args) throws Exception{ ServerSocket serverSocket = new ServerSocket(5000); while (true) { Socket socket = serverSocket.accept(); //启动读写线程 new ServerInputThread(socket).start(); new ServerOutputThread(socket).start(); } } }
读
package JavaBase.network; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class ServerInputThread extends Thread{ private Socket socket; public ServerInputThread(Socket socket) { this.socket = socket; } @Override public void run() { try { InputStream inputStream = socket.getInputStream(); while (true) { byte[] buffer = new byte[1024]; int length = inputStream.read(buffer); String string = new String(buffer, 0, length); System.out.println(string); } } catch (IOException e) { e.printStackTrace(); } } }
写
package JavaBase.network; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; public class ServerOutputThread extends Thread { private Socket socket; public ServerOutputThread(Socket socket) { this.socket = socket; } @Override public void run() { try { OutputStream outputStream = socket.getOutputStream(); while (true) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String line = bufferedReader.readLine(); outputStream.write(line.getBytes()); } } catch (IOException e) { e.printStackTrace(); } } }
客户端
package JavaBase.network;
import java.net.Socket;
public class MainClient {
public static void main(String[] args) throws Exception{
Socket socket = new Socket("127.0.0.1", 5000);
new ClientInputThread(socket).start();
new ClientOutputThread(socket).start();
}
}
读
package JavaBase.network; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class ClientInputThread extends Thread{ private Socket socket; public ClientInputThread(Socket socket) { this.socket = socket; } @Override public void run() { try { InputStream inputStream = socket.getInputStream(); while (true) { byte[] buffer = new byte[1024]; int length = inputStream.read(buffer); String string = new String(buffer, 0, length); System.out.println(string); } } catch (IOException e) { e.printStackTrace(); } } }
写
package JavaBase.network; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; public class ClientOutputThread extends Thread{ private Socket socket; public ClientOutputThread(Socket socket) { this.socket = socket; } @Override public void run() { try { OutputStream outputStream = socket.getOutputStream(); while (true) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String line = bufferedReader.readLine(); outputStream.write(line.getBytes()); } } catch (IOException e) { e.printStackTrace(); } } }
package JavaBase.network; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UdpTest2 { public static void main(String[] args) throws Exception{ //先启动 DatagramSocket socket = new DatagramSocket(7000); byte[] buffer = new byte[1000]; DatagramPacket packet = new DatagramPacket(buffer, 1000); socket.receive(packet); System.out.println(new String(buffer, 0, packet.getLength())); String string = "welocme"; //从发送方接受地址和端口号,并回复信息 DatagramPacket packet1 = new DatagramPacket(string.getBytes(), string.length(), packet.getAddress(), packet.getPort()); socket.send(packet1); socket.close(); } }
结果是:
hello world
package JavaBase.network; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UdpTest1 { public static void main(String[] args) throws Exception{ DatagramSocket socket = new DatagramSocket(); String string = "hello world"; DatagramPacket packet = new DatagramPacket(string.getBytes(), string.length(), InetAddress.getByName("localhost"), 7000); socket.send(packet); byte[] buffer = new byte[100]; DatagramPacket packet1 = new DatagramPacket(buffer, 100); socket.receive(packet1); System.out.println(new String(buffer, 0, packet.getLength())); socket.close(); } }
结果是:
welcome
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。