EchoServer(Java)未将消息返回给客户端(golang)

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

EchoServer (Java) not returning message back to client (golang)

问题

我正在尝试创建一个TCP客户端(使用Go语言)和服务器(使用Java语言)的应用程序,其中客户端写入数据,服务器将该文本回显并将消息返回给客户端,然后客户端再次回显回复。

服务器代码(Java):

public static void main(String[] args) throws Exception {
    int port = 4444;
    ServerSocket serverSocket = new ServerSocket(port);
    System.err.println("Started server on port " + port);

    while (true) {
        Socket clientSocket = serverSocket.accept();
        System.err.println("Accepted connection from client");

        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        String s;
        while ((s = in.readLine()) != null) {
            out.println(s);
            System.out.println(s);
        }

        System.err.println("Closing connection with client");
        out.close();
        in.close();
        clientSocket.close();
    }
}

客户端代码(Go):

package main

import (
    "net"
    "os"
)

func main() {
    strEcho := "Hello"
    servAddr := "localhost:4444"
    tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
    if err != nil {
        println("ResolveTCPAddr failed:", err.Error())
        os.Exit(1)
    }

    conn, err := net.DialTCP("tcp", nil, tcpAddr)
    if err != nil {
        println("Dial failed:", err.Error())
        os.Exit(1)
    }

    _, err = conn.Write([]byte(strEcho))
    if err != nil {
        println("Write to server failed:", err.Error())
        os.Exit(1)
    }

    println("write to server =", strEcho)

    reply := make([]byte, 1024)
    _, err = conn.Read(reply)
    if err != nil {
        println("Write to server failed:", err.Error())
        os.Exit(1)
    }

    println("reply from server=", string(reply))

    conn.Close()
}

当我启动Java服务器并运行Go应用程序时,服务器会显示已接受连接(即"Accepted connection from client"),但从未将回显返回给客户端。客户端在"write to server = Hello"处停顿,即写入服务器正常工作,但读取客户端的回复却没有成功。有任何想法为什么会发生这种情况吗?

英文:

I am trying to create a TCP client (golang) server (Java) application where the client writes, the server echos this text and returns the message back to the client, which subsequently echos the reply.

Server code (Java):

public static void main(String[] args) throws Exception {

	int port = 4444;
	ServerSocket serverSocket = new ServerSocket(port);
	System.err.println("Started server on port " + port);

	while (true) {
		Socket clientSocket = serverSocket.accept();
		System.err.println("Accepted connection from client");

		BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

		PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
		String s;
		while ((s = in.readLine()) != null) {
			out.println(s);
			System.out.println(s);
			
		}

		System.err.println("Closing connection with client");
		out.close();
		in.close();
		clientSocket.close();
	}
}

Client code (golang):

package main

import (
	"net"
	"os"
)

func main() {
	strEcho := "Hello"
	servAddr := "localhost:4444"
	tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
	if err != nil {
		println("ResolveTCPAddr failed:", err.Error())
		os.Exit(1)
	}

	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	if err != nil {
		println("Dial failed:", err.Error())
		os.Exit(1)
	}

	_, err = conn.Write([]byte(strEcho))
	if err != nil {
		println("Write to server failed:", err.Error())
		os.Exit(1)
	}

	println("write to server = ", strEcho)

	reply := make([]byte, 1024)
	_, err = conn.Read(reply)

	if err != nil {
		println("Write to server failed:", err.Error())
		os.Exit(1)
	}

	println("reply from server=", string(reply))

	conn.Close()
}

When I start the Java server and then run the Go application, the server says that the connection has been accepted (i.e. "Accepted connection from client") but never returns the echo back to the client. The client stalls on "write to server = Hello" i.e. the write to the server works fine, but the read back to the client does not. Any idea why this is happening?

答案1

得分: 2

尝试发送一个换行符以匹配服务器端的readLine语句

strEcho := "Hello\n"
英文:

Try sending a newline character to match the readLine statement on the server-side

strEcho := "Hello\n"

huangapple
  • 本文由 发表于 2015年2月2日 07:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/28268997.html
匿名

发表评论

匿名网友

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

确定