文章目录
- 1. InetAddress
- 2. UDP
- 3. TCP
- 4. 总结
1. InetAddress
InetAddress的常用方法如下:
public class InetAddressDemo {
public static void main(String[] args) throws Exception{
// 1. 获取本机IP地址对象
InetAddress ip1 = InetAddress.getLocalHost();
System.out.println(ip1.getHostName());
System.out.println(ip1.getHostAddress());
// 2. 获取指定IP或者域名的IP地址对象
InetAddress ip2 = InetAddress.getByName("www.baidu.com");
System.out.println(ip2.getHostName());
System.out.println(ip2.getHostAddress());
// 3. 与对方地址是否可以联通
System.out.println(ip2.isReachable(6));
}
}
2. UDP
客户端
package Socket.UDP;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception{
// 1. 创建客户端对象
DatagramSocket socket = new DatagramSocket();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入发送的数据,输入exit退出:");
String message = sc.nextLine();
if(message.equals("exit"))
break;
byte[] data = message.getBytes();
// 2. 创建数据包对象封装要发出去的数据
/**
* 参数一:封装要发出去的数据
* 参数二:长度
* 参数三:服务端IP地址
* 参数四:端口号
*/
DatagramPacket packet = new DatagramPacket(data, data.length,
InetAddress.getLocalHost(), 6666);
// 3. 开始发送数据
socket.send(packet);
}
// 4. 释放资源
// System.out.println("客户端数据发送完毕!");
socket.close();
}
}
服务端
package Socket.UDP;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Server {
public static void main(String[] args) throws Exception{
// 1. 创建一个服务端对象
DatagramSocket socket = new DatagramSocket(6666);
// 2. 创建一个数据包对象,用于接收数据对象
while (true) {
byte[] buf = new byte[1024]; // 1KB
DatagramPacket packet = new DatagramPacket(buf, buf.length);
// 3. 开始正式使用数据包来接收客户端发来的数据
socket.receive(packet);
// 4. 打印数据
String res = new String(buf, 0, packet.getLength());
System.out.println(res);
}
}
}
3. TCP
客户端
服务端
实现一个多人聊天室
这个小demo当时大二的时候自己写,用C++写了400多行代码,没想到java总共就100多行,哈哈哈~一是感叹java封装的好,而是觉得,当时觉得特别难特别自豪的一个项目,现在看来只是入门的一个小demo,不过心里更多是感慨这一两年内的成长变化太大了吧。
ServerReadThread
package Socket.TCP;
import java.io.*;
import java.net.Socket;
public class ServerReadThread extends Thread{
private Socket socket;
private String name;
public ServerReadThread(Socket t) {
socket = t;
name = Thread.currentThread().getName();
}
@Override
public void run() {
try {
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
while(true) {
try {
String msg = dis.readUTF();
System.out.println(name + ": " + msg);
sendMessage2All(msg);
} catch (IOException e) {
System.out.println(socket.getLocalAddress() + "下线了");
Server.onLineSockets.remove(socket);
dis.close();
socket.close();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMessage2All(String msg) throws Exception {
for (Socket onLineSocket : Server.onLineSockets) {
OutputStream os = onLineSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(msg);
dos.flush();
}
}
}
服务端代码
package Socket.TCP;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static List<Socket> onLineSockets = new ArrayList<>();
public static void main(String[] args) throws Exception{
// 1. 创建对象
ServerSocket ss = new ServerSocket(8888);
System.out.println("服务端启动成功");
while(true) {
Socket socket = ss.accept();
onLineSockets.add(socket);
new ServerReadThread(socket).start();
System.out.println("Hello, " + socket.getLocalAddress());
}
}
}
ClientReader
package Socket.TCP;
import java.io.*;
import java.net.Socket;
public class ClientReader extends Thread{
private Socket socket;
private String name;
public ClientReader(Socket s) {
socket = s;
name = Thread.currentThread().getName();
}
@Override
public void run() {
try {
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
while(true) {
try {
String msg = dis.readUTF();
System.out.println(name + ": " + msg);
sendMessage2All(msg);
} catch (IOException e) {
System.out.println(socket.getLocalAddress() + "下线了");
dis.close();
socket.close();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMessage2All(String msg) throws Exception {
for (Socket onLineSocket : Server.onLineSockets) {
OutputStream os = onLineSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(msg);
dos.flush();
}
}
}
客户端代码
package Socket.TCP;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception{
// 1. 创建Socket对象,并同时请求与服务端程序的连接
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
Scanner sc = new Scanner(System.in);
// 创建线程负责收消息
new ClientReader(socket).start();
while(true) {
System.out.println("请发送消息:");
String msg = sc.nextLine();
if("exit".equals(msg)) {
System.out.println("bye~");
break;
}
dos.writeUTF(msg);
dos.flush();
}
dos.close();
socket.close();
}
}
4. 总结
到这,Java基本的一些东西就学的差不多了,后面还剩一些Java高级,比如反射注解这些,再学一下吧。目前只能说快速拉通了一遍API,但是熟悉程度肯定没有我用了3年的C++熟悉的,后面还是要多用才行,有机会做个小项目吧。还有就是各种数据结构,这点通过去刷力扣应该就能更熟悉了。目前对我来说,没什么压力学习起来,等过几天我会再好好整理一下我的未来的一个规划的。今天就先到这告一段落了~