java之網絡編程1-Tcp
阿新 • • 發佈:2018-10-14
sco sta out ack socket編程 同時 byname 自身 tin
一,了解之前先了解一下網絡基礎
首先理清一個概念:網絡編程 != 網站編程,網絡編程現在一般稱為TCP/IP編程
一般的網絡編程都稱為Socket編程,Socket的英文意思是“插座”
網絡編程的目的:直接或者間接的通過網絡協議與其他計算機進行通信。
網絡編程中有兩個主要的問題:
-->如何準確定為網絡上一臺或者多臺主機:IP和端口號
-->找到主機後如何可靠搞笑的進行數據傳輸:Tcp/Ip 協議(四層)
二,網絡通信要素
網絡通信要素1:IP地址,通過Ip,可以唯一的定為互聯網上的一臺主機
ip地址唯一定為一臺主機,而端口號定為主機上的某一個正在運行的軟件
通訊要素2:網絡通信協議TCP/IP協議
三,TCP編程案例1:客戶端給服務端發消息,服務端輸出此消息到控制臺
import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * @author: wsj * @date: 2018/10/14 * @time: 15:08 */ //客戶端給服務端發消息,服務端輸出此消息到控制臺public class testCS { //客戶端 @Test public void client(){ Socket socket = null; OutputStream outputStream = null; try { //1.創建一個Scoket 的對象,通過構造器知名服務端的IP地址,以及其接收的端口號 socket = new Socket(InetAddress.getByName("127.0.0.1"),9090); //2.getOutputStream():發送數據,方法返回OutputStream對象outputStream = socket.getOutputStream(); //3.具體的輸出過程 outputStream.write(("我是客戶端,我的地址是"+socket.getInetAddress().getHostAddress()).getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { //4.關閉流和Socket if(null != outputStream) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != socket) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } //服務端 @Test public void server(){ ServerSocket ss = null; Socket st = null; InputStream is = null; try { //1.創建一個ServerSocket的對象,通過構造器指明自身的端口號 ss = new ServerSocket(9090); //2.調用其accept()方法,返回一個Socket對象 st = ss.accept(); //3.調用Socket對象的getInputStream() 獲取一個從客戶端發送過來的輸入流 is = st.getInputStream(); //4.對獲取的輸入流進行操作 byte[] b = new byte[50]; int len; while((len = is.read(b)) != -1){ String str = new String(b,0,len); System.out.println(str+"\n"); System.out.println("收到來自"+st.getInetAddress().getHostAddress()+"的請求並已回應"); } } catch (IOException e) { e.printStackTrace(); }finally { //5.關閉流和Socket 和ServerSocket對象 if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != ss) { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != st) { try { st.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
TCP編程案例2:客戶端給服務端發消息,服務端輸出此消息到控制臺,同時發送已收到信息給客戶端
import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * @author: wsj * @date: 2018/10/14 * @time: 15:08 */ //客戶端給服務端發消息,服務端輸出此消息到控制臺 public class testCS { //客戶端 @Test public void client(){ Socket socket = null; OutputStream outputStream = null; InputStream is = null; try { //1.創建一個Scoket 的對象,通過構造器知名服務端的IP地址,以及其接收的端口號 socket = new Socket(InetAddress.getByName("127.0.0.1"),9090); //2.getOutputStream():發送數據,方法返回OutputStream對象 outputStream = socket.getOutputStream(); //3.具體的輸出過程 outputStream.write(("我是客戶端,我的地址是"+socket.getInetAddress().getHostAddress()).getBytes()); //聲明給服務端發送的東西完畢 socket.shutdownOutput(); //接收服務端的回應 is = socket.getInputStream(); byte[] b = new byte[50]; int len; while((len = is.read(b)) != -1){ String str = new String(b,0,len); System.out.println(str); } } catch (IOException e) { e.printStackTrace(); }finally { //4.關閉流和Socket if(null != outputStream) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != socket) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } //服務端 @Test public void server(){ ServerSocket ss = null; Socket st = null; InputStream is = null; OutputStream outputStream = null; try { //1.創建一個ServerSocket的對象,通過構造器指明自身的端口號 ss = new ServerSocket(9090); //2.調用其accept()方法,返回一個Socket對象 st = ss.accept(); //3.調用Socket對象的getInputStream() 獲取一個從客戶端發送過來的輸入流 is = st.getInputStream(); //4.對獲取的輸入流進行操作 byte[] b = new byte[50]; int len; while((len = is.read(b)) != -1){ String str = new String(b,0,len); System.out.println(str); } // if(st.isInputShutdown()) { //給予客戶端回應 outputStream = st.getOutputStream(); outputStream.write("我已經收到,這是給你(客戶端)的回應哦".getBytes()); // } } catch (IOException e) { e.printStackTrace(); }finally { //5.關閉流和Socket 和ServerSocket對象 if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != ss) { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != st) { try { st.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
一般關流的時候,遵循從後往前的順序。
案例3:從客戶端發送文件給服務端,服務端保存到本地,並返回"發送成功"給客戶端,並關閉相關連接
import org.junit.Test; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * @author: wsj * @date: 2018/10/14 * @time: 16:21 */ //從客戶端發送文件給服務端,服務端保存到本地,並返回"發送成功"給客戶端,並關閉相關連接
//註意:本案例中的關流等操作都應該使用try...catch...finally 而不能使用throws Exception 本案例只是為書寫方便~
public class socketTomcat { //客戶端 @Test public void client() throws Exception{ // Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9898); // OutputStream outputStream = socket.getOutputStream(); FileInputStream file = new FileInputStream(new File("1.jpg")); byte[] b = new byte[1024]; int len ; while((len = file.read(b)) != -1){ outputStream.write(b,0,len); } // socket.shutdownOutput(); // InputStream inputStream = socket.getInputStream(); byte[] b1 = new byte[1024]; int len1 ; while((len1 = inputStream.read(b1)) != -1){ String str = new String(b1,0,len1); System.out.println(str); } inputStream.close(); outputStream.close(); file.close(); } @Test public void server() throws Exception{ ServerSocket serverSocket = new ServerSocket(9898); Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); FileOutputStream file = new FileOutputStream("3.jpg"); byte[] b1 = new byte[1024]; int len1 ; while((len1 = inputStream.read(b1)) != -1){ file.write(b1,0,len1); } OutputStream outputStream = socket.getOutputStream(); outputStream.write(("來自"+socket.getInetAddress().getHostAddress()+"的圖片接收成功").getBytes()); outputStream.close(); file.close(); inputStream.close(); socket.close(); serverSocket.close(); } }
java之網絡編程1-Tcp