溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-10-25 06:09:06 來源:腳本之家 閱讀:98 作者:qq_42412646 欄目:編程語言

本文實(shí)例講述了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");
 }
 }
}

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

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI