限于个人能力,没有能实现服务器端的多线程。
注册、登录、上传文件到数据库的功能基本都实现了。
源代码:
https://pan.baidu.com/s/1TahTLXp2STzADhuehOMX5Q?pwd=13e5
基于UDP的Socket通信实现多线程:
/** *服务器端 */ package com.imooc.UDPTest; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Server { public static void main(String[] args) throws IOException { //创建服务器端DatagramSocket,指定端口 DatagramSocket socket=new DatagramSocket(8800); byte[] data=new byte[1024]; DatagramPacket packet=new DatagramPacket(data,data.length); System.out.println("服务器端已经启动,等待客户端发送数据"); int count=0; while (true){ socket.receive(packet);//此方法在接收到数据包之前会一直堵塞 Thread serverThread=new ServerThread(socket,packet,data); serverThread.start(); InetAddress address=packet.getAddress(); System.out.println("当前客户端IP:"+address.getHostAddress()); count++; System.out.println("当前客户端数量:"+count); } } }
/** *服务器线程处理类 */ package com.imooc.UDPTest; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class ServerThread extends Thread{ DatagramSocket socket=null; DatagramPacket packet=null; byte[] data=null; public ServerThread(DatagramSocket socket,DatagramPacket packet,byte[] data){ this.socket=socket; this.packet=packet; this.data=data; } public void run() { String info=new String(data,0,packet.getLength()); System.out.println("我是服务器,客户端说:"+info); InetAddress address=packet.getAddress(); int port=packet.getPort(); data="欢迎您".getBytes(); packet=new DatagramPacket(data,data.length,address,port); try { socket.send(packet); } catch (IOException e) { e.printStackTrace(); } } }
/** *客户端 */ package com.imooc.UDPTest; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Client { public static void main(String[] args) throws IOException { //定义服务器的地址、端口号、数据 InetAddress address=InetAddress.getByName("DESKTOP-M8K8E0E"); int port=8800; byte[] data="用户名:admin;密码:123".getBytes(); //创建数据报,包含发送的数据信息 DatagramPacket packet=new DatagramPacket(data,data.length,address,port); //创建DatagramSocket对象 DatagramSocket socket=new DatagramSocket(); socket.send(packet); byte[] data1=new byte[1024]; DatagramPacket packet1=new DatagramPacket(data1,data1.length); socket.receive(packet1); String info=new String(data1,0,packet1.getLength()); System.out.println("我是客户端,服务器说:"+info); socket.close(); } }
对象
传输对象
传递
传递文件
四大类
ip地址和端口号组成了scoket
http超文件传输协议
两台计算机要想能够进行 通信,需要满足一些必要的条件
唯一的标识:ip;用来标识自己的身份以及自己所处的地址
需要共同的语言;即协议
相应的端口号:每个主机上可以运行多个应用程序,使用端口号来决定与哪个应用进行通信
TCP/IP是目前世界上使用最为广泛的协议
TCP(Transmission Control Protocol)传输控制协议
IP:Internet Protocol 互联网协议
该套课程重点知识
UDP的两个编程主要类
socket多客户端通信
socket 通信模型和实现步骤
package com.study.socket; import javax.sound.sampled.Port; import java.io.IOException; import java.net.*; /** * UDP Client客服端 */ public class UDPClient { public static void main(String[] args) throws IOException { //1.定义消息 服务端ip地址 端口号 String msg = "用户名:sw;密码:123456"; InetAddress address = InetAddress.getByName("DESKTOP-VQBUTGQ"); int port = 8000; //2.创建数据报 byte[] data = msg.getBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, address , port ); //3.创建socket DatagramSocket socket = new DatagramSocket(); //4.发送消息 socket.send(packet); //5.接收反馈信息 byte[] data1 = new byte[1024]; DatagramPacket packet1 = new DatagramPacket(data1 , 0 , data1.length); socket.receive(packet1); String rt = new String(data1,0,packet1.getLength()); System.out.println("这是客户端,服务端反馈信息为: " + rt); //5.释放资源 socket.close(); } }
package com.study.socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; /** * UDP 服务器端 */ public class UDPServer { public static void main(String[] args) { try { //1.建立socket并制定端口号 DatagramSocket socket = new DatagramSocket(8000); int num = 0; while (true){ UDPServerThread thread = new UDPServerThread(socket); thread.setPriority(4); thread.run(); num++; System.out.println("服务器端接受了 " + num + " 个客户端请求!"); } } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package com.study.socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * UDP server 多线程实现 */ public class UDPServerThread extends Thread{ DatagramSocket socket = null; public UDPServerThread(DatagramSocket socket){ this.socket = socket; } @Override public void run() { try { byte[] data = new byte[1024]; DatagramPacket packet = new DatagramPacket(data,data.length); //3.接收客户端发送的数据信息 socket.receive(packet);//此方法在接收到数据报之前一直阻塞 //4.读取数据报信息 String msg = new String(data,0,packet.getLength()); System.out.println("UDP server服务器接收到的信息: " + msg); //5.反馈信息 String rt = "欢迎您!"; byte[] data1 = rt.getBytes(); InetAddress address = packet.getAddress(); int port = packet.getPort(); DatagramPacket packet1 = new DatagramPacket(data1, data1.length , address , port ); socket.send(packet1); } catch (IOException e) { e.printStackTrace(); } } }
package com.study.socket; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; /** * socket编程模拟客服端 */ public class Client { public static void main(String[] args) { //1.创建客户端socket Socket socket = null; try { socket = new Socket("127.0.0.1",8888); //2.获取输出流 OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream()); PrintWriter pw = new PrintWriter(osw); pw.write("用户名:polly;密码:809"); pw.flush(); //4.释放资源 pw.close(); osw.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
package com.study.socket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * socket编程模拟服务器端 */ public class Server { public static void main(String[] args) { try { //1.建立服务端socket ServerSocket serverSocket = new ServerSocket(8888); int num = 0; while(true){ //2.监听端口 Socket socket = serverSocket.accept(); ServerThread st = new ServerThread(socket); st.setPriority(4);//设置线程的优先级,范围是【1,10】 默认是5 st.start(); num++; System.out.println("服务器端接受 " + num + " 个客户端请求!"); } } catch (IOException e) { e.printStackTrace(); } } }
package com.study.socket; import java.io.*; import java.net.Socket; /** * 服务器线程实现 */ public class ServerThread extends Thread { Socket socket = null; public ServerThread(Socket socket){ this.socket = socket; } @Override public void run() { if(socket != null){ //读取输入流 InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; OutputStreamWriter osw = null; PrintWriter pw = null; try { is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); System.out.println("服务器读取客户端信息:" + socket.getLocalAddress() + " : " + socket.getLocalPort() + " " + br.readLine()); //返回响应流 osw = new OutputStreamWriter(socket.getOutputStream()); pw = new PrintWriter(osw); pw.write("请求已接到!"); pw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { //关闭资源 try { if(pw != null) pw.close(); if(osw != null) osw.close(); if(br != null) br.close(); if(isr != null) isr.close(); if(is != null) is.close(); if(socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
基于udp通信:
Socket socket=serverSocket.accept();
URL--统一资源定位符=协议名称:资源名称
java.net提供URL类表示URL
1.URL类中的常用方法:
//创建一个URL对象
URL url = new URL("http://www.baidu.com");
URL url1 = new URL(url, "/index.html?usrname=lqq#test");
//url的常用方法
System.out.println("URL主机名称:"+url1.getHost());
System.out.println("URL协议:"+url1.getProtocol());
System.out.println("URL端口:"+url1.getPort());
System.out.println("URL文件路径:"+url1.getPath());
System.out.println("URL查询字符串:"+url1.getQuery());
System.out.println("URL相对路径:"+url1.getRef());
System.out.println("URL文件名:"+url1.getFile());
2.通过URL获取html文件内容:
public static void main(String[] args) throws IOException {
URL url=new URL("http://www.snnu.edu.cn");
InputStream is =url.openStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader bufferedReader = new BufferedReader(isr);
String data=bufferedReader.readLine();
while (data!=null){
System.out.println(data);
data=bufferedReader.readLine();
}
bufferedReader.close();
isr.close();
is.close();
}
网络基础知识
地址、端口号、协议
java 提供的网络功能有四大类
InetAddress :用于标识网络上的硬件资源
URL:统一资源定位符 通过URL可以直接读取或写入网络上的数据
Sockets:使用TCP协议实现网络通信的Socket相关的类
Datagram:使用UDP协议,将数据保存在数据报中,通过网络进行通信。
通信模型啊
InetAddress.getLocalhost();获取InetAddress实例
Socket编程
1、网络基础知识
2、InetAddress类
3、URL
4、TCP编程(Socket)
5、UDP编程(Datagram)
IP、端口、协议(共同语言)
tcp/ip模型 物理层、数据链路层、网络层、传输层、应用层
端口号范围0~65535,0~1023为系统保留
Java网络功能四大类:InetAddress、URL、Socket、Datagram
多线程的知识点
Socket通信模型