日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

go将服务器图片响应给客户端,Go中来自客户端和服务器的RPC

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go将服务器图片响应给客户端,Go中来自客户端和服务器的RPC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我目前正在使用thrift(thrift4go)來實現服務器->客戶端和客戶端->服務器RPC功能。默認情況下,thrift僅像net / rpc一樣執行客戶端->服務器調用。由于還需要服務器與客戶機之間的通信,因此我進行了一些研究,發現bidi-thrift。Bidi-thrift解釋了如何連接Java服務器+ Java客戶端進行雙向節儉通信。

比迪蒂節儉是做什么的,它的局限性。

TCP連接具有傳入和傳出的通信線路(RC和TX)。bidi-thrift的想法是將RS和TX分開,并將它們提供給客戶端應用程序和服務器應用程序上的服務器(處理器)和客戶端(遠程)。我發現在Go中很難做到這一點。同樣,這種方式沒有“響應”的可能(正在使用響應線)。因此,服務中的所有方法都必須“單向無效”。(開火忘了,打電話沒有結果)。

解決方案

我改變了“比迪節約”的概念,使客戶端打開了到服務器的兩個連接,即A和B。第一個連接(A)用于執行客戶端->服務器通信(客戶端照常進行呼叫)。第二個連接(B)被“劫持”,并且連接到客戶端上的服務器(處理器),而第二個連接(B)連接到服務器上的客戶端(遠程)。我已經將其與Go服務器和Java客戶端一起使用了。效果很好。它既快速又可靠(就像普通的節儉一樣)。

一些來源。B連接(服務器->客戶端)的設置如下:

轉到服務器

// factories

framedTransportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())

protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

// create socket listener

addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9091")

if err != nil {

log.Print("Error resolving address: ", err.Error(), "\n")

return

}

serverTransport, err := thrift.NewTServerSocketAddr(addr)

if err != nil {

log.Print("Error creating server socket: ", err.Error(), "\n")

return

}

// Start the server to listen for connections

log.Print("Starting the server for B communication (server->client) on ", addr, "\n")

err = serverTransport.Listen()

if err != nil {

log.Print("Error during B server: ", err.Error(), "\n")

return //err

}

// Accept new connections and handle those

for {

transport, err := serverTransport.Accept()

if err != nil {

return //err

}

if transport != nil {

// Each transport is handled in a goroutine so the server is availiable again.

go func() {

useTransport := framedTransportFactory.GetTransport(transport)

client := worldclient.NewWorldClientClientFactory(useTransport, protocolFactory)

// Thats it!

// Lets do something with the connction

result, err := client.Hello()

if err != nil {

log.Printf("Errror when calling Hello on client: %s\n", err)

}

// client.CallSomething()

}()

}

}

Java客戶端

// preparations for B connection

TTransportFactory transportFactory = new TTransportFactory();

TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();

YourServiceProcessor processor = new YourService.Processor(new YourServiceProcessor(this));

/* Create thrift connection for B calls (server -> client) */

try {

// create the transport

final TTransport transport = new TSocket("127.0.0.1", 9091);

// open the transport

transport.open();

// add framing to the transport layer

final TTransport framedTransport = new TFramedTransport(transportFactory.getTransport(transport));

// connect framed transports to protocols

final TProtocol protocol = protocolFactory.getProtocol(framedTransport);

// let the processor handle the requests in new Thread

new Thread() {

public void run() {

try {

while (processor.process(protocol, protocol)) {}

} catch (TException e) {

e.printStackTrace();

} catch (NullPointerException e) {

e.printStackTrace();

}

}

}.start();

} catch(Exception e) {

e.printStackTrace();

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的go将服务器图片响应给客户端,Go中来自客户端和服务器的RPC的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。