Chris Blades

Greenhorn
+ Follow
since Dec 23, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Chris Blades

The above suggestions about following the 302 redirection response is correct.

My suggestion, though is to reformat you're http requests like so:

end each line with \c\r

end the request with \c\r\c\r

so the code would be something like this (pseudo):

write("GET index.html HTTP/1.1\c\r"); //end line with \c\r
write("User-agent: mozilla\c\r\c\r"); //end request wiht \c\r\c\r

this is compliant with http docs and some servers complain about it (not many, but it's annoying when it happens).

However, be aware that it's not uncommon for servers to use \n in responses, so they may use a \n\n to terminate their http header. I use a regular expression to match either \c\r\c\r or \n\n.
The OS will close dead connections automatically, but it could take some time (relative to how fast operations should happen on a computer). A good example, though not with files (same concept though), is ports. Use a socket to connect to a port on your own computer, don't close the connection but exit the program. Immediately re-run the program and you'll sometimes get an IOException because the socket is still blocked.

The same applies to files, it's just that there can be multiple processes accessing a file at once. There is a limit though.
15 years ago
I think you may have stdin and stdout backwards. I looked up Process in the API and it is quite vague, so I can't say for sure. From my understanding, Process.getOutputStream() returns stdout for the process and .getInputStream() returns stdin for the process.

-Chris

[ December 24, 2008: Message edited by: Chris Blades ]

edit: close() flushes automatically.
[ December 24, 2008: Message edited by: Chris Blades ]
15 years ago
They both essentially do the same thing. The process the JVM goes through when making a new object and assigning a reference to it is:
-allocate space for reference on stack or heap (depending on location); the variable will be null at this point
-initialize object on heap
-initialize reference with object's address

so wether you explicitly do:


or:



the jvm is doing the exact same number of steps in the exact same order, so use whichever you feel is most readable.
15 years ago
Thanks, I was thinking something along those lines might be it. I've been playing around with the exceptions to see if I could narrow it down and associate specific exceptions with specific types of failures (by which I mean a failure to connect).

What I'm playing with at the moment is catching a SocketTimeoutException. This appears to be thrown when using connect(host, timeout) and the host doesn't return RST. However, the results I'm getting seem a bit off (some ports that I'm sure should be returning RST just aren't and vice versa), so I'm thinking I'm either catching the exceptions in the wrong order (as like you said there is a lengthy hierarchy) or there would be a more useful exception to catch in this case.

Does anyone know if connect will return normally without throwing an exception if RST is recieved? Or will it continue to try to connect?

I'll post any solution I come up with.

-Chris
The host you are connecting to is closing the stream (or you are) while there is still data in the buffer, thus the read exception. This causes RST to be sent, which is causing the connection reset exception.
The problem I'm running into is that I need to determine how connect() failed. In other words, I need to know if the remote host acknowledged with FIN causing the connection to fail or if there was no response at all.

I believe that if connect() blocks, then the remote host did not respond; however, if the remote host responds with FIN, the socket will still not be connected, but connect should return anyway (and possibly throw an IOException?).

The java API is pretty skimpy on this, and I've not been able to find the answer to this anywhere else.


Thanks in advance.

-Chris

p.s. I've never looked for this, but does java have the equivilant of a raw socket? That would solve my problem as well(by compicating it ), and could be handy anyway.
[ December 24, 2008: Message edited by: Chris Blades ]