-
限于個(gè)人能力,沒有能實(shí)現(xiàn)服務(wù)器端的多線程。
注冊(cè)、登錄、上傳文件到數(shù)據(jù)庫的功能基本都實(shí)現(xiàn)了。
源代碼:
https://pan.baidu.com/s/1TahTLXp2STzADhuehOMX5Q?pwd=13e5?
查看全部 -
基于UDP的Socket通信實(shí)現(xiàn)多線程:
/** *服務(wù)器端 */ 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?{ ????????//創(chuàng)建服務(wù)器端DatagramSocket,指定端口 ????????DatagramSocket?socket=new?DatagramSocket(8800); ????????byte[]?data=new?byte[1024]; ????????DatagramPacket?packet=new?DatagramPacket(data,data.length); ????????System.out.println("服務(wù)器端已經(jīng)啟動(dòng),等待客戶端發(fā)送數(shù)據(jù)"); ????????int?count=0; ????????while?(true){ ????????????socket.receive(packet);//此方法在接收到數(shù)據(jù)包之前會(huì)一直堵塞 ????????????Thread?serverThread=new?ServerThread(socket,packet,data); ????????????serverThread.start(); ????????????InetAddress?address=packet.getAddress(); ????????????System.out.println("當(dāng)前客戶端IP:"+address.getHostAddress()); ????????????count++; ????????????System.out.println("當(dāng)前客戶端數(shù)量:"+count); ????????} ????} }
/** *服務(wù)器線程處理類 */ 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("我是服務(wù)器,客戶端說:"+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?{ ????????//定義服務(wù)器的地址、端口號(hào)、數(shù)據(jù) ????????InetAddress?address=InetAddress.getByName("DESKTOP-M8K8E0E"); ????????int?port=8800; ????????byte[]?data="用戶名:admin;密碼:123".getBytes(); ????????//創(chuàng)建數(shù)據(jù)報(bào),包含發(fā)送的數(shù)據(jù)信息 ????????DatagramPacket?packet=new?DatagramPacket(data,data.length,address,port); ????????//創(chuàng)建DatagramSocket對(duì)象 ????????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("我是客戶端,服務(wù)器說:"+info); ????????socket.close(); ????} }
查看全部 -
不錯(cuò),項(xiàng)目思路清晰查看全部
-
?
對(duì)象
查看全部 -
?
傳輸對(duì)象
查看全部 -
傳遞
查看全部 -
傳遞文件
查看全部 -
四大類
查看全部 -
ip地址和端口號(hào)組成了scoket
查看全部 -
http超文件傳輸協(xié)議
查看全部 -
兩臺(tái)計(jì)算機(jī)要想能夠進(jìn)行 通信,需要滿足一些必要的條件
唯一的標(biāo)識(shí):ip;用來標(biāo)識(shí)自己的身份以及自己所處的地址
需要共同的語言;即協(xié)議
相應(yīng)的端口號(hào):每個(gè)主機(jī)上可以運(yùn)行多個(gè)應(yīng)用程序,使用端口號(hào)來決定與哪個(gè)應(yīng)用進(jìn)行通信
TCP/IP是目前世界上使用最為廣泛的協(xié)議
????TCP(Transmission Control Protocol)傳輸控制協(xié)議
????IP:Internet Protocol 互聯(lián)網(wǎng)協(xié)議
查看全部 -
該套課程重點(diǎn)知識(shí)
查看全部 -
UDP的兩個(gè)編程主要類
查看全部 -
socket多客戶端通信
查看全部 -
socket 通信模型和實(shí)現(xiàn)步驟
查看全部 -
socket過程中要考慮優(yōu)先級(jí),這影響到運(yùn)行效率查看全部
-
多線程通信查看全部
舉報(bào)