網路編程TCP(五)__編程

來源:互聯網
上載者:User
補充:網路編程tcp(二)
package com;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;import org.junit.Test;public class TestTcp1 {//用戶端@Testpublic void client(){Socket socket=null;OutputStream os=null;try {socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);os = socket.getOutputStream();os.write("我是1號客戶,請多關照".getBytes());//向外輸出資料} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(os!=null){try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//服務端@Testpublic void server(){ServerSocket server=null;Socket socket=null;InputStream is=null;try {server=new ServerSocket(9000);socket=server.accept();//accept拿到用戶端socket對象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.println(str);}System.out.println("小夥子:收到來自"+socket.getLocalAddress());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(is!=null){try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(server!=null){try {server.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

上例是只能用戶端發送,伺服器接收。下面的例子是伺服器和用戶端互動。有問有答。
package com;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;import org.junit.Test;/** * 案例:用戶端給伺服器發送訊息,伺服器列印資訊。同時發出“已收到資訊”給客戶 * @author Administrator * */public class TestTcp2 {//用戶端@Testpublic void client(){Socket socket=null;OutputStream os=null;InputStream is=null;try {socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);os = socket.getOutputStream();os.write("wo shi kehu 2".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 (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(is!=null){try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(os!=null){try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//服務端@Testpublic void server(){ServerSocket server=null;Socket socket=null;InputStream is=null;OutputStream os=null;try {server=new ServerSocket(9000);socket=server.accept();//accept拿到用戶端socket對象is=socket.getInputStream();//拿到用戶端的socket的資料向程式裡寫入資料byte[] b=new byte[20];int len;while((len=is.read(b))!=-1){String str=new String(b,0,len);System.out.println("fuwqi:收到了:"+socket.getLocalAddress()+str);}//然後伺服器說了,我收到資訊我也給你回饋os=socket.getOutputStream();os.write("shou dao ni de xinxile ,yihouhuihaohao guanzhao ni de ".getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(os!=null){try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(is!=null){try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(server!=null){try {server.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.