最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)TCP/IP協(xié)議的收發(fā)數(shù)據(jù)(服務(wù)端)代碼實(shí)例

 更新時(shí)間:2019年11月28日 14:33:43   作者:農(nóng)名工進(jìn)城  
這篇文章主要介紹了Java實(shí)現(xiàn)TCP/IP協(xié)議的收發(fā)數(shù)據(jù)(服務(wù)端)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Java實(shí)現(xiàn)TCP/IP協(xié)議的收發(fā)數(shù)據(jù)(服務(wù)端)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

功能如下:

注:

只有服務(wù)端,沒(méi)有客戶端,測(cè)試時(shí)采用第三方軟件作為客戶端的。

收發(fā)數(shù)據(jù)目前能正常收發(fā)數(shù)據(jù),只是中文的會(huì)變成亂碼顯示。

采用Thread類(lèi)實(shí)現(xiàn)一個(gè)收發(fā)數(shù)據(jù)的線程。

服務(wù)端代碼:

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

public class Server {
  //監(jiān)聽(tīng)端口
  private static final int PORT = 60020;

  public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    Socket socket = null;
    try {
      //建立服務(wù)器的Socket,并設(shè)定一個(gè)監(jiān)聽(tīng)的端口PORT
      serverSocket = new ServerSocket(PORT);
      //由于需要進(jìn)行循環(huán)監(jiān)聽(tīng),因此獲取消息的操作應(yīng)放在一個(gè)while大循環(huán)中
      while(true){
         try {
          //建立跟客戶端的連接
          socket = serverSocket.accept();
         } catch (Exception e) {
           System.out.println("建立與客戶端的連接出現(xiàn)異常");
           e.printStackTrace();
         }
         ServerThread thread = new ServerThread(socket);
         thread.start();
      }
    } catch (Exception e) {
      System.out.println("端口被占用");
      e.printStackTrace();
    }    
    finally {
      serverSocket.close();
    }
  }
}

//服務(wù)端線程類(lèi)
//繼承Thread類(lèi)的話,必須重寫(xiě)run方法,在run方法中定義需要執(zhí)行的任務(wù)。
class ServerThread extends Thread {
  private Socket socket ;
  InputStream inputStream;
  OutputStream outputStream;
  
  public ServerThread(Socket socket){
    this.socket=socket;
  }
  public void run(){
    try {
      while (true){
        //接收客戶端的消息并打印
        System.out.println(socket);
        inputStream=socket.getInputStream();
        byte[] bytes = new byte[1024];
        inputStream.read(bytes);
        String string = new String(bytes);
        System.out.println(string);  
        
        //向客戶端發(fā)送消息
        outputStream = socket.getOutputStream();
        outputStream.write("OK".getBytes());
        System.out.println("OK");
        
      }
    } catch (Exception e) {
      System.out.println("客戶端主動(dòng)斷開(kāi)連接了");
      //e.printStackTrace();
    }
    //操作結(jié)束,關(guān)閉socket
    try{
      socket.close(); 
    }catch(IOException e){
      System.out.println("關(guān)閉連接出現(xiàn)異常");  
      e.printStackTrace();
    }
  }
}

先開(kāi)啟服務(wù)端,再開(kāi)啟客戶端,進(jìn)行操作。

客戶端:

(端口號(hào)選擇“TCPClient”,遠(yuǎn)程填寫(xiě)IP地址和程序里設(shè)定好的端口“60020”,本地選擇自己電腦的IP地址)

服務(wù)端:

注:

串口調(diào)試工具下載地址

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

三明市| 无为县| 普兰店市| 剑河县| 浦城县| 永顺县| 醴陵市| 新沂市| 密山市| 阿拉尔市| 五常市| 黔东| 萍乡市| 山阴县| 合水县| 社会| 惠东县| 玉林市| 安宁市| 黔东| 防城港市| 益阳市| 荣昌县| 盐池县| 铅山县| 平江县| 井研县| 鞍山市| 鄂伦春自治旗| 高雄市| 扶沟县| 绥中县| 广南县| 佳木斯市| 桂平市| 淳安县| 天峻县| 垦利县| 台南市| 固原市| 乳山市|