InetAddress关键字
为了方便我们获取和操作IP地址,java提供了一个类InetAddress 供我们使用
- 确定主机名称的IP地址。public static InetAddress getByName(String host)
- 获取此IP地址的主机名。String getHostName()
- 返回文本显示中的IP地址字符串,String getHostAddress()。
代码如下:
public class InetAddressDemo {
public static void main(String[] args) {
//public static InetAddress getByName(String host) throws UnknownHostException
// 确定主机名称的IP地址。
InetAddress addressName = null;
try {
addressName = InetAddress.getByName("192.168.10.147");
System.out.println(addressName);
} catch (UnknownHostException e) {
e.printStackTrace();
}
//String getHostName()
//获取此IP地址的主机名。
String hostName = addressName.getHostName();
System.out.println(hostName);
//String getHostAddress()
//返回文本显示中的IP地址字符串。
String hostAddress = addressName.getHostAddress();
System.out.println(hostAddress);
//static InetAddress getLocalHost()
//返回本地主机的地址。
try {
InetAddress localHost = addressName.getLocalHost();
System.out.println(localHost);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
UDP 协议建立连接
UDP协议发送数据的步骤:
- 1.创建发送端的Socket对象
- 2.创建数据,并将数据打包
- 3.调用Socket对象中的一个办法,将数据包发送出去
- 4.释放资源,关闭Socket对象
public class SendDemo1 {
public static void main(String[] args) {
//创建发送端的Socket对象
//DatagramSocket()
//构造数据报套接字并将其绑定到本地主机上的任何可用端口。
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
//将数据转成字节数组
byte[] bytes = "云想衣裳花想容".getBytes();
//获取字节数组的长度
int len = bytes.length;
//IP地址,(你要发送给谁的ip地址)
InetAddress address = null;
try {
address = InetAddress.getByName("192.168.10.147");
} catch (UnknownHostException e) {
e.printStackTrace();
}
//指定接收的数据端口
int port = 10086;
//创建一个数据包
//DatagramPacket(byte[] buf, int length, InetAddress address, int port)
//构造用于发送长度的分组的数据报包 length指定主机上到指定的端口号。
DatagramPacket datagramPacket = new DatagramPacket(bytes, len, address, port);
//void send(DatagramPacket p)
//从此套接字发送数据报包。
try {
ds.send(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
}
//释放资源,关闭Socket对象
ds.close();
}
}
UDP接收端代码步骤
- 1.接收端的Socket对象
- 2.创建一个数据包(用来接收数据的容器)
- 3.调用Socket对象中的方法来接收数据
- 4.解析数据包,打印在控制台上
- 5.释放资源
- 注意:接收端不能重复启动
public class ReceiveDemo1 {
public static void main(String[] args) {
//创建接收端的Socket对象
//DatagramSocket(int port)
//构造函数报套接字并将其绑定到本地主机的指定端口
DatagramSocket ds = null;
try {
ds= new DatagramSocket(10086);
} catch (SocketException e) {
e.printStackTrace();
}
//创建一个数据包(用来接收数据的容器)
//DatagramPacket(byte[] buf,int length)
//构造一个 DatagramPacket 用于接收数据包length
byte[] bytes = new byte[1024];
int len = bytes.length;
DatagramPacket dp = new DatagramPacket(bytes, len);
//调用Socket对象中的方法来接收数据
//void receive(DatagramPacket P)
try {
ds.receive(dp); // 阻塞方法
} catch (IOException e) {
e.printStackTrace();
}
InetAddress address = dp.getAddress();
String ip = address.getHostAddress();
String hostName = address.getHostName();
//解析数据包,打印到控制台上
//byte[] getData()
//返回数据缓冲区
byte[] data = dp.getData();
int length = dp.getLength();//获取实际长度
String s = new String(data,0,length);
System.out.println(hostName+",IP地址为:"+ip+"发送的内容为:"+s);
//释放资源
ds.close();
}
}
通过循环改进,代码如下
发送端:
package com.bigdat.java.day28.udpcoding;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class SendDemo2 {
public static void main(String[] args) {
while(true){
DatagramSocket ds = null;
//发送端发送数据
try {
ds = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
//创建一个输入对象
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
if("886".equals(next)){
break;
}else{
byte[] bytes = next.getBytes();
int length = bytes.length;
InetAddress address = null;
try {
address = InetAddress.getByName("192.168.10.147");
} catch (UnknownHostException e) {
e.printStackTrace();
}
int port = 10086;
DatagramPacket datagramPacket = new DatagramPacket(bytes,length,address,port);
try {
ds.send(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
}
//关闭资源
//ds.close();
}
}
}
}
接收端:
package com.bigdat.java.day28.udpcoding;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ReceiveDemo2 {
public static void main(String[] args) {
//创建接收对象
DatagramSocket ds = null;
try {
ds = new DatagramSocket(10086);
} catch (SocketException e) {
e.printStackTrace();
}
System.out.println("====================接收端开始接收数据:=======================");
while(true){
//创建一个DatagramPacket 类的对象创建一个容器
byte[] bytes = new byte[1024];
int length = bytes.length;
DatagramPacket dp = new DatagramPacket(bytes,length );
//调用Socket方法receive 方法接收数据
try {
ds.receive(dp); // 阻塞数据包
} catch (IOException e) {
e.printStackTrace();
}
InetAddress address = dp.getAddress();
String hostName = address.getHostName(); //IP地址对应主机名
String hostAddress = address.getHostAddress();//IP 地址
byte[] data = dp.getData();
int length1 = data.length;
String s = new String(data, 0, length1);
System.out.println(hostName+",IP地址为:"+hostAddress+"发送的信息为:"+s);
}
}
}
TCP协议建立连接
TCP协议发送数据的步骤:
- 1.创建客户端的Socket对象
这一步一旦成功就证明连接成功- 2.获取通道中的输出流对象,写数据
- 3.释放资源
客户端代码
public class ClientDemo1 {
public static void main(String[] args) throws Exception {
//此构造方法较为繁琐,一般不用
//Socket(InetAddress address, int port)
//创建流套接字并将其连接到指定IP地址的指定端口号。
//Socket socket = new Socket(InetAddress.getByName("192.168.10.147\""),10086);
//Socket(String host, int port)
//创建流套接字并将其连接到指定的主机上的指定端口
Socket socket = new Socket("192.168.10.147", 10086);
//获取通道中的输出流对象,写数据
while(true){
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
if("886".equals(next)){
break;
}else{
OutputStream outputStream = socket.getOutputStream();
outputStream.write(next.getBytes());
}
}
//释放资源
socket.close();
}
}
服务器端代码:
package com.bigdat.java.day28.tcpcoding;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*
TCP协议服务器端接收数据的步骤:
1.创建服务器的 Socket 对象,
2.监听客户端的连接,返回一个 Socket 对象,
这个 Socket 对象封装的客户端的ip信息
3.获取通道中的输入流对象,读取数据并显示在控制台上
4.释放资源(一般情况下,服务器一旦启动就很少关闭)
*/
public class ServerDemo1 {
public static void main(String[] args) {
//1.创建服务器端的Socket对象
//将线程绑定到计算机的上的一个端口号上
ServerSocket serverSocket = null;
System.out.println("===================客户端发送的消息====================");
try {
serverSocket = new ServerSocket(10086);
} catch (IOException e) {
e.printStackTrace();
}
//监听客户端的连接,返回一个Socket对象,这个Socket对象封装
//了客户端的IP等一些信息
// Socket accept()
//侦听到要连接的套接字并接收它
Socket accept = null;
try {
accept = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
while(true){
InputStream inputStream = null;
try {
inputStream = accept.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = new byte[1024];
int len = 0;
try {
len = inputStream.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
String s = new String(bytes, 0, len);
//获取客户端的名称和地址
InetAddress inetAddress = accept.getInetAddress();
String hostAddress = inetAddress.getHostAddress();
String hostName = inetAddress.getHostName();
System.out.println(hostName+",IP地址为:"+hostAddress+"给你发送的信息为:"+s);
}
}
}
改进(客户端发送数据后,服务器端有返回值)
客户端
package com.bigdat.java.day28.tcpcoding;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class ClientDemo2 {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("192.168.10.147",19999 );
} catch (IOException e) {
e.printStackTrace();
}
while(true){
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
if("886".equals(next)){
System.out.println("连接已断开...");
break;
}else{
OutputStream outputStream = null;
InputStream inputStream = null;
try {
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.write(next.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = new byte[1024];
int length = 0;
try {
length = inputStream.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
String s = new String(bytes, 0, length);
System.out.println("服务器返回的消息是:"+s);
}
}
}
}
服务器端
package com.bigdat.java.day28.tcpcoding;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo2 {
public static void main(String[] args) {
ServerSocket sSocket = null;
try {
sSocket = new ServerSocket(19999);
} catch (IOException e) {
e.printStackTrace();
}
Socket accept = null;
try {
accept = sSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
while(true){
OutputStream outputStream = null;
InputStream inputStream = null;
try {
outputStream = accept.getOutputStream();
inputStream = accept.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = new byte[1024];
int len =0;
try {
len = inputStream.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
String s = new String(bytes, 0, len);
InetAddress inetAddress = accept.getInetAddress();
String hostName = inetAddress.getHostName();
String hostAddress = inetAddress.getHostAddress();
System.out.println(hostName+",IP地址为:"+hostAddress+"客户端上传的信息是:"+s);
try {
outputStream.write("服务器端已经接收到!!!".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}