wood burning stoves*
The moose likes General Computing and the fly likes Socket only works sometimes, i get 'connection reset by peer' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Socket only works sometimes, i get Watch "Socket only works sometimes, i get New topic
Author

Socket only works sometimes, i get 'connection reset by peer'

lawrence clojure
Greenhorn

Joined: Sep 03, 2012
Posts: 2
I wanted to test the speed of sending a file directly from memory. I grabbed a page off wikipedia, it is about 500k. I compile it as a static resource, as part of a simple app that listens on a ServerSocket and sends the page to any client that connects.

If I compile this code and run it on localhost then everything works fine. I can launch this at the command line, get it running on port 3457, and then point my browser at port 3457, and I see the article.

Then I decided to give it a real world test. I rent 2 servers in the Rackspace cloud. I uploaded this app to one server and, again, started it at the command line, this time at port 40000 (no particular reason for that port, I also tried ports 9000 and 6000). Then I ssh to the other server and, from there, I ping the first server with wget. And I get constant problems with "connection reset by peer".

Here is most of the core.clj file:

(def page-string
(with-open [stream (clojure.java.io/input-stream (clojure.java.io/resource "mitochondria.html"))]
(let [buf (BufferedReader. (InputStreamReader. stream))]
(clojure.string/join "\n" (line-seq buf)))))

(defn start-thread [f]
(doto (new Thread f) (.start)))

(defn serve-page [wrt client]
(let [content-length (count page-string)]
(. wrt println "HTTP/1.1 200 OK")
(. wrt println "Content-Type: text/html; charset=UTF-8")
(. wrt println "Connection: Keep-Alive")
(. wrt println (str "Content-Length: " content-length))
(. wrt print "\r\n")
(. wrt print page-string)
(. wrt flush)
(. wrt close)
(. client close)))

(defn create-server [s]
(try
(while (not (. s (isClosed)))
(let [client (. s accept)]
(start-thread #(let [wrt (new PrintWriter (new BufferedWriter (new OutputStreamWriter (. client (getOutputStream)))))]
(try
(serve-page wrt client)
(catch SocketException e
(. System/out (println "Caught: " (.getMessage e)))
"failure"))))))
(catch IOException ioe
(. System/out (println "Caught: " (.getMessage ioe)))
"failure")))

(defn -main [& args]
(println "Server is starting")
(let [port (Integer/parseInt (first args))
ss (new ServerSocket port)]
(println "port: " port)
(try
(create-server ss)
(catch SocketException se
(. System/out (println "Caught: " (.getMessage se)))
"failure"))))

In an attempt to find the problem, I have started wrapping everything in exceptions. But none of these exceptions ever print out at the command line, either because there are no exceptions or because the app no longer writes to stdout after it is running.

The problems in wget look like this:

wget -O tma.html --force-html http://www.tailormadeanswers.com:40000


--2012-09-02 22:17:20-- http://www.tailormadeanswers.com:40000/
Resolving www.tailormadeanswers.com... 184.106.135.172
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

91% [==================================> ] 472,401 --.-K/s in 0.04s

2012-09-02 22:17:20 (10.4 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:21-- (try: 2) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

82% [===============================> ] 426,065 --.-K/s in 0.04s

2012-09-02 22:17:21 (10.8 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:23-- (try: 3) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

86% [================================> ] 446,337 --.-K/s in 0.04s

2012-09-02 22:17:23 (11.4 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:26-- (try: 4) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

83% [===============================> ] 428,961 --.-K/s in 0.04s

2012-09-02 22:17:26 (10.9 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:30-- (try: 5) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

83% [===============================> ] 431,857 --.-K/s in 0.04s

2012-09-02 22:17:30 (11.0 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:35-- (try: 6) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

80% [==============================> ] 415,929 --.-K/s in 0.04s

2012-09-02 22:17:35 (10.6 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:41-- (try: 7) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

80% [==============================> ] 417,377 --.-K/s in 0.04s

2012-09-02 22:17:41 (10.7 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:48-- (try: 8) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

91% [==================================> ] 469,505 --.-K/s in 0.04s

2012-09-02 22:17:48 (11.5 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:17:56-- (try: 9) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

80% [==============================> ] 417,377 --.-K/s in 0.04s

2012-09-02 22:17:56 (11.1 MB/s) - Read error at byte 472401/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:18:05-- (try:10) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

92% [===================================> ] 478,193 --.-K/s in 0.04s

2012-09-02 22:18:05 (11.8 MB/s) - Read error at byte 478193/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:18:15-- (try:11) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

97% [====================================> ] 501,361 --.-K/s in 0.04s

2012-09-02 22:18:15 (12.6 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:18:25-- (try:12) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

92% [==================================> ] 475,297 --.-K/s in 0.04s

2012-09-02 22:18:25 (12.2 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:18:35-- (try:13) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

82% [===============================> ] 424,617 --.-K/s in 0.04s

2012-09-02 22:18:36 (11.1 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:18:46-- (try:14) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

81% [==============================> ] 421,721 --.-K/s in 0.04s

2012-09-02 22:18:46 (10.6 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:18:56-- (try:15) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

80% [==============================> ] 414,481 --.-K/s in 0.04s

2012-09-02 22:18:56 (10.6 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:19:06-- (try:16) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

78% [=============================> ] 404,345 --.-K/s in 0.04s

2012-09-02 22:19:06 (10.3 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:19:16-- (try:17) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

83% [===============================> ] 431,321 --.-K/s in 0.04s

2012-09-02 22:19:16 (10.8 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:19:26-- (try:18) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

80% [==============================> ] 413,033 --.-K/s in 0.04s

2012-09-02 22:19:26 (10.5 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:19:36-- (try:19) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

80% [==============================> ] 415,929 --.-K/s in 0.03s

2012-09-02 22:19:36 (11.7 MB/s) - Read error at byte 501361/515592 (Connection reset by peer). Retrying.

--2012-09-02 22:19:46-- (try:20) http://www.tailormadeanswers.com:40000/
Connecting to www.tailormadeanswers.com|184.106.135.172|:40000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 515592 (504K) [text/html]
Saving to: `tma.html'

100%[======================================>] 515,592 --.-K/s in 0.04s

2012-09-02 22:19:46 (11.7 MB/s) - `tma.html' saved [515592/515592]




That is 20 attempts before the file was finally served correctly. Please note, these 2 server instances are in the same datacenter so the connection speed is very high.

Most of the time what happens is that most of the file gets served and then the socket is cut off somehow.

Any thoughts about what might be happening?

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This is not directly related to Clojure, so I'm moving the thread to General Computing.


[My Blog]
All roads lead to JavaRanch
lawrence clojure
Greenhorn

Joined: Sep 03, 2012
Posts: 2
Christophe Verré, the code is in Clojure. What exactly is the Clojure forum for? All the same, if someone knows an answer, I am happy to cast a wide net.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

As far as I can see, you have problems with wget. Not with Clojure. (and you'll probably get much more help here )
 
 
subject: Socket only works sometimes, i get 'connection reset by peer'
 
Similar Threads
Apache CXF and Properties file
Two sites with Tomcat 6 and Apache 2.2 (Problem)
Can't get ImageIcon to show up from captcha
HttpsConnection not establishing?
each request I got different token for one endpoint, but another endpoint, i got the same token.