计算机网络
- 计算机网络定义:
- 计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络通信协议
- 网络通信协议
- 计算机网络中实现通信必须有一些约定即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准。好比公路交通规则,学生守则。
- ISO/OSI参考模型
分为7层(从底到高):
物理层---->数据链路层---->网络层---->传输层---->会话层---->表示层---->应用层 - TCP/IP协议栈:
分为4层(同上):
物理+数据链路层---->网络层(IP)---->传输层(TCP/UDP)---->应用层 - 1、封装(发送数据)
- 2、拆装(接收数据)
TCP协议和UDP协议
– - Socket套接字
Socket实际是网络传输层供给应用层的编程接口。传输层则在网络层的基础上提供进程到进程问的逻辑通道,而应用层的进程则利用传输层向另一台主机的某一进程通信。Socket就是应用层与传输层之间的桥梁
使用Socket编程可以开发客户机和服务器应用程序,可以在本地网络上进行通信,也可通过Internet在全球范围内通信 - TCP编程
用户输入用户名密码,服务器给出登录成功或失败的提示;使用基于TCP协议的Socket网络编程实现;在网络通讯中,第一次主动发起通讯的程序被称作客户端(Client)程序,第一次通讯中等待连接的程序被称作服务器端(Server)程序,利用IO流实现数据的传输。 - 客户端发送信息:
public class ClientSocket1 {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("客户端");
Socket socket = new Socket("接收端的IP",20000);
OutputStream os=socket.getOutputStream();
DataOutputStream dos= new DataOutputStream(os);
dos.writeUTF("Hello");
dos.flush();
dos.close();
}
}
- 服务器接收信息:
public class ServerSocket2 {
public static void main(String[] args) throws IOException {
System.out.println("服务器");
ServerSocket serverSocket = new ServerSocket();
Socket client=serverSocket.accept();
InputStream is=client.getInputStream();
DataInputStream dis = new DataInputStream(is);
dis.readUTF();
dis.close();
is.close();
client.close();
}
}
在这里面我把异常全部抛出去了,你们练习的时候自己解决
- 实现俩个人在控制台聊天:
客户端:
public class Socket1 {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("客户端启动");
Scanner scanner = new Scanner(System.in);
while(true){
Socket socket = new Socket("接收端的IP", 15980);//后面端口号
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(scanner.next());
ServerSocket serverSocket = new ServerSocket(15980);//端口号
Socket socket1 = serverSocket.accept();
DataInputStream dis = new DataInputStream(socket1.getInputStream());
dis.readUTF();
dis.close();
dos.close();
}
}
}
服务端:
public class ServerSocket1 {
public static void main(String[] args) throws IOException {
System.out.println("服务器启动");
Scanner scanner = new Scanner(System.in);
while (true) {
ServerSocket serverSocket = new ServerSocket(15980);
Socket socket1 = serverSocket.accept();
DataInputStream dis = new DataInputStream(socket1.getInputStream());
System.out.println(socket1.getInetAddress() + "发送:" + dis.readUTF());
Socket socket = new Socket("192.168.88.212", 15980);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(scanner.next());
dis.close();
dos.close();
}
}
}
- UDP编程
- 用UDP实现俩人在控制台聊天,输入bye结束
客户端:
public class AskClient {
public static void main(String[] args) throws IOException {
System.out.println("客户端启动");
DatagramSocket ds = new DatagramSocket();// 端口接收数据
Scanner scanner = new Scanner(System.in);
while (true) {
String str = scanner.nextLine();
byte[] buf1 = new byte[1024];
DatagramPacket dp1 = new DatagramPacket(buf1, buf1.length, InetAddress.getByName("192.168.88.123"), 20005);
ds.receive(dp1);
byte[] buf = str.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.send(dp);
System.out.println("收到的数据:" + new String(dp1.getData()));
if ("bye".equals(str)) {
break;
}
}
ds.close();
}
}
服务器端:
public class AskServer {
public static void main(String[] args) throws IOException {
System.out.println("在线客服 服务器正在启动-----");
Scanner scanner = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket(20005);
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String info = new String(dp.getData());
System.out.println("收到数据" + info);
String str = scanner.nextLine();
byte[] buf1 = str.getBytes();
DatagramPacket dp1 = new DatagramPacket(buf1, buf1.length, dp.getAddress(), dp.getPort());
ds.send(dp1);
if ("bye".equals(info)) {
break;
}
}
ds.close();
}
}
URl
URL u = new URL("https://www.baidu.com/");
System.out.println("获取与此url关联的协议的默认端口:"+u.getDefaultPort());
System.out.println(“getFile:”+u.getFile()); //端口号后面的内容
System.out.println("主机名:"+u.getHost()); //www.google.cn
System.out.println(“路径:”+u.getPath()); //端口号后,参数前的内容
System.out.println(“端口:”+u.getPort()); //存在返回80.否则返回-1
System.out.println("协议:"+u.getProtocol());
System.out.println("参数部分:"+u.getQuery());
System.out.println("锚点:"+u.getRef());
把网络上的图片写到本地:
public class DownImageUtils {
String usrStr;
File file;
public void downImagefile(String arr, File file2) {
HttpURLConnection hu = null;
FileOutputStream fos = null;
URL url = null;
BufferedInputStream bis = null;
try {
url = new URL(usrStr);
hu = (HttpURLConnection) url.openConnection();
hu.connect();
bis = new BufferedInputStream(hu.getInputStream());
fos = new FileOutputStream(file);
byte[] buf = new byte[512];
int len = -1;
while ((len = bis.read(buf)) != -1) {
fos.write(buf, 0, len);
fos.flush();
}
} catch (MalformedURLException e) {
System.out.println("URL连接");
e.printStackTrace();
} catch (IOException e) {
System.out.println("");
e.printStackTrace();
} finally {
try {
fos.close();
bis.close();
hu.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String[] arr = {
"http://img.sccnn.com/bimg/339/17195.jpg",
"http://img.sccnn.com/bimg/339/18041.jpg" };
DownImage down = new DownImage();
for (int i = 0; i <arr.length; i++) {
down.downTmagefile(arr[i], new File("F:\\"+i+"a.jpg"));
}
}
}