javaTcp通信客戶端與服務(wù)器端實例代碼-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“javaTcp通信客戶端與服務(wù)器端實例代碼”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)建站長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為納雍企業(yè)提供專業(yè)的網(wǎng)站制作、做網(wǎng)站納雍網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

本文實例講述了java Tcp通信客戶端與服務(wù)器端。分享給大家供大家參考,具體如下:

由服務(wù)器端發(fā)送數(shù)據(jù)

服務(wù)器端:

import java.io.*;import java.net.*;public class TestSocket { public static void main(String[] args) { try {  ServerSocket ss = new ServerSocket(8888);  while(true) {  Socket s = ss.accept();  OutputStream os = s.getOutputStream();  DataOutputStream dos = new DataOutputStream(os);  dos.writeUTF("hello" + s.getInetAddress() + "port" + s.getPort() + "beybye");  dos.close();//  os.flush();  os.close();//  s.close();  } } catch (IOException e) {  e.printStackTrace();  System.out.println("there is a wrong"); } }}

用戶端:

import java.io.*;import java.net.*;public class TestClient { public static void main(String[] args){ try {  Socket s = new Socket("127.0.0.1",8888);  DataInputStream dis = new DataInputStream(s.getInputStream());  System.out.println(dis.readUTF());   s.close();  dis.close(); } catch (Exception e) {  e.printStackTrace(); } }}

無論是客戶端還是服務(wù)器端都可以收發(fā)數(shù)據(jù)。

交互型

用戶端

import java.io.*;import java.net.*;public class TestClient2 { public static void main(String[] args){ try {  Socket s = new Socket("127.0.0.1",8886);  DataOutputStream dos = new DataOutputStream(s.getOutputStream());  DataInputStream dis = new DataInputStream(s.getInputStream());  System.out.println(dis.readUTF());   dos.writeUTF("hey");  String str = null;  if((str = dis.readUTF()) != null) {  System.out.println(str);  }  s.close();  dis.close();  dos.close(); } catch (Exception e) {  e.printStackTrace(); } }}

服務(wù)器端:

public class TestServer2 { public static void main(String[] args) { InputStream in = null; OutputStream out = null; try {  ServerSocket ss = new ServerSocket(8886);  while(true) {  Socket s = ss.accept();  in = s.getInputStream();  out = s.getOutputStream();  DataOutputStream dos = new DataOutputStream(s.getOutputStream());  DataInputStream dis = new DataInputStream(s.getInputStream());  String str = null;  if((str = dis.readUTF() )!= null) {   System.out.println(str);   System.out.println("form " + s.getInetAddress());   System.out.println("port " + s.getPort());//   dos.writeUTF("hello" + s.getInetAddress() + "port" + s.getPort() + "beybye");  }  dos.writeUTF("hi hello");  dis.close();  dos.close();  s.close();  } } catch (IOException e) {  e.printStackTrace();  System.out.println("there is a wrong"); } }}

“javaTcp通信客戶端與服務(wù)器端實例代碼”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

當前標題:javaTcp通信客戶端與服務(wù)器端實例代碼-創(chuàng)新互聯(lián)
分享地址:http://bm7419.com/article32/dcospc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、小程序開發(fā)、服務(wù)器托管品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、面包屑導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管