- 三要素:IP地址,端口,协议
InetAddress的使用:
- static InetAddress getByName(String host) 确定主机名称的IP地址。主机名可以是IP地址也可以是机器名称。
- static InetAddress getByName(String host) 获取此IP地址的主机名
- String getHostAddress() 返回文本显示中的IP地址字符串
public class Demo {
public static void main(String[] args) throws UnknownHostException {
InetAddress address = InetAddress.getByName("192.168.0.101") ;
String name = address.getHostName() ;
String ip = address.getHostAddress() ;
System.out.println(name+","+ip);
}
}
UDP
- UDP发送数据
- 创建发送端的Socket对象(DatagramSocket)
- 创建数据,并把数据打包
- 调用DatagramSocket对象的方法发送数据
- 关闭发送端 ```java import java.io.IOException; import java.net.*; import java.nio.charset.StandardCharsets;
- UDP发送数据
public class Demo { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket() ; byte[] bys = “helloWorld”.getBytes(StandardCharsets.UTF_8) ; DatagramPacket dp = new DatagramPacket(bys,bys.length, InetAddress.getByName(“192.168.0.101”),10086) ; ds.send(dp); ds.close(); } }
- UDP接收数据
- 创建接收端的Scoket对象(DatagramSocket)
- 创建一个数据包,用于接收数据
- 调用DatagramSocket对象的方法接收数据
- 解析数据包,并把数据在控制台显示
- 关闭接收端
```java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Demo1 {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086) ;
byte[] bys = new byte[1024] ;
DatagramPacket dp = new DatagramPacket(bys,bys.length) ;
ds.receive(dp);
byte[] data = dp.getData() ;
System.out.println(new String(data,0,dp.getLength()));
ds.close();
}
}
/* 手动输入数据发送 */
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket() ;
Scanner sc = new Scanner(System.in) ;
while(true){
String line = sc.nextLine() ;
byte[] bys = line.getBytes(StandardCharsets.UTF_8) ;
DatagramPacket dp = new DatagramPacket(bys, bys.length,InetAddress.getByName("192.168.0.101"),10086) ;
ds.send(dp);
if(line.equals("886")){
break;
}
}
ds.close();
}
}
*************************************************************************************
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Demo1 {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086) ;
byte[] bys = new byte[1024] ;
DatagramPacket dp = new DatagramPacket(bys,bys.length) ;
while(true){
ds.receive(dp);
byte[] data = dp.getData() ;
String line = new String(data,0,dp.getLength()) ;
System.out.println(line);
if(line.equals("886")){
break;
}
}
}
}
- TCP
- 发送数据
- 创建客户端Socket对象(Socket)
- 获取输入流
- 释放资源
- 接收数据
- 创建服务器端Socket对象(Server Socket)
- 监听客户端连接,返回一个Socket对象
- 获取输入流,读数据,并把数据显示在控制台
- 释放资源 ```java / 发送端 / import java.io.IOException; import java.io.OutputStream; import java.net.*; import java.nio.charset.StandardCharsets;
- 发送数据
public class Demo { public static void main(String[] args) throws IOException { Socket socket = new Socket(“192.168.0.101”,10086) ; OutputStream os = socket.getOutputStream() ; os.write(“helloWorld”.getBytes(StandardCharsets.UTF_8)); socket.close();
}
}
```java
/* 接收端 */
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Demo1 {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10086) ;
Socket socket = ss.accept() ;
InputStream is = socket.getInputStream() ;
byte[] bys = new byte[1024] ;
int len = is.read(bys) ;
String data = new String(bys,0,len) ;
System.out.println(data);
ss.close();
socket.close();
}
}