1 本章重点
本章重点有:
1 网络传输三要素
2Tcp和udp区别
3tcp三次握手
4 tcp和udp 传输文件
2 本章学习注意
会用到多线程,字符串API,IO流等知识,前面的学不会,后面的也难学
3 udp传输
udp传输服务器先后开无所谓
1 InetAddress
public class InterAdderess {
public static void main(String[] args) throws UnknownHostException {
//封装了域名的基本信息
InetAddress b1 = InetAddress.getByName("www.acfun.cn");
System.out.println(b1);
System.out.println("----------------------");
String hostAddress = b1.getHostAddress();
String name = b1.getHostName();
//获得ip地址
System.out.println(hostAddress);
//获得网址名字-主机名
System.out.println(name);
}
}
2 udp传输基本
发送端
public class Send {
public static void main(String[] args) throws Exception {
//创建发送用对象
DatagramSocket ds=new DatagramSocket();
//创建包裹信息
//DatagramPacket(byte buf[], int length, InetAddress address, int port)
byte[] bytes = "得国之正者惟汉与明".getBytes();
int length = bytes.length;
InetAddress ip = InetAddress.getByName("192.168.0.192");
int port=10088;
DatagramPacket dp=new DatagramPacket(bytes,length,ip,port);
//发送
ds.send(dp);
//关闭
ds.close();
}
}
接受端
public class ReceiveDemo {
public static void main(String[] args) throws Exception {
//创建socket对象
DatagramSocket ds=new DatagramSocket(10088);
//创建接受容器
byte[] b1=new byte[1048576];
int length = b1.length;
DatagramPacket dp=new DatagramPacket(b1,length);
//存储接收器对象
ds.receive(dp);
//解析数据包
//获得缓冲区
byte[] data = dp.getData();
//获得长度
int length1 = dp.getLength();
//获得解析后数据
String s=new String(data,0,length1);
//获得ip
InetAddress address = dp.getAddress();
String ip=address.getHostAddress();
System.out.println(ip+":"+s);
//释放资源
ds.close();
}
}
3 UDP键盘录入接受数据
客户端
public class Send {
public static void main(String[] args) throws Exception {
//创建发送用对象
DatagramSocket ds=new DatagramSocket();
//创建包裹信息
//DatagramPacket(byte buf[], int length, InetAddress address, int port)
Scanner sc=new Scanner(System.in);
String s=null;
while (true){
s=sc.next();
while (s.equals("886")){
//关闭
ds.close();
System.exit(0);
}
byte[] bytes = s.getBytes();
int length = bytes.length;
InetAddress ip = InetAddress.getByName("192.168.0.192");
int port=10088;
DatagramPacket dp=new DatagramPacket(bytes,length,ip,port);
//发送
ds.send(dp);
}
}
}
接收端
public class ReceiveDemo {
public static void main(String[] args) throws Exception {
//创建socket对象
DatagramSocket ds=new DatagramSocket(10088);
while (true){
//创建接受容器
byte[] b1=new byte[1048576];
int length = b1.length;
DatagramPacket dp=new DatagramPacket(b1,length);
//存储接收器对象
ds.receive(dp);
//解析数据包
//获得缓冲区
byte[] data = dp.getData();
//获得长度
int length1 = dp.getLength();
//获得解析后数据
String s=new String(data,0,length1);
//获得ip
InetAddress address = dp.getAddress();
String ip=address.getHostAddress();
System.out.println(ip+":"+s);
// //释放资源
// ds.close();
}
}
}
5 多线程开启UDP
主客户端
public class ChatRoom {
public static void main(String[] args) throws Exception {
DatagramSocket sendDs=new DatagramSocket();
DatagramSocket receiveDs=new DatagramSocket(10087);
SendThread s1=new SendThread(sendDs,10087);
ReceiveThread r1=new ReceiveThread(receiveDs);
Thread t1=new Thread(s1);
Thread t2=new Thread(r1);
t2.start();
t1.start();
}
}
发送
public class SendThread implements Runnable {
private DatagramSocket ds;
private int port;
public SendThread( DatagramSocket send,int port) {
this.ds=send;
this.port=port;
}
@Override
public void run() {
Scanner sc=new Scanner(System.in);
String s=null;
while (true){
s=sc.next();
while (s.equals("886")){
//关闭
ds.close();
System.exit(0);
}
byte[] bytes = s.getBytes();
int length = bytes.length;
InetAddress ip = null;
try {
ip = InetAddress.getByName("192.168.0.192");
DatagramPacket dp=new DatagramPacket(bytes,length,ip,port);
//发送
ds.send(dp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
接收
public class ReceiveThread implements Runnable {
DatagramSocket rs;
public ReceiveThread(DatagramSocket rs) {
this.rs = rs;
}
@Override
public void run() {
try {
while (true) {
//创建接受容器
byte[] b1 = new byte[1048576];
DatagramPacket dp = new DatagramPacket(b1, b1.length);
//存储接收器对象
rs.receive(dp);
//解析数据包
//获得缓冲区
//获得解析后数据
String s = new String(dp.getData(), 0, dp.getLength());
//获得ip
String ip = dp.getAddress().getHostAddress();
System.out.println(ip + ":" + s);
// //释放资源
// ds.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
4 TCP传输
tcp传输必须先开服务器
1 基本传输
客户端
/**
* @author 花溪
* @create 2021-07-15 13:46
* 1 写socket对象
* 2 输出流数据
* 3 释放
*/
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.0.192", 10876);
OutputStream outputStream = s.getOutputStream();
outputStream.write("法国元帅贝当爷爷".getBytes());
s.close();
}
}
服务器
/**
* @author 花溪
* @create 2021-07-15 13:53
* 1 创建服务器socket
* 2 监听
* 3 读
* 4 释放
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(10876);
Socket ss = s.accept();
//获取ip和内容
String ip = ss.getInetAddress().getHostAddress();
InputStream inputStream = ss.getInputStream();
byte[] b=new byte[1024];
int len=inputStream.read(b);
String s1= new String(b,0,len);
System.out.println(ip+":"+s1);
ss.close();
}
}
2 键盘传输
客户端
/**
* @author 花溪
* @create 2021-07-15 19:57
*/
public class Client {
public static void main(String[] args) throws IOException {
Socket s=new Socket("192.168.0.192",19999);
Scanner sc=new Scanner(System.in);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String ss=null;
while (true){
System.out.println("聊点什么?");
String line=sc.nextLine();
if(line.equals("886")){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
s.close();
}
}
服务器
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(19999);
Socket ss = s.accept();
BufferedReader is=new BufferedReader(new InputStreamReader(ss.getInputStream()));
String line=null;
while ((line=is.readLine())!=null){
System.out.println(ss.getInetAddress().getHostAddress()+":"+line);
}
ss.close();
}
}
3 提供回复信息
/**
* @author 花溪
* @create 2021-07-15 19:39
* 服务器给反馈
*/
public class TcpServer {
public static void main(String[] args) throws IOException {
//创建Socket对象
ServerSocket s=new ServerSocket(10076);
//监听对象
Socket ss = s.accept();
//读
InputStream is = ss.getInputStream();
byte[] bytes=new byte[1024];
int len=is.read(bytes);
String server=new String(bytes,0,len);
String ip = ss.getInetAddress().getHostAddress();
System.out.println(ip+":"+server);
//回复数据
OutputStream outputStream = ss.getOutputStream();
outputStream.write("你们是虫子".getBytes());
//关闭
ss.close();
}
}
客户端
public class Client {
public static void main(String[] args) throws IOException {
//获取socket
Socket s=new Socket("192.168.0.192",10076);
//获取输出流
OutputStream outputStream = s.getOutputStream();
outputStream.write("烦死啦".getBytes());
//获取输入流
InputStream inputStream = s.getInputStream();
byte[] bytes=new byte[1024];
int read = inputStream.read(bytes);
String s1=new String(bytes,0,read);
System.out.println(s1);
//释放
s.close();
}
}
4 上传图片
客户端
public class UploadClient {
public static void main(String[] args) throws IOException {
Socket s=new Socket("192.168.0.192",11111);
// 封装图片
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\香缇.png"));
//封装流
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
//把数据读入流中
byte[] bys = new byte[1024];
int len=0;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
bos.flush();
}
// 在客户端执行shutdownOutput方法用来表明数据发送完毕,不再发送更多数据。
s.shutdownOutput();
//接受反馈
InputStream inputStream = s.getInputStream();
byte[] bytes=new byte[1024];
int len2=inputStream.read(bytes);
String client = new String(bytes,0,len2);
System.out.println(client);
bis.close();
s.close();
}
}
服务器
public class UploadServer {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(11111);
Socket sss= s.accept();
BufferedInputStream bs=new BufferedInputStream(sss.getInputStream());
BufferedOutputStream bf=new BufferedOutputStream(new FileOutputStream("E:\\mmm.png"));
byte[] bys=new byte[1024];
int len;
while ((len=bs.read(bys))!=-1){
bf.write(bys,0,len);
bf.flush();
}
//反馈
OutputStream outputStream = sss.getOutputStream();
outputStream.write("上传图片成功了!".getBytes());
outputStream.close();
sss.close();
}
}
5 包装流上传文本
服务器
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(9999);
Socket ss=s.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(ss.getInputStream()));
BufferedWriter bw=new BufferedWriter(new FileWriter("e:\\a.txt"));
String line=null;
while ((line=br.readLine())!=null){
System.out.println("读取数据");
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
ss.close();
}
}
客户端
public class Clinet {
public static void main(String[] args) throws IOException {
File file = new File("E:\\1852铁血中华.txt");
Socket s=new Socket("192.168.0.192",9999);
BufferedReader br=new BufferedReader(new FileReader(file));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
s.close();
}
}