Go connection.Writer and reader not behaving properly, reading 2 write in one read operation

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

Go connection.Writer and reader not behaving properly, reading 2 write in one read operation

问题

我正在尝试从客户端读取文件,然后将其发送到服务器。

操作如下:在客户端程序中输入send <fileName>,然后将会将<fileName>发送到服务器。服务器通过TCP连接从客户端读取两个内容,第一个是命令send <fileName>,第二个是文件的内容。

然而,有时候我的程序会随机将文件内容包含在<fileName>字符串中。例如,假设我有一个名为xyz.txt的文本文件,其中的内容是“Hello world”。服务器有时会接收到send xyz.txtHello world。有时候不会出现这个问题,一切正常。

我认为这可能是同步或者读写缓冲区未刷新的问题,但我不太确定。提前感谢!

客户端代码:

func sendFileToServer(fileName string, connection net.Conn) {

	fileBuffer := make([]byte, BUFFER_SIZE)
	var err error

	file, err := os.Open(fileName) // For read access.

	lock := make(chan int)
	w := bufio.NewWriter(connection)

	go func(){
		w.Write([]byte("send " + fileName))
		w.Flush()
		lock <- 1
	}()

	<-lock
	// make a read buffer
	r := bufio.NewReader(file)

	//read file until there is an error
	for err == nil || err != io.EOF {
		//read a chunk
		n, err := r.Read(fileBuffer)
		if err != nil && err != io.EOF {
			panic(err)
		}
		if n == 0 {
			break
		}
		// write a chunk
		if _, err := w.Write(fileBuffer[:n]); err != nil {
			panic(err)
		}
	}
	file.Close()
	connection.Close()
	fmt.Println("Finished sending.")
}

服务器代码:(connectionHandler是一个为每个来自客户端的TCP连接请求调用的goroutine)

func connectionHandler(connection net.Conn, bufferChan chan []byte, stringChan chan string) {
	buffer := make([]byte, 1024)

	_, error := connection.Read(buffer)
	if error != nil {
		fmt.Println("There is an error reading from connection", error.Error())
		stringChan <- "failed"
		return 
	}
	fmt.Println("command recieved: " + string(buffer))
	if("-1"==strings.Trim(string(buffer), "\x00")){
		stringChan <- "failed"
		return
	}

	arrayOfCommands := strings.Split(string(buffer)," ")
	arrayOfCommands[1] = strings.Replace(arrayOfCommands[1],"\n","", -1)
	fileName := strings.Trim(arrayOfCommands[1], "\x00")	

	if arrayOfCommands[0] == "get" {
        fmt.Println("Sending a file " + arrayOfCommands[1])
		sendFileToClient(fileName, connection, bufferChan, stringChan)
	} else if arrayOfCommands[0] == "send" {
		fmt.Println("Getting a file " + arrayOfCommands[1])
		getFileFromClient(fileName, connection, bufferChan, stringChan)
	} else {
		_, error = connection.Write([]byte("bad command"))
	}
    fmt.Println("connectionHandler finished")
}


func getFileFromClient(fileName string, connection net.Conn,bufferChan chan []byte, stringChan chan string) { //put the file in memory
	stringChan <- "send"
	fileBuffer := make([]byte, BUFFER_SIZE)

	var err error
	r := bufio.NewReader(connection)

	for err == nil || err != io.EOF {
		//read a chunk
		n, err := r.Read(fileBuffer)
		if err != nil && err != io.EOF {
			panic(err)
		}
		if n == 0 {
			break
		}
		bufferChan <- fileBuffer[:n]
		stringChan <- fileName
	}

	connection.Close()
	return 

}

希望对你有帮助!

英文:

I am trying to read a file from client and then send it to server.

It goes like this, you input send &lt;fileName&gt; in the client program, then &lt;fileName&gt; will be sent to server. The server read 2 things from the client via TCP connection, first the command send &lt;fileName&gt; and second the content of the file.

However, sometimes my program will randomly include the file content in the &lt;fileName&gt; string. For example, say I have a text file called xyz.txt, the content of which is "Hellow world". The server sometimes receive send xyz.txtHellow world. Sometimes it doesn't and it works just fine.

I think that it is the problem of synchronization or not flushing reader/writer buffer. But I am not quite sure.
Thanks in advance!

Client code:

