Socket 網(wǎng)絡(luò)編程
目的
了解什么是網(wǎng)絡(luò)編程
網(wǎng)絡(luò)編程:在兩臺不同的計算機之間傳遞數(shù)據(jù)
1.QQ聊天
2.打開網(wǎng)易app 刷新界面 就有數(shù)據(jù)
3.打開瀏覽器 網(wǎng)頁 www.baidu.com
客戶端:手機APP 瀏覽器 桌面QQ
服務(wù)器端:存儲、處理數(shù)據(jù)
URL:統(tǒng)一的資源定位
http/https :傳輸協(xié)議 TCP/ IP
www.baidu.com:域名 表示一臺網(wǎng)絡(luò)中的電腦
IPv4 IPv6 地址 : 唯一標識一網(wǎng)絡(luò)中的計算機
32位:四個部分 每個部分是8個字節(jié)
170.20.10.15DNS服務(wù)器:域名解析
將www.baidu.com類型的域名解析為對應(yīng)的IP地址search 對應(yīng)的后臺程序文件 PHP java jsp
? 表示運行這個程序需要傳遞的參數(shù)
code=android code是服務(wù)器端規(guī)定的字段
& 如果有多個參數(shù) 使用&符號鏈接
IP地址 找到這臺電腦 唯一標識一臺電腦
端口號:找到這臺電腦上的某一個程序 唯一標識電腦上的某一個進程(程序)
數(shù)據(jù)傳輸:TCP/IP協(xié)議
TCP:面向鏈接的 安全的
UDP:面向無鏈接的 不安全 但快
網(wǎng)絡(luò)中的數(shù)據(jù)傳輸:Socket實現(xiàn)->套接字
Socket:客服端
ServerSocket:服務(wù)器端
讀取數(shù)據(jù):服務(wù)器讀取 客戶端讀取
BufferedReader -> InputStreamReader -> socket.getInputStream();
讀取終端
BufferedReader -> InputStreamReader -> System.in輸出:客戶端輸出 服務(wù)器端輸出
BufferedWriter -> OutputStreamWriter -> socket.getOutputStream();
技術(shù)
1.實現(xiàn)客戶端和服務(wù)器端傳遞數(shù)據(jù)
2.實現(xiàn)服務(wù)器端手動輸入抒线,將輸入的內(nèi)容發(fā)送給客戶端
3.實現(xiàn)客戶端向服務(wù)器端上傳圖片
4.使用多線程對聊
5.實現(xiàn)群聊
技術(shù)實現(xiàn)
- 實現(xiàn)客戶端和服務(wù)器端傳遞數(shù)據(jù)
注意端口號必須相同 先運行Server 再運行Client
// 模擬客戶端
class Client{
public static void main(String[] args) throws IOException {
// 1.創(chuàng)建用于通信的socket
// 指明和誰通信:IP地址 端口號
Socket socket = new Socket("10.129.28.234",6767);
// 接收服務(wù)器端的數(shù)據(jù)
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// // 向服務(wù)器端發(fā)送數(shù)據(jù)
//
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// bw.write("thank you very much");
// 讀取服務(wù)器端發(fā)來的數(shù)據(jù)
String line = null;
while ((line = br.readLine()) != null){
System.out.println(line);
}
// 客戶端向服務(wù)器端發(fā)送數(shù)據(jù)
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("你好啊");
socket.shutdownInput();
}
}
// 模擬服務(wù)器端
class Server{
public static void main(String[] args) throws IOException {
// 1.創(chuàng)建服務(wù)器端的serverSocket
ServerSocket ss = new ServerSocket(6767);
// 2.獲取鏈接的客戶端的socket
Socket clientSocket = ss.accept();
// 3.向客戶端發(fā)送數(shù)據(jù)
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
ps.println("登錄成功");
// 輸出完畢
clientSocket.shutdownInput();
// 讀取客戶端信息
// BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// String line = null;
// while ((line = br.readLine()) != null) {
// System.out.println(line);
// }
//4.接收客戶端發(fā)來的數(shù)據(jù)
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line = null;
while ((line = br.readLine()) != null){
System.out.println(line);
}
}
}
- 實現(xiàn)服務(wù)器端手動輸入垢乙,將輸入的內(nèi)容發(fā)送給客戶端
可以一直發(fā)
class MyClient{
public static void main(String[] args) throws IOException {
Socket socket = null;
// 連接服務(wù)器端的socket
try {
socket = new Socket("10.129.28.234",8888);
// 接收服務(wù)器端的信息
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = br.readLine())!=null){
System.out.println(line);
}
} catch (IOException e) {
System.out.println("網(wǎng)絡(luò)出錯查近,請重新登錄"); }finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class MyServer{
public static void main(String[] args){
try( ServerSocket ss = new ServerSocket(8888)) {
// 監(jiān)聽客戶端
Socket socket = ss.accept();
// 從終端接收數(shù)據(jù)
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
// 獲取向客戶端輸出數(shù)據(jù)的輸出流
PrintStream ps = new PrintStream(socket.getOutputStream());
String line = null;
while ((line = keyin.readLine())!= null){
// 發(fā)送給客戶端
ps.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 實現(xiàn)客戶端向服務(wù)器端上傳圖片
class Client{
public static void main(String[] args) throws IOException {
// 連接服務(wù)器端 獲取 socket
Socket socket = new Socket("10.129.28.234",1111);
// 創(chuàng)建服務(wù)器端對應(yīng)的輸入流 用于接收服務(wù)器端發(fā)來的數(shù)據(jù)
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(br.readLine());
// 向服務(wù)器端發(fā)送圖片
// 1.將文件寫入到內(nèi)存里
String path = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\java\\src\\main\\java\\day8\\1.jpg";
FileInputStream fis = new FileInputStream(path);
// 2.創(chuàng)建字節(jié)流
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
// 3.將文件的內(nèi)容一點一點的傳輸?shù)椒?wù)器
byte[] buf = new byte[1024];
int len = -1;
while ((len = fis.read(buf))!= -1){
bos.write(buf,0,len);
}
}
}
class Server{
public static void main(String[] args) throws IOException {
// 創(chuàng)建服務(wù)器端的ServerSocket
ServerSocket ss = new ServerSocket(1111);
// 監(jiān)聽客戶端連接
// 當前有客戶端來連接這個服務(wù)器 就可以得到對應(yīng)的socket
// 當沒有客戶端來連接 服務(wù)器一直在這里等待
Socket socket = ss.accept();
// 創(chuàng)建客戶端對應(yīng)的輸出流 用于向這個客戶端發(fā)送數(shù)據(jù)
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("連接成功 可以發(fā)數(shù)據(jù)了");
// 接收客戶端傳遞過來的圖片
// 文件對應(yīng)的輸出流
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
String path = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\java\\src\\main\\java\\day12\\medio\\2.jpg";
FileOutputStream fos = new FileOutputStream(path);
byte[] buf = new byte[1024];
int len = -1;
while((len = bis.read(buf)) != -1){
fos.write(buf,0,len);
}
}
}
- 使用多線程實現(xiàn)對聊
class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("10.129.28.234",6789);
// 主線程處理終端輸入 發(fā)送給服務(wù)器端
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
PrintStream ps = new PrintStream(socket.getOutputStream());
// 用一個子線程處理服務(wù)器端數(shù)據(jù)
new Thread(new ClientThread(socket)).start();
String line = null;
while ((line = keyin.readLine())!=null){
ps.println(line);
}
}
}
/**
* 創(chuàng)建一個子線程處理客戶端接收服務(wù)器端的數(shù)據(jù)
*/
class ClientThread implements Runnable{
private Socket socket;
// 保存socket對象
public ClientThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
// 獲取服務(wù)器端的輸入流對象
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = br.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
System.out.println("網(wǎng)絡(luò)出錯 請重新登錄");
System.exit(-1);
}finally {
if(br != null){
try {
br.close();
if (socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(6789);
Socket socket = ss.accept();
// 創(chuàng)建子線程 處理客戶端輸入信息
new ServerThread(socket).start();
// 終端輸入流對象
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
// 客戶端的輸出流
PrintStream ps = new PrintStream(socket.getOutputStream());
// 讀取終端的輸入 將輸入輸出給客戶端
String line = null;
while ((line = keyin.readLine()) != null){
ps.println(line);
}
}
}
/**
* 創(chuàng)建一個子線程處理服務(wù)器端接收客戶端的數(shù)據(jù)
*/
class ServerThread extends Thread{
private Socket socket;
public ServerThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = br.readLine())!= null){
System.out.println(line);
}
} catch (IOException e) {
System.out.println("網(wǎng)絡(luò)異常 請重新登錄");
System.exit(-1);
}finally {
try {
if (br != null){
br.close();
}
if (socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 實現(xiàn)群聊
class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("10.129.28.234",7657);
// 主線程處理終端輸入 發(fā)送給服務(wù)器端
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
PrintStream ps = new PrintStream(socket.getOutputStream());
// 用一個子線程處理服務(wù)器端數(shù)據(jù)
new Thread(new ClientThread(socket)).start();
String line = null;
while ((line = keyin.readLine())!=null){
ps.println(line);
}
}
}
class Client1 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("10.129.28.234",7657);
// 主線程處理終端輸入 發(fā)送給服務(wù)器端
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
PrintStream ps = new PrintStream(socket.getOutputStream());
// 用一個子線程處理服務(wù)器端數(shù)據(jù)
new Thread(new ClientThread(socket)).start();
String line = null;
while ((line = keyin.readLine())!=null){
ps.println(line);
}
}
}
class Server{
// 保存每一個連接過來的socket對象
public static ArrayList<Socket> sockets = new ArrayList<>();
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(7657);
// 不停地等待客戶端來連接
while (true){
Socket socket = ss.accept();
// 當有客戶端連接過來了 就保存
sockets.add(socket);
// 開啟一個線程處理每個客戶端的輸入
new ServerThread(socket).start();
}
}
}
class ClientThread implements Runnable{
private Socket socket;
// 保存socket對象
public ClientThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
// 獲取服務(wù)器端的輸入流對象
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = br.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
System.out.println("網(wǎng)絡(luò)出錯 請重新登錄");
System.exit(-1);
}finally {
if(br != null){
try {
br.close();
if (socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class ServerThread extends Thread{
private Socket socket;
public ServerThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = br.readLine())!= null){
// 群發(fā)消息
// 遍歷Server維護的數(shù)組
for(Socket s:Server.sockets){
// 向socket對應(yīng)的客戶端發(fā)送消息
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println(line);
}
}
} catch (IOException e) {
System.out.println("網(wǎng)絡(luò)異常 請重新登錄");
System.exit(-1);
}finally {
try {
if (br != null){
br.close();
}
if (socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
心得
今天學習網(wǎng)絡(luò),其實在上課期間我特別懵挤忙,就是一直在跟著敲霜威,但是卻沒有搞懂。感覺挺難的饭玲,不過當晚上看視頻的時候侥祭,很多地方都明白了,感覺也不是那么難茄厘。這樣我就能更好學習明天的知識了矮冬。