Question
Socket oddities
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
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
