1.TCP通信
TcpClient
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args){
try {
//1.要知道服务器的地址,端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port =9999;
//2.创建一个socket连接
Socket socket = new Socket(serverIP,port);
//3.发送消息 IO
OutputStream os = socket.getOutputStream();
// os.write("你好,欢迎学习狂神说java".getBytes());
//3.读取文件
FileInputStream fis = new FileInputStream(new File("a.png"));
//4.写出文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//通知服务器,我已经结束了
//我已经传输完了!
socket.shutdownOutput();
//确定服务器接送完毕,才能够断开连接
InputStream inputStream = socket.getInputStream();
//String byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[2014];
int len2;
while ((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
baos.close();
inputStream.close();
os.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
TcpServer
import javax.xml.ws.Service;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args){
try {
//1.我的有一个地址
ServerSocket serverSocket = new ServerSocket(9999);
//2.等待客服端连接过来
Socket sock = serverSocket.accept();
//3.读取客服端的消息
InputStream is = sock.getInputStream();
// //管道流
// ByteArrayOutputStream bassos = new ByteArrayOutputStream();
// byte[] buffer = new byte[1024];
// int len;
// while ((len=is.read(buffer))!=-1){
// bassos.write(buffer,0,len);
// }
// System.out.println(bassos.toString());
// bassos.close();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("revice.png"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//通知客服端我接受完毕了
OutputStream os = sock.getOutputStream();
os.write("我接受完毕了,你可以断开了".getBytes());
fos.close();
is.close();
sock.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
}
}
}
2.UDP通信
UdpClient
import java.io.InputStream;
import java.net.*;
//不需要连接服务器
public class UdpClient {
public static void main(String args []) throws Exception{
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建立一个包
String msg ="你好啊,服务器";
//发送给谁
InetAddress locahost = InetAddress.getByName("localhost");
int port = 9090;
//数据,数据的长度起始,要发个谁
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,locahost,port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
UdpServer
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServer {
public static void main(String args []) throws Exception{
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);//阻赛接收
String data = new String(packet.getData(), 0, packet.getData().length);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(data);
socket.close();
}
}
3.使用URL下载网上资源
先找资源
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class URLdown {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("https://m701.music.126.net/20210310132553/78b97fa4d84601a1831542ef6aa3db66/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/7813139243/057c/8e68/f262/d25ca515923d07649b47e51e9da092db.m4a");
//2.忽略HTTPS请求的SSL证书
if("https".equalsIgnoreCase(url.getProtocol())){
//忽略HTTPS请求的SSL证书
SslUtils.ignoreSsl();
}
//3.连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("a.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fileOutputStream.write(buffer,0,len);//写出这个数据
}
fileOutputStream.close();
inputStream.close();
urlConnection.disconnect();//断开连接
// System.out.println(url.getProtocol());//协议
// System.out.println(url.getHost());//主机ip
// System.out.println(url.getPort());//端口
// System.out.println(url.getFile());//获得文件
// System.out.println(url.getPath());//全路径
// System.out.println(url.getQuery());//参数
}
}