深圳市建筑工程佛山seo外包平臺
4. 綜合練習
練習一:多發(fā)多收
需求:
客戶端:多次發(fā)送數(shù)據(jù)
服務器:接收多次接收數(shù)據(jù),并打印
代碼示例:
public class Client {public static void main(String[] args) throws IOException {//客戶端:多次發(fā)送數(shù)據(jù)//服務器:接收多次接收數(shù)據(jù),并打印
?//1. 創(chuàng)建Socket對象并連接服務端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("請輸入您要發(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ù)//服務器:接收多次接收數(shù)據(jù),并打印
?//1.創(chuàng)建對象綁定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();}
}
練習二:接收并反饋
-
案例需求
客戶端:發(fā)送數(shù)據(jù),接受服務器反饋
服務器:收到消息后給出反饋
-
案例分析
-
客戶端創(chuàng)建對象,使用輸出流輸出數(shù)據(jù)
-
服務端創(chuàng)建對象,使用輸入流接受數(shù)據(jù)
-
服務端使用輸出流給出反饋數(shù)據(jù)
-
客戶端使用輸入流接受反饋數(shù)據(jù)
-
-
代碼實現(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();如果在這里關流,會導致整個socket都無法使用socket.shutdownOutput();//僅僅關閉輸出流.并寫一個結束標記,對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();} } // 服務器 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();} }
練習三:上傳練習(TCP協(xié)議)
-
案例需求
客戶端:數(shù)據(jù)來自于本地文件,接收服務器反饋
服務器:接收到的數(shù)據(jù)寫入本地文件,給出反饋
-
案例分析
-
創(chuàng)建客戶端對象,創(chuàng)建輸入流對象指向文件,每讀一次數(shù)據(jù)就給服務器輸出一次數(shù)據(jù),輸出結束后使用shutdownOutput()方法告知服務端傳輸結束
-
創(chuàng)建服務器對象,創(chuàng)建輸出流對象指向文件,每接受一次數(shù)據(jù)就使用輸出流輸出到文件中,傳輸結束后。使用輸出流給客戶端反饋信息
-
客戶端接受服務端的回饋信息
-
-
相關方法
方法名 說明 void shutdownInput() 將此套接字的輸入流放置在“流的末尾” void shutdownOutput() 禁止用此套接字的輸出流 -
代碼實現(xiàn)
public class Client {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。 ? ?//1. 創(chuàng)建Socket對象,并連接服務器Socket socket = new Socket("127.0.0.1",10000); ?//2.讀取本地文件中的數(shù)據(jù),并寫到服務器當中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);} ?//往服務器寫出結束標記socket.shutdownOutput(); ? ?//3.接收服務器的回寫數(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 {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。 ? ?//1.創(chuàng)建對象并綁定端口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();} }
練習四:文件名重復
```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 {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
?
?//1. 創(chuàng)建Socket對象,并連接服務器Socket socket = new Socket("127.0.0.1",10000);
?//2.讀取本地文件中的數(shù)據(jù),并寫到服務器當中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);}
?//往服務器寫出結束標記socket.shutdownOutput();
?
?//3.接收服務器的回寫數(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 {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
?
?//1.創(chuàng)建對象并綁定端口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();}