From: Jean-Hugues ROBERT Date: 2002-04-29T03:44:50+09:00 Subject: Re: Inconsistent network behaviour Hello, What you are experiencing is not an inconsistency, it is normal. The reason is that TCP is a stream oriented protocol, not a block oriented protocol. As a result there is no guarantee that multiple send() will result in multiple recv(), it pretty much depends on the buffering of the TCP stack and timing. As a consequence: Never rely on .recv() matching .sent() to break stream into pieces, it often works, but sometimes does not. In your case .gets() will do a better job. BTW: I am implementing some SIP in Ruby. Early stage so far. Yours, Jean-Hugues At 01:36 29/04/2002 +0900, you wrote: >Hi all, > >I'm a Ruby newbie when it comes to network programming so while writing >a little example to teach myself it I came across this rather >inconsistent behaviour. Firstly here are my two scripts. One is a server >which listens on a port and the other a client which sends it simple >messages. > >#client.rb >require "socket" >session = TCPSocket.new("localhost",5179) >session.send("INVITE sip:you@bar.com SIP/2.0\n",0) >session.send("From: me@foo.com\n",0) >session.send("To: you@bar.com\n",0) >session.send("\r\n",0) >session.close > >#server.rb >require "socket" >def parse_header(socket) > data = "" > connec = Hash.new > while (data != "\r\n") > data = socket.recv(1000) > print "Data read: ",data > property,value = data.split(/: /) > connec[property]=value > end > connec >end >server = TCPServer.new("localhost",5179) >while (session=server.accept) > connection = Hash.new > data = session.recv(100) > if (data.split)[0] == "INVITE" > connection = parse_header(session) > end >end > >When I run the server on my Linux box (Mandrake 8.1, Kernel 2.4.8, Ruby >1.6.4) I get the desired output: > >Data read: me@foo.com >Data read: you@bar.com > >When I run it on a FreeBSD box (FreeBSD 4.4,Ruby 1.6.3) the output is: > >Data read: me@foo.com >To: you@bar.com > >The program reads the two lines (sent using two seperate send method >calls) in with one recv, as well as the final "\r\n" which causes it to >then go into an infinite loop (printing out Data read: Data read: Data >read: ...) as it keeps on reading in from the socket even though it is >closed on the client side. > >This is a simple exercise so I'm sure there's a solution, just that I >can't find anything in the pickaxe book or online docs. Any ideas what >the problem is and why the behaviour is different on Linux and FreeBSD? > >Thanks in advance, >Farrel Lifson >-- >|DNA Research Group mailto:flifson@cs.uct.ac.za >|Dept. of Computer Science http://www.cs.uct.ac.za/~flifson >|University of Cape Town +27-21-650-3127 ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17