dongzhong9055 2016-05-25 16:07
浏览 82
已采纳

在Windows OS上使用Go和Python实施Client-Server模型

I am working on an GUI application, the UI is developed using python(+kivy) and the core is implemented using GoLang.

My Application involves passing data from UI to Core, for which i am using pipes. Following is the snippet of Client and Server code.

Client.py:

p = win32pipe.CreateNamedPipe(r'\\.\pipe\test_pipe',
                              win32pipe.PIPE_ACCESS_DUPLEX,
                              win32pipe.PIPE_TYPE_MESSAGE |win32pipe.PIPE_WAIT,
                              1, 65536, 65536,300,None)

win32pipe.ConnectNamedPipe(p, None)


data = "Hello Pipe"  
win32file.WriteFile(p, bytes(data,"UTF-8")) 

Server.go:

ln, err := npipe.Listen(`\\.\pipe\test_pipe`)
if err != nil {
    // handle error
}


for {
    conn, err := ln.Accept()
    if err != nil {
        // handle error
        continue
    }

    // handle connection like any other net.Conn
    go func(conn net.Conn) {
        r := bufio.NewReader(conn)
        msg, err := r.ReadString('
')
        if err != nil {
            // handle error
            return
        }
        fmt.Println(msg)

    }(conn)
}

With the above piece of code, i am not able to setup a connection between them. My Application involves duplex communication between client and server

Any sort of help is appreciated!!

  • 写回答

1条回答 默认 最新

  • doulin8374 2016-05-27 13:23
    关注

    i Could not find a solution using pipes, so i moved to sockets and i am able to communicate via sockets. Following is the snippets of the codes

    Client.py

    import socket
    import struct
    
    # create a socket object
    requestCore = socket.socket(
                    socket.AF_INET, socket.SOCK_STREAM) 
    responseCore = socket.socket(
                    socket.AF_INET, socket.SOCK_STREAM) 
    
    # get local machine name
    host = socket.gethostname()                           
    port = 8100                                           
    
    # bind to the port
    requestCore.bind((host, port))                                  
    # queue up to 5 requests
    requestCore.listen(5)                                           
    client,addr = requestCore.accept()
    
    port = 9999
    responseCore.connect((host, port))
    
    while True:
        msg = input("User Input: ")
        client.send(bytes(msg.encode('UTF-8')))
    
        msgFrmServ = responseCore.recv(64)
        print("message from Server",msgFrmServ)
    
    client.close()
    responseCore.close()
    

    server.go

    package main 
    
    import "net" 
    import "fmt" 
    import "bufio" 
    import "os"
    import "bytes"
    
    func main() {   
    
        hostname,_ := os.Hostname()
    
        // connect to this socket   
        readconn, _ := net.Dial("tcp", hostname+":8100")   
        reader := bufio.NewReader(readconn)     
    
        ln, _ := net.Listen("tcp", hostname+":9999")   
        writeconn, _ := ln.Accept()    // accept connection on port   
    
        for { 
            text := make([]byte,64)
            reader.Read(text)
            text = bytes.Trim(text,"\x00")
            fmt.Println(string(text))
    
            Acktext := "Core Acknowledge: " + string(text)
            writeconn.Write([]byte(Acktext))
        } 
    }
    

    The above snippet works fine for my application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境