网络编程
InetAddress的使用
实例化方法:
- InetAddressget ByName(String host) 使用IP地址或域名作为形参,生成一个IP地址的对象
- InetAddressget LocalHost() 获取本机IP地址生成一个IP地址的对象
此类使用两个静态实例化方法,其对象就是一个IP地址,
常用方法:
- getAddress() 返回IP地址
- getHostAddress 返回域名
TCP和UDP
TCP
- TCP的进程:客户端、服务端
- 先建立链接才能使用
- 三次握手,点对点通信,是可靠的
- 连接时有大量数据传输
- 传输完毕,需释放已经建立的链接,效率低
UDP
- UDP的进程:发送端、接收端
- 将数据封装成包,不需要建立链接
- 不保证数据的完整性,是不可靠的
- 一个数据包的大小限制到64K内
- 发送数据结束时,无需释放资源,开销小,通信效率高
TCP案例
客户端发送数据,服务端保存数据
| 客户端 |
服务端 |
| 1.创建Socket 指明对方的服务器端的IP地址和端口号 |
1.创建Server Socket |
| 2.创建File的实例、FileInputStream的实例 |
2.接收来自于客户端的socket:accept() 二次握手 |
- 通过Socket,获取输出流 读写数据|3.通过socket获取一个输入流
4.关闭相关流|4.创建File类的实例、FileOutStream的实例
|5.关闭相关流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| import org.junit.Test;
import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets;
public class TCPTest2 { @Test public void client() throws IOException { InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); int port = 9091; Socket socket = new Socket(inetAddress, port); FileInputStream fis = new FileInputStream(new File("pic.jpg")); OutputStream os = socket.getOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len = fis.read(bytes)) != -1) { os.write(bytes, 0, len); } System.out.println("数据发送完毕");
socket.shutdownOutput();
InputStream is = socket.getInputStream(); byte[] bytes1 = new byte[1024]; int len1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((len1 = is.read(bytes1)) != -1) { baos.write(bytes1,0,len1); } System.out.println(baos); is.close(); baos.close(); os.close(); fis.close(); socket.close(); }
@Test public void server() throws IOException { int port = 9091; ServerSocket serverSocket = new ServerSocket(port); Socket socket = serverSocket.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("pic_copy.jpg")); byte[] bytes = new byte[1024]; int len; while ((len = is.read(bytes)) != -1) { fos.write(bytes, 0, len); } System.out.println("数据接收完毕");
OutputStream os = socket.getOutputStream(); os.write("你的图片很漂亮,我接受到了".getBytes(StandardCharsets.UTF_8)); fos.close(); is.close(); socket.close(); serverSocket.close(); } }
|
注:在多次反复传输数据时,客户端需使用shutdown和close表明不再继续发送数据,如果不表明,服务端将一直等待客户端发送,形成死锁
UDP案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import org.junit.Test;
import java.io.IOException; import java.net.*;
public class UDPTest {
@Test public void sender() throws Exception { DatagramSocket ds = new DatagramSocket(); InetAddress byName = InetAddress.getByName("127.0.0.1"); int port = 9090; byte[] bytes = "我是发送端".getBytes("utf-8"); DatagramPacket dp = new DatagramPacket(bytes, 0, bytes.length, byName, port); ds.send(dp); ds.close(); }
@Test public void receiver() throws IOException { int port = 9090; DatagramSocket ds = new DatagramSocket(port); byte[] bytes = new byte[1024 * 64]; DatagramPacket dp = new DatagramPacket(bytes, 0, bytes.length); ds.receive(dp); java.lang.String str = new java.lang.String(dp.getData(), 0, dp.getLength()); System.out.println(str); ds.close(); } }
|
URL编程
URL(Uniform Resource Locator): 统一资源定位符
| 方法名 |
说明 |
| public String getProtocol() |
获取该URL的协议名 |
| public String getHost() |
获取该URL的主机名 |
| public String getPort() |
获取该URL的端口号 |
| public String getPath() |
获取该URL的文件路径 |
| public String getQuery() |
获取该URL的查询名 |
URL编程案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import org.junit.Test;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;
public class URLTest1 { @Test public void test1(){ HttpURLConnection urlConnection = null; InputStream is = null; FileOutputStream fos = null; try { URL url = new URL("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"); urlConnection = (HttpURLConnection) url.openConnection(); is = urlConnection.getInputStream(); File file = new File("dest.jpg"); fos = new FileOutputStream(file);
byte[] bytes = new byte[1024]; int len; while ((len = is.read(bytes)) != -1) { fos.write(bytes, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } try { is.close(); } catch (IOException e) { e.printStackTrace(); } urlConnection.disconnect(); } } }
|