深圳市建筑工程佛山seo外包平臺(tái)
4. 綜合練習(xí)
練習(xí)一:多發(fā)多收
需求:
客戶端:多次發(fā)送數(shù)據(jù)
服務(wù)器:接收多次接收數(shù)據(jù),并打印
代碼示例:
public class Client {public static void main(String[] args) throws IOException {//客戶端:多次發(fā)送數(shù)據(jù)//服務(wù)器:接收多次接收數(shù)據(jù),并打印
?//1. 創(chuàng)建Socket對(duì)象并連接服務(wù)端Socket socket = new Socket("127.0.0.1",10000);
?//2.寫出數(shù)據(jù)Scanner sc = new Scanner(System.in);OutputStream os = socket.getOutputStream();
?while (true) {System.out.println("請(qǐng)輸入您要發(fā)送的信息");String str = sc.nextLine();if("886".equals(str)){break;}os.write(str.getBytes());}//3.釋放資源socket.close();}
}
public class Server {public static void main(String[] args) throws IOException {//客戶端:多次發(fā)送數(shù)據(jù)//服務(wù)器:接收多次接收數(shù)據(jù),并打印
?//1.創(chuàng)建對(duì)象綁定10000端口ServerSocket ss = new ServerSocket(10000);
?//2.等待客戶端來連接Socket socket = ss.accept();
?//3.讀取數(shù)據(jù)InputStreamReader isr = new InputStreamReader(socket.getInputStream());int b;while ((b = isr.read()) != -1){System.out.print((char)b);}
?//4.釋放資源socket.close();ss.close();}
}
練習(xí)二:接收并反饋
-
案例需求
客戶端:發(fā)送數(shù)據(jù),接受服務(wù)器反饋
服務(wù)器:收到消息后給出反饋
-
案例分析
-
客戶端創(chuàng)建對(duì)象,使用輸出流輸出數(shù)據(jù)
-
服務(wù)端創(chuàng)建對(duì)象,使用輸入流接受數(shù)據(jù)
-
服務(wù)端使用輸出流給出反饋數(shù)據(jù)
-
客戶端使用輸入流接受反饋數(shù)據(jù)
-
-
代碼實(shí)現(xiàn)
// 客戶端 public class ClientDemo {public static void main(String[] args) throws IOException {Socket socket = new Socket("127.0.0.1",10000); ?OutputStream os = socket.getOutputStream();os.write("hello".getBytes());// os.close();如果在這里關(guān)流,會(huì)導(dǎo)致整個(gè)socket都無法使用socket.shutdownOutput();//僅僅關(guān)閉輸出流.并寫一個(gè)結(jié)束標(biāo)記,對(duì)socket沒有任何影響B(tài)ufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String line;while((line = br.readLine())!=null){System.out.println(line);}br.close();os.close();socket.close();} } // 服務(wù)器 public class ServerDemo {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(10000); ?Socket accept = ss.accept(); ?InputStream is = accept.getInputStream();int b;while((b = is.read())!=-1){System.out.println((char) b);} ?System.out.println("看看我執(zhí)行了嗎?"); ?BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));bw.write("你誰啊?");bw.newLine();bw.flush(); ?bw.close();is.close();accept.close();ss.close();} }
練習(xí)三:上傳練習(xí)(TCP協(xié)議)
-
案例需求
客戶端:數(shù)據(jù)來自于本地文件,接收服務(wù)器反饋
服務(wù)器:接收到的數(shù)據(jù)寫入本地文件,給出反饋
-
案例分析
-
創(chuàng)建客戶端對(duì)象,創(chuàng)建輸入流對(duì)象指向文件,每讀一次數(shù)據(jù)就給服務(wù)器輸出一次數(shù)據(jù),輸出結(jié)束后使用shutdownOutput()方法告知服務(wù)端傳輸結(jié)束
-
創(chuàng)建服務(wù)器對(duì)象,創(chuàng)建輸出流對(duì)象指向文件,每接受一次數(shù)據(jù)就使用輸出流輸出到文件中,傳輸結(jié)束后。使用輸出流給客戶端反饋信息
-
客戶端接受服務(wù)端的回饋信息
-
-
相關(guān)方法
方法名 說明 void shutdownInput() 將此套接字的輸入流放置在“流的末尾” void shutdownOutput() 禁止用此套接字的輸出流 -
代碼實(shí)現(xiàn)
public class Client {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務(wù)器。接收服務(wù)器的反饋。//服務(wù)器:接收客戶端上傳的文件,上傳完畢之后給出反饋。 ? ?//1. 創(chuàng)建Socket對(duì)象,并連接服務(wù)器Socket socket = new Socket("127.0.0.1",10000); ?//2.讀取本地文件中的數(shù)據(jù),并寫到服務(wù)器當(dāng)中BufferedInputStream bis = new BufferedInputStream(new FileInputStream("mysocketnet\\clientdir\\a.jpg"));BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());byte[] bytes = new byte[1024];int len;while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);} ?//往服務(wù)器寫出結(jié)束標(biāo)記socket.shutdownOutput(); ? ?//3.接收服務(wù)器的回寫數(shù)據(jù)BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String line = br.readLine();System.out.println(line); ? ?//4.釋放資源socket.close(); ?} } public class Server {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務(wù)器。接收服務(wù)器的反饋。//服務(wù)器:接收客戶端上傳的文件,上傳完畢之后給出反饋。 ? ?//1.創(chuàng)建對(duì)象并綁定端口ServerSocket ss = new ServerSocket(10000); ?//2.等待客戶端來連接Socket socket = ss.accept(); ?//3.讀取數(shù)據(jù)并保存到本地文件中BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("mysocketnet\\serverdir\\a.jpg"));int len;byte[] bytes = new byte[1024];while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}bos.close();//4.回寫數(shù)據(jù)BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));bw.write("上傳成功");bw.newLine();bw.flush(); ?//5.釋放資源socket.close();ss.close();} }
練習(xí)四:文件名重復(fù)
```java
public class UUIDTest { public static void main(String[] args) { String str = UUID.randomUUID().toString().replace("-", ""); System.out.println(str);//9f15b8c356c54f55bfcb0ee3023fce8a } } ```public class Client {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務(wù)器。接收服務(wù)器的反饋。//服務(wù)器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
?
?//1. 創(chuàng)建Socket對(duì)象,并連接服務(wù)器Socket socket = new Socket("127.0.0.1",10000);
?//2.讀取本地文件中的數(shù)據(jù),并寫到服務(wù)器當(dāng)中BufferedInputStream bis = new BufferedInputStream(new FileInputStream("mysocketnet\\clientdir\\a.jpg"));BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());byte[] bytes = new byte[1024];int len;while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}
?//往服務(wù)器寫出結(jié)束標(biāo)記socket.shutdownOutput();
?
?//3.接收服務(wù)器的回寫數(shù)據(jù)BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String line = br.readLine();System.out.println(line);
?
?//4.釋放資源socket.close();
?}
}
public class Server {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務(wù)器。接收服務(wù)器的反饋。//服務(wù)器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
?
?//1.創(chuàng)建對(duì)象并綁定端口ServerSocket ss = new ServerSocket(10000);
?//2.等待客戶端來連接Socket socket = ss.accept();
?//3.讀取數(shù)據(jù)并保存到本地文件中BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());String name = UUID.randomUUID().toString().replace("-", "");BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("mysocketnet\\serverdir\\" + name + ".jpg"));int len;byte[] bytes = new byte[1024];while ((len = bis.read(bytes)) != -1) {bos.write(bytes, 0, len);}bos.close();//4.回寫數(shù)據(jù)BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));bw.write("上傳成功");bw.newLine();bw.flush();
?//5.釋放資源socket.close();ss.close();}