一、使用tcp協議實現client向server發送數據
//TCP編程例一:客戶端給服務端發送信息。服務端輸出此信息到控制臺上//網絡編程實際上就是Socket的編程public class TestTCP1 { // 客戶端 @Test public void client() { Socket socket = null; OutputStream os = null; try { // 1.創建一個Socket的對象,通過構造器指明服務端的ip地址,以及其接收程序的端口號 socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090); // 2.getOutputStream():發送數據,方法返回OutputStream的對象 os = socket.getOutputStream(); // 3.具體的輸出過程 os.write("我是客戶端,請多關照".getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.PRintStackTrace(); } finally { // 4.關閉相應的流和Socket對象 if (os != null) { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } // 服務端 @Test public void server() { ServerSocket ss = null; Socket s = null; InputStream is = null; try { // 1.創建一個ServerSocket的對象,通過構造器指明自身的端口號 ss = new ServerSocket(9090); // 2.調用其accept()方法,返回一個Socket的對象 s = ss.accept(); // 3.調用Socket對象的getInputStream()獲取一個從客戶端發送過來的輸入流 is = s.getInputStream(); // 4.對獲取的輸入流進行的操作 byte[] b = new byte[20]; int len; while ((len = is.read(b)) != -1) { String str = new String(b, 0, len); System.out.print(str); } System.out.println("收到來自于" + s.getInetAddress().getHostAddress() + "的連接"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 5.關閉相應的流以及Socket、ServerSocket的對象 if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (s != null) { try { s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (ss != null) { try { ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}二、在一的基礎上實現server向client返回數據//TCP編程例二:客戶端給服務端發送信息,服務端將信息打印到控制臺上,同時發送“已收到信息”給客戶端public class TestTCP2 { //客戶端 @Test public void client(){ Socket socket = null; OutputStream os = null; InputStream is = null; try { socket = new Socket(InetAddress.getByName("127.0.0.1"),8989); os = socket.getOutputStream(); os.write("我是客戶端".getBytes()); //shutdownOutput():執行此方法,顯式的告訴服務端發送完畢! socket.shutdownOutput(); is = socket.getInputStream(); byte[] b = new byte[20]; int len; while((len = is.read(b)) != -1){ String str = new String(b,0,len); System.out.print(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(is != null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(os != null){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //服務端 @Test public void server(){ ServerSocket ss = null; Socket s = null; InputStream is = null; OutputStream os = null; try { ss = new ServerSocket(8989); s = ss.accept(); is = s.getInputStream(); byte[] b = new byte[20]; int len; while((len = is.read(b)) != -1){ String str = new String(b,0,len); System.out.print(str); } os = s.getOutputStream(); os.write("我已收到你的情意".getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(os != null){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(is != null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(s != null){ try { s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ss != null){ try { ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}三、使用流,client向server發送文件//TCP編程例三:從客戶端發送文件給服務端,服務端保存到本地。并返回“發送成功”給客戶端。并關閉相應的連接。//如下的程序,處理異常時,要使用try-catch-finally!!本例僅為了書寫方便~public class TestTCP3 { @Test public void client()throws Exception{ //1.創建Socket的對象 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9898); //2.從本地獲取一個文件發送給服務端 OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("1.jpg")); byte[] b = new byte[1024]; int len; while((len = fis.read(b)) != -1){ os.write(b,0,len); } socket.shutdownOutput(); //3.接收來自于服務端的信息 InputStream is = socket.getInputStream(); byte[] b1 = new byte[1024]; int len1; while((len1 = is.read(b1)) != -1){ String str = new String(b1,0,len1); System.out.print(str); } //4.關閉相應的流和Socket對象 is.close(); os.close(); fis.close(); socket.close(); } @Test public void server() throws Exception{ //1.創建一個ServerSocket的對象 ServerSocket ss = new ServerSocket(9898); //2.調用其accept()方法,返回一個Socket的對象 Socket s = ss.accept(); //3.將從客戶端發送來的信息保存到本地 InputStream is = s.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("3.jpg")); byte[] b = new byte[1024]; int len; while((len = is.read(b)) != -1){ fos.write(b, 0, len); } System.out.println("收到來自于" + s.getInetAddress().getHostAddress() + "的文件"); //4.發送"接收成功"的信息反饋給客戶端 OutputStream os = s.getOutputStream(); os.write("你發送的圖片我已接收成功!".getBytes()); //5.關閉相應的流和Socket及ServerSocket的對象 os.close(); fos.close(); is.close(); s.close(); ss.close(); }}
新聞熱點
疑難解答