Java中的InetAddress代表地址,主要是代表ip地址

获取地址的对象
(1)public static InetAddress getLocalhost() throws UnkownHostException:获取本地的主机
(2)public static InetAddress getByName(String host)throws UnkownHostException:依据主机名(Ip地址)获取主机

通过地址对象获取ip字符串或者是主机的名称
(1)public static String getHostName():获取主机的名字
(2)public static String getHostAddress():获取主机的字符串形式ip

UDP是一个传输协议

特点:传输速度快,但不稳定,无连接,存在丢包。
通常用于不是非常要求专业的传输,比如微信语音和视频,网络游戏
网络编程 - 图1

使用UDP的步骤:
1、创建连接:DatagramSocket(int port):指定端口号,方便发送端寻找
2、建立数据包,用于接收数据:DatagramSocket(byte[] buf,int length)
3、调用socket的方法
4、解析数据
5、关闭socket

  1. package cn.ant.demoNet;
  2. import java.io.IOException;
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6. import java.net.SocketException;
  7. import java.util.Scanner;
  8. /*
  9. * 发送数据
  10. * */
  11. public class UDPClient {
  12. public static void main(String[] args) throws IOException {
  13. //建立连接
  14. DatagramSocket ds=new DatagramSocket();
  15. Scanner sc=new Scanner(System.in);
  16. //循环发送
  17. while(true){
  18. //创建数据
  19. System.out.println("请输入数据:");
  20. String s=sc.next();
  21. //打包数据
  22. byte[] data=s.getBytes();
  23. int length=data.length;
  24. //连接服务器的ip地址和端口
  25. InetAddress address=InetAddress.getByName("localhost");
  26. int port=5650;
  27. //打包
  28. DatagramPacket dp=new DatagramPacket(data, length,address,port);
  29. //调用方法
  30. ds.send(dp);
  31. }
  32. //关闭
  33. // ds.close();
  34. }
  35. }
  1. package cn.ant.demoNet;
  2. import java.net.DatagramPacket;
  3. import java.net.DatagramSocket;
  4. import java.net.InetAddress;
  5. import java.net.SocketException;
  6. public class UDPServer {
  7. public static void main(String[] args) throws Exception {
  8. //只接收数据
  9. //1、建立连接
  10. DatagramSocket ds=new DatagramSocket(45377);
  11. //反复接收数据
  12. while(true){
  13. //建立数据包 用于接收数据
  14. byte[] buf=new byte[1024];
  15. DatagramPacket dp=new DatagramPacket(buf, buf.length);
  16. //调用socket方法接收数据
  17. ds.receive(dp);
  18. //解析数据 针对数据包进行解析 --dp\
  19. InetAddress sendAddress=dp.getAddress();
  20. String sendIP=sendAddress.getHostAddress();
  21. int sendPort=dp.getPort();
  22. //获取数据包中的数据
  23. byte[] data=dp.getData();
  24. int length=dp.getLength();
  25. //将字节数据中的数据转换成字符串
  26. String content=new String(data,0,length);
  27. //输出
  28. System.out.println("接收到来自"+sendIP+"的,端口号为"+sendPort+"的数据:"+content);
  29. }
  30. //关闭
  31. // ds.close();
  32. }
  33. }

Java中提供了Socket进行网络传输

TCP是一个传输协议

image.png

  1. package cn.ant.demoNet;
  2. import java.net.*;
  3. import java.io.*;
  4. public class MyServer extends Thread{
  5. public static void main(String[] args) throws IOException {
  6. //建立服务器端
  7. ServerSocket serverSocket=new ServerSocket(5650);
  8. //当前是服务器端 所以获取io流的同时需要等待客户端发送数据
  9. Socket socket=serverSocket.accept();
  10. //
  11. System.out.println(socket.getInetAddress().getHostAddress());
  12. //建立连接之后,通过socket中的io流,进行数据传输
  13. InputStream is=socket.getInputStream();
  14. //读取反复读取客户端发送过来的数据
  15. int read=-1;
  16. while((read=is.read())!=-1){
  17. System.out.print((char)read);
  18. }
  19. //接收到客户端的数据之后 返回一个数据给客户端
  20. OutputStream os=socket.getOutputStream();
  21. String content="o";
  22. os.write(content.getBytes());
  23. //关闭
  24. socket.close();
  25. serverSocket.close();
  26. }
  27. }
  1. package cn.ant.demoNet;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. public class MyClients {
  8. public static void main(String[] args) throws IOException, IOException {
  9. // TODO Auto-generated method stub
  10. //锟斤拷锟斤拷锟酵伙拷锟斤拷
  11. Socket socket=new Socket("127.0.0.1",5650);
  12. //锟斤拷锟斤拷锟斤拷锟接猴拷始锟斤拷锟斤拷
  13. OutputStream os=socket.getOutputStream();
  14. // //请求
  15. // string req = "";
  16. // req += "GET " + res + " HTTP/1.1\r\n";
  17. // req += "Host: " + host + "\r\n";
  18. // req += "User-Agent:*\r\n";
  19. // req += "Connection:Close\r\n";
  20. // req += "\r\n";
  21. //锟斤拷锟斤拷锟斤拷锟斤拷
  22. String content="I love Java 锟斤拷锟斤拷锟芥拻";
  23. os.write(content.getBytes());
  24. //锟斤拷锟竭凤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  25. socket.shutdownOutput();
  26. //锟斤拷取锟斤拷锟斤拷锟斤拷锟剿碉拷锟斤拷应--o
  27. InputStream is=socket.getInputStream();
  28. int read=-1;
  29. while((read=is.read())!=-1){
  30. System.out.print((char)read);
  31. }
  32. socket.close();
  33. }
  34. }