简单的RPC服务器在我的客户端连接时不会回答。

huangapple go评论97阅读模式
英文:

Simple RPC server will not answer when my client connects

问题

我采用并改编了Golang RPC的示例,制作了一个简单的RPC服务器作为我想要实现的真实任务的测试运行。但是,无论我尝试了什么,最终客户端都会出现以下错误:

2014/03/21 13:28:41 dialing: dial-http tcp MYPC:61740: unexpected HTTP response: 404 Not Found

我尝试过使用MYPC:0127.0.0.1:61470MYPC:61470和其他类似的服务器变体。还尝试在本地和两台不同的计算机上运行(注意,我在Windows上使用的是Go 1.2)。通常情况下,使用Go进行调试是很简单的,但是即使阅读rpc包的源代码也无法帮助我解决这个问题-不过,我确实学到了使用:0来获取一个可用端口的技巧。

服务器可以通过运行可执行文件来运行,客户端可以使用服务器输出的端口运行chat -c -server=127.0.0.1:8082

以下代码有什么问题?

package main

import (
	"flag"
	"log"
	"net"
	"net/http"
	"net/rpc"
)

type Chat string

func (t *Chat) Msg(msg string, bytes *int) error {
	*bytes = len(msg)
	return nil
}

func main() {

	server := flag.String("server", "", "Server and port")
	client := flag.Bool("c", false, "Make me a client")
	flag.Parse()

	log.Println("Server: ", *server)

	if !(*client) {

		chat := new(Chat)
		rpc.Register(chat)
		l, e := net.Listen("tcp", *server)
		if e != nil {
			log.Fatal("listen error:", e)
		}
		log.Println(l.Addr().String())
		go rpc.Accept(l)
		http.Serve(l, nil)

	} else {

		log.Println("Client connecting to", *server)
		// !! The error occurs here
		client, err := rpc.DialHTTP("tcp", *server)
		if err != nil {
			log.Fatal("dialing: ", err)
		}

		var reply int
		err = client.Call("Chat.Msg", "Make it so!", &reply)
		if err != nil {
			log.Fatal("chat error:", err)
		}
		log.Println("Msg: returned", reply)
	}
}
英文:

I took and adapted the example from Golang RPC to make a simple RPC server as a test run for the real task I want to acheive. But whatever I have tried I end up with the client getting

2014/03/21 13:28:41 dialing: dial-http tcp MYPC:61740: unexpected HTTP response: 404 Not Found

I have tried using MYPC:0, 127.0.0.1:61470, MYPC:61470 and other variants of that theme for the server. Also have tried locally and on two different computers (Note I am using Go 1.2 on Windows). Usually with Go I find it straight-forward to debug, but even reading the source for the rpc pacakge is not helping this time - however I did pick up the trick of :0 giving an available port.

Server can be run by just running the exe, and Client can be run with the port output by the server chat -c -server=127.0.0.1:8082.

What is actually wrong with the following code?

package main
import (
"flag"
"log"
"net"
"net/http"
"net/rpc"
)
type Chat string
func (t *Chat) Msg(msg string, bytes *int) error {
*bytes = len(msg)
return nil
}
func main() {
server := flag.String("server", "", "Server and port")
client := flag.Bool("c", false, "Make me a client")
flag.Parse()
log.Println("Server: ", *server)
if !(*client) {
chat := new(Chat)
rpc.Register(chat)
l, e := net.Listen("tcp", *server)
if e != nil {
log.Fatal("listen error:", e)
}
log.Println(l.Addr().String())
go rpc.Accept(l)
http.Serve(l, nil)
} else {
log.Println("Client connecting to", *server)
// !! The error occurs here
client, err := rpc.DialHTTP("tcp", *server)
if err != nil {
log.Fatal("dialing: ", err)
}
var reply int
err = client.Call("Chat.Msg", "Make it so!", &reply)
if err != nil {
log.Fatal("chat error:", err)
}
log.Println("Msg: returned", reply)
}
}

答案1

得分: 2

我认为你在 rpc.Register(chat) 后面漏掉了 rpc.HandleHTTP()。尝试将你的代码改成以下形式:

...
rpc.Register(chat)
rpc.HandleHTTP()
...

这样对我来说是有效的,我得到了以下输出:

2014/03/20 18:31:21 服务器:  localhost:55209
2014/03/20 18:31:21 客户端连接到 localhost:55209
2014/03/20 18:31:21 消息: 返回了 11

而且你不需要 go rpc.Accept(l) 这一行。至少对我来说,没有这一行也能正常工作。

英文:

I think you're missing rpc.HandleHTTP() after rpc.Register(chat). Try changing your code to

...
rpc.Register(chat)
rpc.HandleHTTP()
...

It worked for me this way, I got

2014/03/20 18:31:21 Server:  localhost:55209
2014/03/20 18:31:21 Client connecting to localhost:55209
2014/03/20 18:31:21 Msg: returned 11

And you don't need go rpc.Accept(l). At least for me it worked without this line.

huangapple
  • 本文由 发表于 2014年3月21日 08:38:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/22548024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定