溫馨提示×

溫馨提示×

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

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

java 中如何實(shí)現(xiàn)模擬TCP協(xié)議進(jìn)行傳輸數(shù)據(jù)

發(fā)布時間:2020-11-12 16:12:05 來源:億速云 閱讀:179 作者:Leah 欄目:編程語言

java 中如何實(shí)現(xiàn)模擬TCP協(xié)議進(jìn)行傳輸數(shù)據(jù)?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

一、創(chuàng)建TCP傳輸?shù)目蛻舳?/strong>

1、建立TCP客戶端的Socket服務(wù),使用的是Socket對象,建議該對象一創(chuàng)建就明確目的地,即要連接的主機(jī);

2、如果連接建立成功,說明數(shù)據(jù)傳輸通道已建立,該通道就是Socket流,是底層建立好的,既然是流,說著這里既有輸入流,又有輸出流,想要輸入流或者輸出流對象,可以通過Socket來獲取,可以通過getOutputStream()和getInputStream()來獲??;

3、使用輸出流,將數(shù)據(jù)寫出;

4、關(guān)閉Socket服務(wù)。

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
  public static void main(String[] args) throws IOException {

    // 1、創(chuàng)建客戶端的Socket服務(wù)
    Socket socket = new Socket("192.168.1.100", 10002);

    // 2、獲取Socket流中輸入流
    OutputStream out = socket.getOutputStream();

    // 3、使用輸出流將指定的數(shù)據(jù)寫出去
    out.write("TCP is coming !".getBytes());

    // 4、關(guān)閉Socket服務(wù)
    socket.close();
  }
}

二、創(chuàng)建TCP傳輸?shù)姆?wù)端

1、建立TCP服務(wù)端的的Socket服務(wù),通過ServerSocket對象;

2、服務(wù)端必須對外提供一個端口,否則客戶端無法連接;

3、獲取連接過來的客戶端對象;

4、通過客戶端對象來獲取Socket流,讀取客戶端發(fā)來的數(shù)據(jù);

5、關(guān)閉資源,關(guān)客戶端,關(guān)服務(wù)端。

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
  public static void main(String[] args) throws IOException {

    // 1、創(chuàng)建客戶端對象
    ServerSocket ss = new ServerSocket(10002);

    // 2、獲取連接過來的客戶端對象
    Socket s = ss.accept();

    String ip = s.getInetAddress().getHostAddress();

    // 3、通過Socket對象獲取輸入流,讀取客戶端發(fā)來的數(shù)據(jù)
    InputStream in = s.getInputStream();

    byte[] buf = new byte[1024];

    int len = in.read(buf);
    String text = new String(buf, 0, len);
    System.out.println(ip + ":" + text);

  // 4、關(guān)閉資源
    s.close();
    ss.close();
  }
}

三、優(yōu)化TCP傳輸?shù)目蛻舳撕头?wù)端

在本部分,我們對前兩部分的內(nèi)容進(jìn)行優(yōu)化,實(shí)現(xiàn)TCP傳輸模式下的客戶端和服務(wù)端的交互功能。

