TCP
server端:
package com.xxliao.tcp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author xxliao
* @description: TCP 服务端
* @date 2023/11/19$ 12:47$
*/
public class TcpServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1. 开放服务器端口,创建ServerSocket
serverSocket = new ServerSocket(8899);
//2. 等待客户端的连接
accept = serverSocket.accept();
//3. 读入客户端的消息,
is = accept.getInputStream();
/*
回忆之前的IO流方案,弊端:存在中文,可能存在乱码。
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
String str = new String(buffer,0,len);
System.out.println(str);
}
**/
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
System.out.println(
"数据来源地址:"+accept.getInetAddress().getHostName());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4. 关闭资源
try {
if (baos!=null){
baos.close();
}
if (is!=null){
is.close();
}
if (accept!=null){
accept.close();
}
if (serverSocket!=null){
serverSocket.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
client端:
package com.xxliao.tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author xxliao
* @description: TCP演示
* @date 2023/11/19$ 12:45$
*/
public class TcpClient {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1. 连接服务器的地址
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 8899;
//2. 创建一个Socket
socket = new Socket(serverIP,port);
//3. 创建一个输出流,向外写东西
os = socket.getOutputStream();
os.write("hello,tcp...".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//4. 关闭资源
try {
if (os!=null){
os.close();
}
if (socket!=null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
UDP
udp接受端:
package com.xxliao.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @author xxliao
* @description: UdpReceiver
* @date 2023/11/19$ 12:54$
*/
public class UdpReceiver {
public static void main(String[] args) throws Exception{
//1. 建立DatagramSocket,开放端口
DatagramSocket socket = new DatagramSocket(9090);
//2. 接收数据
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
//3. 输出数据
// packet.getData() : 获取packet中的数据
System.out.println(new String(packet.getData(), 0,
packet.getLength()));
//4. 关闭socket
socket.close();
}
}
udp发送端:
package com.xxliao.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @author xxliao
* @description: UdpSender
* @date 2023/11/19$ 12:53$
*/
public class UdpSender {
public static void main(String[] args) throws Exception {
//1. 建立DatagramSocket
DatagramSocket socket = new DatagramSocket();
//2. 封装数据包
String msg = "UDPSender==>";
byte[] data = msg.getBytes();
InetAddress inet = InetAddress.getByName("127.0.0.1");
int port = 9090;
DatagramPacket packet = new
DatagramPacket(data,0,data.length,inet,port);
//3. 通过 Socket 发送 packet
socket.send(packet);
//4. 关闭socket
socket.close();
}
}
URL
举例1:
package com.xxliao.url;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author xxliao
* @description: url demo one
* @date 2023/11/19$ 13:01$
*/
public class URLDemo {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/helloworld/index.jsp? username=kuangshen&password=123");
System.out.println(url.getProtocol()); //获取URL的协议名
System.out.println(url.getHost()); //获取URL的主机名
System.out.println(url.getPort()); //获取URL的端口号
System.out.println(url.getPath()); //获取URL的文件路径
System.out.println(url.getFile()); //获取URL的文件名
System.out.println(url.getQuery()); //获取URL的查询名
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
举例2:
package com.xxliao.url;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author xxliao
* @description: url demo two
* @date 2023/11/19$ 13:02$
*/
public class URLDemo02 {
public static void main(String[] args) {
try {
//1. 定位到服务器端的资源
URL url = new
URL("http://localhost:8080/helloworld/xxliao.jpg");
//2. 创建连接
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
//3. 获取输入流
InputStream is = connection.getInputStream();
//4. 写出文件
FileOutputStream fos = new FileOutputStream("xxliao2.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//关闭资源
fos.close();
is.close();
connection.disconnect(); //断开连接
} catch (Exception e) {
e.printStackTrace();
}
}
}