Skip to main content
Participant
March 22, 2006
Question

Socket oddities

  • March 22, 2006
  • 3 replies
  • 824 views
Hi all,

I'm wondering if anybody has had any experience working with Sockets in Bridge and can explain some odd behaviour that I am seeing.

I am implementing a simple mechanism for retrieving a file from a remote server using HTTP (GET). With the request, I noticed that if the connection is opened using the default "ASCII" encoding, Socket.write does not send any CR characters. ie.

sock.write("GET /foo/bar HTTP/1.1\r\n");

sends the message "GET /foo/bar HTTP/1.1\n". With the "binary" encoding, everything goes through ok. Is the behaviour that I am seein for ASCII correct? I do not believe that \r should be dropped.

With respect to the response, I am trying to read the data line by line using Socket.readln. The first read will retrieve the status line

HTTP/1.1 200 OK

but I can't read anything else (regardless of connection encoding). After readln is called, socket.connected becomes false (expected - the server sends all data and a normal TCP connection shutdown occurs; I've verified this with a packet sniffer). The unexpected thing is that socket.eof is true! Somehow, the read buffer has become empty eventhough all data has been sent. Again, I've only called readln once. Is this a bug in Socket?

The code that I'm using to test retrieval of data is:

var sock = new Socket();
sock.open("myserver:80", "binary");
sock.write(request);

$.writeln("!socket error : " + sock.error);
$.writeln("!socket connected: " + sock.connected);
$.writeln("!socket eof : " + sock.eof);

var res = "";
while (sock.connected || !sock.eof)
{
var line = sock.readln();
res = res + "\r\n" + line;
$.writeln("!socket error : " + sock.error);
$.writeln("!socket connected: " + sock.connected);
$.writeln("!socket eof : " + sock.eof);
}
$.writeln("result: " + res);

Output looks like this:

!socket error :
!socket connected: true
!socket eof : false
!socket error :
!socket connected: false
!socket eof : true
result:
HTTP/1.1 200 OK

Cheers,

liam
This topic has been closed for replies.

3 replies

Participant
April 11, 2006
I took a look at the socket object documentation from GoLive; I belive that there was a post on this forum from Bob Stucky about Bridge implementing that object.

It's good to know I'm not crazy :) I've filed a bug report regarding the read issue. I haven't tried actually sending any binary data, but the behaviour you're seeing doesn't seem right.

liam
Known Participant
April 7, 2006
How did you get socket to work with the bridge? I've only gotten it to work under after effects...

I've had luck sending GETs like this:

conn.write("GET /loginform.php HTTP/1.1\nHost: somewhere.somewhereelse.com:88\nConnection: Keep-Alive\n\n");

(without the wordwrap, of course...)

That being said, I've noticed similar things. readln() only reads one line, then it thinks there's nothing else. I haven't figured out how to stop it from doing that. Also, the data I'm reading in is chunked, so I have to do a loop until my read() comes up empty. Currently I'm messing with read(1) to just get a character at a time, but it stops after the first chunk and doesn't see any more. Not very helpful, I know, but at least you aren't crazy. :)

Have you tried sending binary data (jpegs, for instance)? Since read() puts all the data in a string, even though the open mode is set to "binary" my data is getting garbled. I'd love to know how to stop that from happening...

-Rich
Participant
March 22, 2006
I should say that I am using Bridge 1.0.3.107 on Windows XP Pro SP2. I do not have a Mac so I can't check behaviour on that platform.

liam