目录
2.3 在上一个例子进行更改 客户端向服务器端传输图片,服务器端将图片保存在本地 并返回收到数据成功消息给客户端
1.简介:
1.1基本概述:
1.2 IP和端口号的介绍:
1.3具体操作:
/*
测试InetAddress的使用,以及InetAddress的构造器: getByName() 和 getLocalHost() 的使用;
以及InetAddress的2个常用方法:getHostName() 和 getHostAddress() 的使用;
*/
import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
@Test
public void test1() throws UnknownHostException {
InetAddress inet1 = InetAddress.getByName("127.0.0.1");
System.out.println(inet1); // /127.0.0.1
InetAddress inet2 = InetAddress.getByName("www.baidu.com");
System.out.println(inet2); // www.baidu.com/183.232.231.174
InetAddress inet3 = InetAddress.getLocalHost();
System.out.println(inet3); // FYFPJ4VMEAQU9JM/192.168.112.1
System.out.println();
System.out.println(inet1.getHostName()); // 127.0.0.1
System.out.println(inet2.getHostName()); // www.baidu.com
System.out.println(inet3.getHostName()); // FYFPJ4VMEAQU9JM
System.out.println();
System.out.println(inet1.getHostAddress()); // 127.0.0.1
System.out.println(inet2.getHostAddress()); // 183.232.231.174
System.out.println(inet3.getHostAddress()); // 192.168.112.1
}
}
1.4端口号基本介绍:
1.5 TCP与UDP:
2. TCP:
2.1.使用客户端向服务器端发送数据,服务器端输出到控制台
具体操作:此处为了简洁使用throws抛出异常,正常开发应使用try-catch
package MyTCP;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*
实现TCP网络编程一:
客户端发送数据给服务端,服务端接受数据并输出到控制台;
*/
public class MyTcpTest01 {
//客户端
@Test
public void client() throws IOException {
//1.创建Socket对象,指明服务器端的IP地址和端口号
InetAddress iner = InetAddress.getByName("127.0.0.1");
Socket socket = new Socket(iner,8111);
//2.获取一个输出流,用于输出数据
OutputStream os = socket.getOutputStream();
//3.写出数据的操作
os.write("你好,我是客户端的一条消息;".getBytes());
//4.关闭相关的流
os.close();
socket.close();
}
//服务器端
@Test
public void serve() throws IOException {
//1.创建服务器端的ServerSocket对象,指明自己的端口号
ServerSocket ss = new ServerSocket(8111);
//2.调用accept()表明接收客户端的socket
Socket accept = ss.accept();
//3.获取输入流
InputStream is = accept.getInputStream();
//4.读取输入流中的数据
int len;
byte[] a = new byte[20];
/*
ByteArrayOutputStream操作为:先暂存数据在其对象中,可以防止汉字乱码
*/
ByteArrayOutputStream bis = new ByteArrayOutputStream();
while((len=is.read(a))!=-1){
bis.write(a,0,len);
}
System.out.println(bis.toString());
System.out.println("收到来自于:"+accept.getInetAddress().getHostName()+"的数据");
//5.关闭相关流
bis.close();
is.close();
accept.close();
ss.close();
}
}
ByteArrayOutputStream的使用:声明不必指明文件目录;
新建一个数组可以暂时缓存在其对象中,然后再调用toString()方法输出或转换成其他形式;
2.2.客户端向服务器端传输图片,服务器端将图片保存在本地
package MyTCP;
/*
客户端向服务器端传输图片,服务器端将图片保存在本地
*/
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MyTCPTest02 {
//客户端
@Test
public void client() throws IOException {
//1.造Socket对象,指明服务器IP和端口号
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8111);
//2.获取一个输出流,用于输出数据
OutputStream os = socket.getOutputStream();
//3.创建目标文件的输出流,用于输出
FileInputStream fis = new FileInputStream("photo.jpg");
//4.输出文件操作
int len;
byte[] a = new byte[1024];
while((len=fis.read(a))!=-1){
os.write(a,0,len);
}
//5.关闭相关流
fis.close();
os.close();
socket.close();
}
//服务器端
@Test
public void serve() throws IOException {
//1.创建服务器端的ServerSocket对象,指明自己的端口号
ServerSocket ss = new ServerSocket(8111);
//2.调用accept()表明接收客户端的socket
Socket socket = ss.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//4.将读入数据保存在本地
FileOutputStream fos = new FileOutputStream("photo1.jpg");
int len;
byte[] a = new byte[1024];
while((len=is.read(a))!=-1){
fos.write(a,0,len);
}
System.out.println("完成本地下载;");
//5.关闭相关流
fos.close();
is.close();
socket.close();
ss.close();
}
}
2.3 在上一个例子进行更改 客户端向服务器端传输图片,服务器端将图片保存在本地 并返回收到数据成功消息给客户端
package MyTCP;
/*
在上一个例子进行更改
客户端向服务器端传输图片,服务器端将图片保存在本地
并返回收到数据成功消息给客户端
*/
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MyTCPTest03 {
//客户端
@Test
public void client() throws IOException {
//1.造Socket对象,指明服务器IP和端口号
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8111);
//2.获取一个输出流,用于输出数据
OutputStream os = socket.getOutputStream();
//3.创建目标文件的输出流,用于输出
FileInputStream fis = new FileInputStream("photo.jpg");
//4.输出文件操作
int len;
byte[] a = new byte[1024];
while((len=fis.read(a))!=-1){
os.write(a,0,len);
}
//5.关闭数据的输出;
socket.shutdownOutput();
//6,.接受来自服务器端的数据,并输出到控制台上
InputStream is = socket.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len1;
byte[] b = new byte[20];
while((len1=is.read(b))!=-1){
bos.write(b,0,len1);
}
System.out.println(bos.toString());
//7 .关闭相关流
is.close();
bos.close();
fis.close();
os.close();
socket.close();
}
//服务器端
@Test
public void serve() throws IOException {
//1.创建服务器端的ServerSocket对象,指明自己的端口号
ServerSocket ss = new ServerSocket(8111);
//2.调用accept()表明接收客户端的socket
Socket socket = ss.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//4.将读入数据保存在本地
FileOutputStream fos = new FileOutputStream("photo2.jpg");
int len;
byte[] a = new byte[1024];
while((len=is.read(a))!=-1){
fos.write(a,0,len);
}
System.out.println("完成本地下载;");
//5.服务器给与客户端反馈
OutputStream os1 = socket.getOutputStream();
os1.write("数据已接受成功;".getBytes());
//6.关闭相关流
os1.close();
fos.close();
is.close();
socket.close();
ss.close();
}
}
3.UDP:
3.1简介:
3.2具体操作:
package MyUDP;
import org.junit.Test;
import java.io.IOException;
import java.net.*;
public class MyUDPTest01 {
//发送方:
@Test
public void sender() throws IOException {
//1.建立DatagramSocket对象,此时不需要指明端口号
DatagramSocket socket = new DatagramSocket();
//2.确认发送数据,包装在DatagramPacket中,并指明要发送的端口号;
String string = "我是发送方的消息;";
byte[] a = string.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(a,0,a.length,inet,8111);
//3.发送数据包
socket.send(packet);
System.out.println("数据包发送成功");
//4.关闭流
socket.close();
}
//接受方:
@Test
public void receiver() throws IOException {
//1.新建DatagramSocket,指明端口号
DatagramSocket socket = new DatagramSocket(8111);
//2.确定数据要存储的地方
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
//3.接受数据包
socket.receive(packet);
//4.输出接受的数据包的内容
System.out.println(new String(packet.getData(), 0, packet.getLength()));
//5.关闭流
socket.close();
}
}
4.URL编程
常用方法:
具体操作:
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MyURLTest {
@Test
public void test01() throws IOException {
URL url = new URL("https://www.bizhishe.com/d/file/2021-01-28/b6d8189c82ffa2c89bab39f574650b0c.jpg");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("1.jpg");
byte[] data = new byte[1024];
int len;
while((len=is.read(data))!=-1){
fos.write(data,0,len);
}
//关闭资源
fos.close();
is.close();
urlConnection.disconnect();
}
}