> 文档中心 > 网络编程基础知识

网络编程基础知识

网络编译中两个主要要素: 通信地址( ip和端口)和 各种网络协议

1.ip地址查詢

import java.net.InetAddress;import java.net.UnknownHostException;public class test1 {    public static void main(String[]args)    { try {     //查詢本地ip地址 这两个最常用了查询本机ip     InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");     System.out.println(inetAddress1);     InetAddress inetAddress4 = InetAddress.getByName("localhost");     System.out.println(inetAddress4);     //查詢網站ip地址     InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");     System.out.println(inetAddress2);     //查詢本地ip地址-方式二     InetAddress inetAddress3 = InetAddress.getLocalHost();     System.out.println(inetAddress3); } catch (UnknownHostException e) {     e.printStackTrace(); }    }}

2.端口

不同进程有不同的端口,不同的端口表示计算机上的不同进程

端口分类:

    公用端口:http:80

        https:443

        ftp:21

        telent:23

   程序注册端口:tomacat:8080

        mysql:3066

        oracle:1521

import java.net.InetSocketAddress;public class test2 {    public static void main(String[]args)    { //创建两个端口 InetSocketAddress inetSocketAddress =new InetSocketAddress("127.0.0.1",8080); System.out.println(inetSocketAddress); InetSocketAddress inetSocketAddress1 = new InetSocketAddress("localhost",8081); System.out.println(inetSocketAddress1); System.out.println(inetSocketAddress1.getAddress());//获得ip地址 System.out.println(inetSocketAddress1.getHostName());//根据域名获取 System.out.println(inetSocketAddress1.getPort());//获得端口号    }}

3.通信协议:

网络通信协议:速率,传输码率,代码结构,传输控制,....

tcp: 类似于打电话,打通了,有人接,发过去 (用户传输协议)

->连接,稳定,客户端,服务端,传输完成,就会释放连接,效率低

三次握手 四次挥手

至少需要三次 确定连接

A;你去跳舞吗

B:   我去跳舞

A:那我们走吧

至少需要四次 确定断开

A;我要走了

B: 知道了

B:你真的走了么

A:我真的要走了

udp:类似于发短信,只要发过去就不再管了 (用户数字报协议)不连接,不稳定

->客服端,服务端,没有明确的界限,不管有没有准备好,都可以发给你

3.1 Tcp实现聊天

客户端:

1.连接服务器

2.发送消息

服务器:

1.建立服务的端口serverSocket

2.等待用户的连接 

3.接受用户的信息

客户端:

import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;public class tcpcClientDemo1 {    public static void main(String[] args) { Socket socket = null; OutputStream os = null; try {     //服务器的地址     InetAddress  server = InetAddress.getByName("127.0.0.1");     //端口号     int port = 3000;     //创建一个socket连接     socket = new Socket(server,port);     //发送消息 io流     os = socket.getOutputStream();     os.write("你好,欢迎信息来往".getBytes()); } catch (Exception e) {     e.printStackTrace(); } finally {     if(socket != null)     {  try {      socket.close();  } catch (IOException e) {      e.printStackTrace();  }     }     if(os!= null)     {  try {      os.close();  } catch (IOException e) {      e.printStackTrace();  }     } }    }}

服务器

import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;public class tcpSeverseDemo1 {    public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; //创建地址 try {     serverSocket = new ServerSocket(3000);     //等待客户端来凝结过来     socket = serverSocket.accept();     //读取客户端的消息     is = socket.getInputStream();     //管道流     baos = new ByteArrayOutputStream();     byte[] buffer = new byte[1024];     int len;     while((len=is.read(buffer))!=-1)     {  baos.write(buffer,0,len);     } } catch (IOException e) {     e.printStackTrace(); }finally{     System.out.println(baos.toString());     if(baos != null) {  try {      baos.close();  } catch (IOException e) {      e.printStackTrace();  }     }     if(is != null){  try {      is.close();  } catch (IOException e) {      e.printStackTrace();  }     }     if(socket != null){  try {      socket.close();  } catch (IOException e) {      e.printStackTrace();  }     }     if(serverSocket != null) {  try {      serverSocket.close();  } catch (IOException e) {      e.printStackTrace();  }     }}    }}

3.2  tcp实现文件上传

客户端:

import java.io.*;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;public class tcoClientDemo2 {    public static void main(String[] args) throws Exception { //创建一个Socket连接 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),3000); //创建一个输出流 OutputStream os = socket.getOutputStream(); //文件流 读取文件 FileInputStream  fils = new FileInputStream(new File("开会图片.jpg")); //写出文件 byte [] buffer = new byte[1024]; int len; while((len =fils.read(buffer))!= -1) {     os.write(buffer,0,len); } //通知服务器 我已经传输完了 socket.shutdownOutput();//我已经传输完了 InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream bans= new ByteArrayOutputStream(); byte[] buffer2= new byte[1024]; int len2; while((len2=inputStream.read(buffer2))!=-1) {     bans.write(buffer2,0,len2); } System.out.println(bans.toString()); inputStream.close(); bans.close(); os.close(); fils.close(); socket.close();    }}

服务器端:

import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class tcpSeverseDemo2 {    public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(3000); Socket socket = serverSocket.accept(); //3.获得输入流 InputStream is = socket.getInputStream(); //4.文件输出 FileOutputStream fils = new FileOutputStream("数据202.jpg"); byte [] buffer  = new byte[1024]; int len; while((len=is.read(buffer))!= -1) {     fils.write(buffer,0,len); } OutputStream os = socket.getOutputStream(); os.write("我读取完毕了,你可以结束啦".getBytes()); fils.close(); socket.close(); is.close(); serverSocket.close();    }}

 3.3 Udp数据报发送接收

发送端:

package class2;import java.io.InputStream;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;public class UdpClientDemo1 {    public static void main(String[] args) throws Exception { //1.建立一个Socket 传送的是数据报 没有所谓的服务器 DatagramSocket socket = new DatagramSocket(); //2.建个包 String msg = "你好啊,服务器!"; InetAddress localhost = InetAddress.getByName("localhost"); int port = 3000; //3.数据 数据的长度起始,要发送给谁 DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port); //4.发送包 socket.send(packet); //5.关闭流 socket.close();    }}

接收端:

package class2;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class UdpSeverseDemo1 {    public static void main(String[] args) throws Exception { //1.开放端口 DatagramSocket socket = new DatagramSocket(3000); //2.接收数据包 byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); //阻塞接收 socket.receive(packet); System.out.println(packet.getAddress().getHostAddress()); System.out.println(new String(packet.getData(), 0, packet.getLength())); socket.close();    }}