TCP協議發送數據 A:創建Socket對象 利用Socket對象 B:獲取輸出流,寫數據 C:釋放資源 package Day26; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; / ...
TCP協議發送數據
A:創建Socket對象---利用Socket對象
B:獲取輸出流,寫數據
C:釋放資源
package Day26; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; /* * TCP協議發送數據: * A:創建發送端的Socket對象 * 這一步如果成功,就說明連接已經建立成功了。 * B:獲取輸出流,寫數據 * C:釋放資源 * * 連接被拒絕。TCP協議一定要先看伺服器。 * java.net.ConnectException: Connection refused: connect */ public class TcpDemo { public static void main(String[] args) throws IOException { //創建Socket對象 // Socket(InetAddress address, int port) //Socket s = new Socket(InetAddress.getByName("192.168.12.92"), 8888); //public Socket(String host,int port) // ----創建一個流套接字並將其連接到指定主機上的指定埠號。 //指定IP地址和連接埠 Socket sk = new Socket("192.168.1.8",12345); //獲取輸出流,寫數據 //OutputStream getOutputStream()------返回此套接字的輸出流。 //此抽象類是表示輸出位元組流的所有類的超類。輸出流接受輸出位元組並將這些位元組發送到某個接收器。 //獲取輸出流 OutputStream os = sk.getOutputStream(); //寫數據-----將字元數據轉換為位元組數組,寫入 os.write("你好中國".getBytes()); //釋放資源 sk.close(); } }
TCP協議接受數據
A:創建Socket接受對象
B:監聽客戶端連接--返回一個對應的Socket對象
C:獲取輸入流,讀取數據並將其顯示在控制台
D:釋放資源
package Day26; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; // * TCP協議接收數據: // * A:創建接收端的Socket對象 // * B:監聽客戶端連接。返回一個對應的Socket對象 // * C:獲取輸入流,讀取數據顯示在控制台 // * D:釋放資源 // */ public class ServersDemo { public static void main(String[] args) throws IOException { //創建Socket接收對象---並給出連接的埠 //public ServerSocket(int port)-------創建綁定到特定埠的伺服器套接字 ServerSocket ss = new ServerSocket(12345); //監聽客戶端連接,返回一個對應的Socket對象 //public Socket accept()----偵聽並接受到此套接字的連接---返回新的套接字 Socket st = ss.accept(); //獲取輸入流和讀出數據顯示到控制台 //獲取輸入流對象 //public InputStream getInputStream()---從此套接字讀取位元組的輸入流。 InputStream is = st.getInputStream(); //讀出數據 byte [] bt = new byte[1024]; //public int read(byte[] b) //從輸入流中讀取一定數量的位元組,並將其存儲在緩衝區數組 b 中 int len = is.read(bt); //public String(byte[] bytes,int offset,int length)---將位元組數組轉換為字元串 String aa = new String(bt,0,len); //獲取發送端的ip地址 String ip = st.getInetAddress().getHostAddress(); //輸出到控制台 System.out.println(ip+"--"+aa); //釋放資源 st.close(); //ss.close();---伺服器不應該關閉 } }
伺服器給客戶端一個反饋的案例
Tcp客戶端
package cn.itcast_07; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class ClientDemo { public static void main(String[] args) throws IOException { // 創建客戶端Socket對象 Socket s = new Socket("192.168.12.92", 9999); // 獲取輸出流 OutputStream os = s.getOutputStream(); os.write("今天天氣很好,適合睡覺".getBytes()); // 獲取輸入流 InputStream is = s.getInputStream(); byte[] bys = new byte[1024]; int len = is.read(bys);// 阻塞 String client = new String(bys, 0, len); System.out.println("client:" + client); // 釋放資源 s.close(); } }
TCP協議伺服器
package cn.itcast_07; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 創建伺服器Socket對象 ServerSocket ss = new ServerSocket(9999); // 監聽客戶端的連接 Socket s = ss.accept(); // 阻塞 // 獲取輸入流 InputStream is = s.getInputStream(); byte[] bys = new byte[1024]; int len = is.read(bys); // 阻塞 String server = new String(bys, 0, len); System.out.println("server:" + server); // 獲取輸出流 OutputStream os = s.getOutputStream(); os.write("數據已經收到".getBytes()); // 釋放資源 s.close(); // ss.close(); } }
* 客戶端鍵盤錄入,伺服器輸出到控制台
客戶端:
package cn.itcast_08; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /* * 客戶端鍵盤錄入,伺服器輸出到控制台 */ public class ClientDemo { public static void main(String[] args) throws IOException { // 創建客戶端Socket對象 Socket s = new Socket("192.168.12.92", 22222); // 鍵盤錄入數據 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 把通道內的流給包裝一下 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { // 鍵盤錄入數據要自定義結束標記 if ("886".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } // 釋放資源 // bw.close(); // br.close(); s.close(); } }
伺服器:
package cn.itcast_08; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 創建伺服器Socket對象 ServerSocket ss = new ServerSocket(22222); // 監聽客戶端連接 Socket s = ss.accept(); // 包裝通道內容的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } // br.close(); s.close(); // ss.close(); } }
客戶端鍵盤錄入,伺服器輸出文本文件
package cn.itcast_09; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /* * 客戶端鍵盤錄入,伺服器輸出文本文件 */ public class ClientDemo { public static void main(String[] args) throws IOException { // 創建客戶端Socket對象 Socket s = new Socket("192.168.12.92", 23456); // 封裝鍵盤錄入 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 封裝通道內的數據 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { if ("over".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } // bw.close(); // br.close(); s.close(); } }
接受數據
package cn.itcast_09; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 創建伺服器Socket對象 ServerSocket ss = new ServerSocket(23456); // 監聽客戶端連接 Socket s = ss.accept(); // 封裝通道內的數據 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 封裝文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); // br.close(); s.close(); // ss.close(); } }
客戶端文本文件,伺服器輸出到控制台
客戶端
package cn.itcast_10; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; /* * 客戶端文本文件,伺服器輸出到控制台 */ public class ClientDemo { public static void main(String[] args) throws IOException { // 創建Socket對象 Socket s = new Socket("192.168.12.92", 34567); // 封裝文本文件 BufferedReader br = new BufferedReader(new FileReader( "InetAddressDemo.java")); // 封裝通道內的流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } br.close(); s.close(); } }
伺服器
package cn.itcast_10; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 創建伺服器Socket對象 ServerSocket ss = new ServerSocket(34567); // 監聽客戶端連接 Socket s = ss.accept(); // 封裝通道內的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } s.close(); } }
TCP協議上傳文本文件----字元流
客戶端
package cn.itcast_11; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; public class UploadClient { public static void main(String[] args) throws IOException { // 創建客戶端Socket對象 Socket s = new Socket("192.168.12.92", 11111); // 封裝文本文件 BufferedReader br = new BufferedReader(new FileReader( "InetAddressDemo.java")); // 封裝通道內流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } // 釋放資源 br.close(); s.close(); } }
伺服器端
package cn.itcast_11; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { // 創建伺服器端的Socket對象 ServerSocket ss = new ServerSocket(11111); // 監聽客戶端連接 Socket s = ss.accept(); // 封裝通道內的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 封裝文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); s.close(); } }
TCP協議上傳文本文件並給出反饋信息----字元流
客戶端
package cn.itcast_12; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /* * 按照我們正常的思路加入反饋信息,結果卻沒反應。為什麼呢? * 讀取文本文件是可以以null作為結束信息的,但是呢,通道內是不能這樣結束信息的。 * 所以,伺服器根本就不知道你結束了。而你還想伺服器給你反饋。所以,就相互等待了。 * * 如何解決呢? * A:在多寫一條數據,告訴伺服器,讀取到這條數據說明我就結束,你也結束吧。 * 這樣做可以解決問題,但是不好。 * B:Socket對象提供了一種解決方案 * public void shutdownOutput() */ public class UploadClient { public static void main(String[] args) throws IOException { // 創建客戶端Socket對象 Socket s = new Socket("192.168.12.92", 11111); // 封裝文本文件 BufferedReader br = new BufferedReader(new FileReader( "InetAddressDemo.java")); // 封裝通道內流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { // 阻塞 bw.write(line); bw.newLine(); bw.flush(); } //自定義一個結束標記 // bw.write("over"); // bw.newLine(); // bw.flush(); //Socket提供了一個終止,它會通知伺服器你別等了,我沒有數據過來了 s.shutdownOutput(); // 接收反饋 BufferedReader brClient = new BufferedReader(new InputStreamReader( s.getInputStream())); String client = brClient.readLine(); // 阻塞 System.out.println(client); // 釋放資源 br.close(); s.close(); } }
伺服器
package cn.itcast_12; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { // 創建伺服器端的Socket對象 ServerSocket ss = new ServerSocket(11111); // 監聽客戶端連接 Socket s = ss.accept();// 阻塞 // 封裝通道內的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 封裝文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java")); String line = null; while ((line = br.readLine()) != null) { // 阻塞 // if("over".equals(line)){ // break; // } bw.write(line); bw.newLine(); bw.flush(); } // 給出反饋 BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); bwServer.write("文件上傳成功"); bwServer.newLine(); bwServer.flush(); // 釋放資源 bw.close(); s.close(); } }
TCP協議上傳圖片 ----採用位元組流
客戶端
A: OutputStream------
public void flush()
throws IOException
- 刷新此輸出流並強制寫出所有緩衝的輸出位元組
package cn.itcast_13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class UploadClient { public static void main(String[] args) throws IOException { // 創建客戶端Socket對象 Socket s = new Socket("192.168.12.92", 19191); // 封裝圖片文件 BufferedInputStream bis = new BufferedInputStream(new FileInputStream( "林青霞.jpg")); // 封裝通道內的流 BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream()); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); bos.flush(); } s.shutdownOutput(); // 讀取反饋 InputStream is = s.getInputStream(); byte[] bys2 = new byte[1024]; int len2 = is.read(bys2); String client = new String(bys2, 0, len2); System.out.println(client); // 釋放資源 bis.close(); s.close(); } }
伺服器端
A: OutputStream------
public void flush()
throws IOException
- 刷新此輸出流並強制寫出所有緩衝的輸出位元組
package cn.itcast_13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { // 創建伺服器Socket對象 ServerSocket ss = new ServerSocket(19191); // 監聽客戶端連接 Socket s = ss.accept(); // 封裝通道內流 BufferedInputStream bis = new BufferedInputStream(s.getInputStream()); // 封裝圖片文件 BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("mn.jpg")); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); bos.flush(); } // 給一個反饋 OutputStream os = s.getOutputStream(); os.write("圖片上傳成功".getBytes()); bos.close(); s.close(); } }
TCP多線程改進多個客戶端上傳文件
客戶端
package cn.itcast_15; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class UploadClient { public static void main(String[] args) throws IOException { // 創建客戶端Socket對象 Socket s = new Socket("192.168.12.92", 11111); // 封裝文本文件 // BufferedReader br = new BufferedReader(new FileReader( // "InetAddressDemo.java")); BufferedReader br = new BufferedReader(new FileReader( "ReceiveDemo.java")); // 封裝通道內流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { // 阻塞 bw.write(line); bw.newLine(); bw.flush(); } // Socket提供了一個終止,它會通知伺服器你別等了,我沒有數據過來了