1.TCP网络编程
1.客户端发送内容给服务器服务器将内容打印到控制台上。
package top.oneluckyguy.socket;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Liu Qingfeng
* @create 2020-12-21----1:23
* 实现:
* 客户端发送内容给服务器服务器将内容打印到控制台上。
*/
public class TCPTest {
public static void main(String[] args) {
}
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
try {
//1.创建Socket对象,指明服务器端的ip和端口号
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress,8899);
//2.获取一个输出流用于输出数据
outputStream = socket.getOutputStream();
outputStream.write("你好,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//3.关闭流
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
ServerSocket serverSocket = null;
try {
//1.指明自己的端口号
serverSocket = new ServerSocket(8899);
//2.调用accept()表示接受来自客户端的socket
socket = serverSocket.accept();
//3.获取输入流
inputStream = socket.getInputStream();
// 不建议这样写字节流和中文可能会乱码
// byte[] buffer = new byte[1024];
// int len;
// while((len = inputStream.read()) != -1){
// String str = new String(buffer,0,len );
// System.out.println(str);
// }
//4.读取输入流数据
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
//read()函数含参
while((len = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer,0,len);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流
if (byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.TCP Socket编程实现文件的传输
package top.oneluckyguy.socket;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Liu Qingfeng
* @create 2020-12-21----2:07
* TCP Socket编程
* 从客户端发送文件给服务端,服务端保存到本地
*/
public class TCPFileTest {
public static void main(String[] args) {
}
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
try {
//1.创建Socket类的对象
socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);
//2.创建输出流
outputStream = socket.getOutputStream();
//3.创建输入流
fileInputStream = new FileInputStream(new File("2.png"));
//4.读取并输出过程
byte[] buffer = new byte[1024];
int len;
while((len = fileInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
//1.造一个ServerSocket对象
serverSocket = new ServerSocket(8899);
//2.获取客户端socket
socket = serverSocket.accept();
//3.获取输入端输入流
inputStream = socket.getInputStream();
//4.建立本地的存储文件
fileOutputStream = new FileOutputStream(new File("11.png"));
//5.获取字节过程
byte[] buffer = new byte[1024];
int len;
while((len = inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//6.关闭流
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.在2的基础上服务器反馈给客户端信息
package top.oneluckyguy.socket;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Liu Qingfeng
* @create 2020-12-21----2:07
* TCP Socket编程
* 从客户端发送文件给服务端,服务端保存到本地,服务器反馈给客户端信息。
* socket.shutdownOutput();函数用来关闭数据的输出,
* 如果不加强制关闭函数,服务器和客户端会不知道什么时候停止输出数据,会一直处于while循环中,可以发送文件接受文件成功,但是服务器
* 到客户端的反馈信息不会执行(因为反馈信息在while循环的后面,)
*
*/
public class TCPFeedback {
public static void main(String[] args) {
}
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1.创建Socket类的对象
socket = new Socket(InetAddress.getByName("127.0.0.1"),55599);
//2.创建输出流
outputStream = socket.getOutputStream();
//3.创建输入流
fileInputStream = new FileInputStream(new File("2.png"));
//4.读取并输出过程
byte[] buffer = new byte[1024];
int len;
while((len = fileInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
//关闭数据的输出
socket.shutdownOutput();
//4.4 接受来自服务器端的反馈,并显示到控制台。
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
Byte[] bytes = new Byte[5];
int length;
while((length = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer,0,length);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
OutputStream outputStream = null;
try {
//1.造一个ServerSocket对象
serverSocket = new ServerSocket(55599);
//2.获取客户端socket
socket = serverSocket.accept();
//3.获取输入端输入流
inputStream = socket.getInputStream();
//4.建立本地的存储文件
fileOutputStream = new FileOutputStream(new File("12.png"));
//5.获取字节过程
byte[] buffer = new byte[1024];
int len;
while((len = inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
//5.5 服务器端给与客户端反馈
outputStream = socket.getOutputStream();
outputStream.write("服务器给客户端的反馈".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//6.关闭流
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.UDP
udp实现发送端发送数据,接收端控制台显示接受数据
package top.oneluckyguy.socket;
import org.junit.Test;
import javax.swing.plaf.basic.BasicButtonUI;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author Liu Qingfeng
* @create 2020-12-21----14:06
* UDP协议的网络编程
*/
public class UDPTest1 {
//发送端
@Test
public void sender(){
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket();
byte[] data = "UDP 发送".getBytes();
InetAddress inetAddress = InetAddress.getLocalHost();
DatagramPacket datagramPacket = new DatagramPacket(data,0,data.length,inetAddress,8787);
datagramSocket.send(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (datagramSocket != null){
datagramSocket.close();
}
}
}
//接收端
@Test
public void receiver(){
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket(8787);
byte[] bytes = new byte[100];
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String (datagramPacket.getData(),0,datagramPacket.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (datagramSocket != null){
datagramSocket.close();
}
}
}
}
3.URL网络编程
一些方法:
package top.oneluckyguy.socket;
import java.net.MalformedURLException;
import java.net.URL;
/**
* URL网络编程
* 1.URL:统一资源定位符,对应着互联网的某一资源地址
* 2.格式:
* http://localhost:8080/examples/beauty.jpg?username=Tom
* 协议 主机名 端口号 资源地址 参数列表
*
*/
public class URLTest {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
// public String getProtocol( ) 获取该URL的协议名
System.out.println(url.getProtocol());
// public String getHost( ) 获取该URL的主机名
System.out.println(url.getHost());
// public String getPort( ) 获取该URL的端口号
System.out.println(url.getPort());
// public String getPath( ) 获取该URL的文件路径
System.out.println(url.getPath());
// public String getFile( ) 获取该URL的文件名
System.out.println(url.getFile());
// public String getQuery( ) 获取该URL的查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
实现url下载资源,要启动tomcat
package top.oneluckyguy.socket;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLTest1 {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("day10\\beauty3.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlConnection != null){
urlConnection.disconnect();
}
}
}
}