What does End Of File on a socket mean?
Asked Answered
H

2

6

Using Action Script 3 in Flex Builder 3.

When handling a SOCKET_DATA event, I occasionally, seemingly at random, get an "Error #2030: End of file was encountered." when calling socket.readInt(). I'm confused as to what this error means, since I'm not reading a file? I'm a little unfamilier with sockets. Thanks.

Hardigg answered 24/8, 2009 at 10:37 Comment(0)
C
4

When reading off a socket, that is closed, you will get: Error #2002: Operation attempted on invalid socket.

End-of-file errors typically occur on any bytestreams, if you read more bytes than available. This is the case for files, sockets, etc. In the case of flash, it occurs when reading from a Socket or a ByteArray and maybe even in other cases.

TCP/IP is package based, but emulates a stream, thus you can only read the data off the stream, that was already sent to you with TCP packages. Check Socket::bytesAvailable to find out, how many bytes are currently available. Always keep in mind, that the data you write to the socket in one operation, may arrive in multiple packages, each very probably causing flash player to trigger socketData events.

Cultivate answered 24/8, 2009 at 12:14 Comment(1)
Just followup for anyone else with this issue. I think it is because I have been only checking that bytesAvailable is > 0, which I had seen from someone else's code, but then doing readInt which would be 4 bytes. So if there were only 1 to 3 bytes of data, readInt would hypothetically fail in some way.Hardigg
V
12

An end-of-file error typically means the other side of the socket has closed their connection, IIRC.

The reason it's end-of-file is that at a very low level within a program, a file on the disk and a socket are both represented with a number -- a file descriptor -- that the OS translates into the object representing a file or socket or pipe or whatever.

Usually, you can avoid this kind of error by checking if you just read in an EOF. If you did read an EOF, and you try reading from the socket/file again, then you will get an EOF error.


Update: According to the ActionScript 9.0 documentation, you do indeed get a close event if the other end closes the socket.
Valdez answered 24/8, 2009 at 11:0 Comment(1)
Thanks, should I get a SOCKET_CLOSE event or similar if the other side closes the connection?Hardigg
C
4

When reading off a socket, that is closed, you will get: Error #2002: Operation attempted on invalid socket.

End-of-file errors typically occur on any bytestreams, if you read more bytes than available. This is the case for files, sockets, etc. In the case of flash, it occurs when reading from a Socket or a ByteArray and maybe even in other cases.

TCP/IP is package based, but emulates a stream, thus you can only read the data off the stream, that was already sent to you with TCP packages. Check Socket::bytesAvailable to find out, how many bytes are currently available. Always keep in mind, that the data you write to the socket in one operation, may arrive in multiple packages, each very probably causing flash player to trigger socketData events.

Cultivate answered 24/8, 2009 at 12:14 Comment(1)
Just followup for anyone else with this issue. I think it is because I have been only checking that bytesAvailable is > 0, which I had seen from someone else's code, but then doing readInt which would be 4 bytes. So if there were only 1 to 3 bytes of data, readInt would hypothetically fail in some way.Hardigg

© 2022 - 2024 — McMap. All rights reserved.