func sendFileToServer(fileName string, connection net.Conn) {
fileBuffer := make([]byte, BUFFER_SIZE)
var err error
file, err := os.Open(fileName) // For read access.
lock := make(chan int)
w := bufio.NewWriter(connection)
go func(){
w.Write([]byte(&quot;send &quot; + fileName))
w.Flush()
lock &lt;- 1
}()
&lt;-lock
// make a read buffer
r := bufio.NewReader(file)
//read file until there is an error
for err == nil || err != io.EOF {
//read a chunk
n, err := r.Read(fileBuffer)
if err != nil &amp;&amp; err != io.EOF {
panic(err)
}
if n == 0 {
break
}
// write a chunk
if _, err := w.Write(fileBuffer[:n]); err != nil {
panic(err)
}
}
file.Close()
connection.Close()
fmt.Println(&quot;Finished sending.&quot;)
}

Server code: (connectionHandler is a goroutine that is invoked for every TCP connection request from client)

func connectionHandler(connection net.Conn, bufferChan chan []byte, stringChan chan string) {
buffer := make([]byte, 1024)
_, error := connection.Read(buffer)
if error != nil {
fmt.Println(&quot;There is an error reading from connection&quot;, error.Error())
stringChan&lt;-&quot;failed&quot;
return 
}
fmt.Println(&quot;command recieved: &quot; + string(buffer))
if(&quot;-1&quot;==strings.Trim(string(buffer), &quot;\x00&quot;)){
stringChan&lt;-&quot;failed&quot;
return
}
arrayOfCommands := strings.Split(string(buffer),&quot; &quot;)
arrayOfCommands[1] = strings.Replace(arrayOfCommands[1],&quot;\n&quot;,&quot;&quot;,-1)
fileName := strings.Trim(arrayOfCommands[1], &quot;\x00&quot;)	
if arrayOfCommands[0] == &quot;get&quot; {
fmt.Println(&quot;Sending a file &quot; + arrayOfCommands[1])
sendFileToClient(fileName, connection, bufferChan, stringChan)
} else if arrayOfCommands[0] == &quot;send&quot; {
fmt.Println(&quot;Getting a file &quot; + arrayOfCommands[1])
getFileFromClient(fileName, connection, bufferChan, stringChan)
} else {
_, error = connection.Write([]byte(&quot;bad command&quot;))
}
fmt.Println(&quot;connectionHandler finished&quot;)
}
func getFileFromClient(fileName string, connection net.Conn,bufferChan chan []byte, stringChan chan string) { //put the file in memory
stringChan&lt;-&quot;send&quot;
fileBuffer := make([]byte, BUFFER_SIZE)
var err error
r := bufio.NewReader(connection)
for err == nil || err != io.EOF {
//read a chunk
n, err := r.Read(fileBuffer)
if err != nil &amp;&amp; err != io.EOF {
panic(err)
}
if n == 0 {
break
}
bufferChan&lt;-fileBuffer[:n]
stringChan&lt;-fileName
}
connection.Close()
return 
}

答案1

得分: 1

TCP是一种流协议。它没有消息的概念。网络可以自由地按字节逐个发送数据,也可以一次性发送所有数据(在一定限制范围内)。即使你幸运地收到了按照你期望的数据包发送的数据,接收端仍然可以将这些数据包连接成一个缓冲区。

换句话说,没有任何机制能够保证每次Read调用返回的字节数与某些特定的Write调用写入的字节数相同。有时候你会幸运,有时候(正如你所注意到的)你可能不会那么幸运。如果没有错误发生,从流中进行的所有读取操作将返回你写入的所有字节,这是你唯一的保证。

你需要定义一个适当的协议。

这与Go语言无关。任何编程语言都会以这种方式运作。

英文:

TCP is a stream protocol. It doesn't have messages. The network is (within some limits we don't need to concern us about) free to send your data one byte at a time or everything at once. And even if you get lucky and the network sends your data in packets like you want them there's nothing that prevents the receive side from concatenating the packets into one buffer.

In other words: there is nothing that will make each Read call return as many bytes as you wrote with some specific Write calls. You sometimes get lucky, sometimes, as you noticed, you don't get lucky. If there are no errors, all the reads you do from the stream will return all the bytes you wrote, that's the only guarantee you have.

You need to define a proper protocol.

This is not related to Go. Every programming language will behave this way.

huangapple
  • 本文由 发表于 2017年2月7日 18:01:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/42086859.html
匿名

发表评论

匿名网友

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

确定