成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

javaTcp通信客戶端與服務(wù)器端實(shí)例

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

創(chuàng)新互聯(lián)是一家專業(yè)提供秭歸企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為秭歸眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。

由服務(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");
 }
 }
}

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

本文題目:javaTcp通信客戶端與服務(wù)器端實(shí)例
網(wǎng)頁地址:http://jinyejixie.com/article4/pgihoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)網(wǎng)站導(dǎo)航、品牌網(wǎng)站制作、小程序開發(fā)云服務(wù)器、微信公眾號

廣告

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

成都網(wǎng)站建設(shè)
锡林浩特市| 台湾省| 巫山县| 平陆县| 六枝特区| 黔东| 安阳县| 宜宾县| 锡林浩特市| 芦溪县| 德格县| 西和县| 都昌县| 牡丹江市| 祁阳县| 贵州省| 电白县| 曲阳县| 安徽省| 剑河县| 宁化县| 隆昌县| 灌南县| 达日县| 营山县| 瑞安市| 左云县| 隆安县| 毕节市| 长寿区| 潞城市| 讷河市| 平阳县| 大连市| 平原县| 德保县| 阿瓦提县| 芜湖市| 全椒县| 驻马店市| 谢通门县|