/**
*優(yōu)化TCP傳輸?shù)目蛻舳?
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ClientUpdate {
  public static void main(String[] args) throws IOException {

    Socket socket = new Socket("192.168.1.100", 10002);

    OutputStream out = socket.getOutputStream();

    out.write("tcp!".getBytes());

    // 讀取服務(wù)端返回的數(shù)據(jù),使用Socket讀取流
    InputStream in = socket.getInputStream();
    byte[] buf = new byte[1024];

    int len = in.read(buf);

    String text = new String(buf, 0, len);

    System.out.println(text);

    socket.close();
  }
}
/**
*優(yōu)化TCP傳輸?shù)姆?wù)端
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerUpdate {
  public static void main(String[] args) throws IOException {

    // 1、創(chuàng)建服務(wù)端對象
    ServerSocket ss = new ServerSocket(10002);

    // 2、獲取連接過來的客戶端對象
    Socket s = ss.accept(); //accept方式為阻塞式方法

    String ip = s.getInetAddress().getHostAddress();

    // 3、通過Socket對象獲取輸入流,要讀取客戶端發(fā)來的數(shù)據(jù)
    InputStream in = s.getInputStream();

    byte[] buf = new byte[1024];

    int len = in.read(buf);
    String text = new String(buf, 0, len);
    System.out.println(ip + ":" + text);

    // 使用客戶端的Socket對象的輸出流給客戶端返回數(shù)據(jù)
    OutputStream out = s.getOutputStream();
    out.write("收到".getBytes());

    s.close();
    ss.close();
  }
}

四、創(chuàng)建英文大寫轉(zhuǎn)換服務(wù)器

應(yīng)用TCP(Transmission Control Protocol,傳輸控制協(xié)議)的相關(guān)性質(zhì),創(chuàng)建一個基于TCP傳輸下的英文大寫轉(zhuǎn)換服務(wù)器,要求:客戶端輸入字母數(shù)據(jù),發(fā)送給服務(wù)端;服務(wù)端收到數(shù)據(jù)后顯示在控制臺,并將該數(shù)據(jù)轉(zhuǎn)成大寫字母返回給客戶端;直到客戶端輸入“over”為止,轉(zhuǎn)換結(jié)束。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TransClient {
  public static void main(String[] args) throws IOException {
    /**
     * 思路:創(chuàng)建客戶端
     * 1、創(chuàng)建Socket客戶端對象
     * 2、獲取鍵盤錄入的數(shù)據(jù)
     * 3、將錄入的信息發(fā)送給Socket輸出流
     * 4、讀取服務(wù)端的數(shù)據(jù)并返回的大寫數(shù)據(jù)
     */

    // 1、創(chuàng)建Socket客戶端對象
    Socket s = new Socket("192.168.1.100", 10004);

    // 2、獲取鍵盤錄入
    BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

    // 3、Socket輸出流
    PrintWriter out = new PrintWriter(s.getOutputStream(), true);

    // 4、Socket輸入流,讀取服務(wù)端的數(shù)據(jù)并返回的大寫數(shù)據(jù)
    BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String line = null;

    while ((line = bufr.readLine()) != null) {

      if ("over".equals(line))
        break;
      out.println(line);

      // 讀取服務(wù)端返回的一行大寫數(shù)據(jù)
      String upperStr = bufIn.readLine();
      System.out.println(upperStr);
    }
    s.close();
  }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TransServer {
  public static void main(String[] args) throws IOException {
    /**
     * 思路:創(chuàng)建服務(wù)端
     * 1、創(chuàng)建SeverSocket客戶端對象
     * 2、獲取Socket流
     * 3、通過Socket, 讀取客戶端發(fā)過來的需要轉(zhuǎn)換的數(shù)據(jù)
     * 4、顯示在控制臺上
     * 5、將數(shù)據(jù)轉(zhuǎn)換成大寫返回給客戶端
     */

    // 1、創(chuàng)建SeverSocket對象
    ServerSocket ss = new ServerSocket(10004);

    // 2、獲取Socket對象
    Socket s = ss.accept();

    // 獲取IP地址
    String ip = s.getInetAddress().getHostAddress();
    System.out.println(ip + "......connected");

    // 3、獲取Socket讀取流,并裝飾
    BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

    // 4、獲取Socket的輸出流,并裝飾
    PrintWriter out = new PrintWriter(s.getOutputStream(), true);

    String line = null;
    while ((line = bufIn.readLine()) != null) {
      System.out.println(line);
      out.println(line.toUpperCase());
    }

    s.close();
    ss.close();
  }
}

關(guān)于java 中如何實(shí)現(xiàn)模擬TCP協(xié)議進(jìn)行傳輸數(shù)據(jù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

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

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

AI