Java.net.SocketException:No buffer space available (maximum connections reached?): Connect__.net

Source: Internet
Author: User

When you recently used httpclient in your project, you encountered this problem with the following exception information:

Info: Retrying Connect
2013-1-23 16:36:31 Org.apache.http.impl.client.DefaultRequestDirector Tryconnect
Info: I/O exception (java.net.SocketException) caught when connecting to the target Host:no buffer space available (maximum Connections reached?): Connect
2013-1-23 16:36:31 Org.apache.http.impl.client.DefaultRequestDirector Tryconnect
Info: Retrying Connect
Java.net.SocketException:No buffer space available (maximum connections reached?): Connect
At Java.net.PlainSocketImpl.socketConnect (Native method)
At Java.net.PlainSocketImpl.doConnect (plainsocketimpl.java:351)
At Java.net.PlainSocketImpl.connectToAddress (plainsocketimpl.java:213)
At Java.net.PlainSocketImpl.connect (plainsocketimpl.java:200)
At Java.net.SocksSocketImpl.connect (sockssocketimpl.java:366)
At Java.net.Socket.connect (socket.java:529)
At Org.apache.http.conn.scheme.PlainSocketFactory.connectSocket (plainsocketfactory.java:127)
At Org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection ( defaultclientconnectionoperator.java:180)
At Org.apache.http.impl.conn.ManagedClientConnectionImpl.open (managedclientconnectionimpl.java:294)
At Org.apache.http.impl.client.DefaultRequestDirector.tryConnect (defaultrequestdirector.java:645)
At Org.apache.http.impl.client.DefaultRequestDirector.execute (defaultrequestdirector.java:480)
At Org.apache.http.impl.client.AbstractHttpClient.execute (abstracthttpclient.java:906)
At Org.apache.http.impl.client.AbstractHttpClient.execute (abstracthttpclient.java:805)
At Org.apache.http.impl.client.AbstractHttpClient.execute (abstracthttpclient.java:784)
At Com.yulore.json.MysqlService.parseJsonShop (mysqlservice.java:118)
At COM.YULORE.JSON.MYSQLSERVICE.WRITETOMYSQL (mysqlservice.java:68)
At Com.yulore.json.MysqlMainTest.main (mysqlmaintest.java:13)
Com.mysql.jdbc.CommunicationsException:Communications link failure due to underlying exception:


* * BEGIN NESTED EXCEPTION * *


Java.net.SocketException
Message:no buffer space available (maximum connections reached?): Connect


StackTrace:


Java.net.SocketException:No buffer space available (maximum connections reached?): Connect
At Java.net.PlainSocketImpl.socketConnect (Native method)
At Java.net.PlainSocketImpl.doConnect (plainsocketimpl.java:351)
At Java.net.PlainSocketImpl.connectToAddress (plainsocketimpl.java:213)
At Java.net.PlainSocketImpl.connect (plainsocketimpl.java:200)
At Java.net.SocksSocketImpl.connect (sockssocketimpl.java:366)
At Java.net.Socket.connect (socket.java:529)
At Java.net.Socket.connect (socket.java:478)
At Java.net.socket.<init> (socket.java:375)
At Java.net.socket.<init> (socket.java:218)
At Com.mysql.jdbc.StandardSocketFactory.connect (standardsocketfactory.java:256)
At Com.mysql.jdbc.mysqlio.<init> (mysqlio.java:271)
At Com.mysql.jdbc.Connection.createNewIO (connection.java:2771)
At Com.mysql.jdbc.connection.<init> (connection.java:1555)
At Com.mysql.jdbc.NonRegisteringDriver.connect (nonregisteringdriver.java:285)
At Java.sql.DriverManager.getConnection (drivermanager.java:582)
At Java.sql.DriverManager.getConnection (drivermanager.java:185)
At Com.yulore.json.db.DBManager.getConnectionWithJDBC (dbmanager.java:39)
At Com.yulore.json.db.impl.GlossaryDaoImpl.insert (glossarydaoimpl.java:20)
At COM.YULORE.JSON.MYSQLSERVICE.WRITETOMYSQL (mysqlservice.java:89)
At Com.yulore.json.MysqlMainTest.main (mysqlmaintest.java:13)




* * End NESTED EXCEPTION * *


Finally, Google and Baidu found that they created the httpget to send a GET request, and finally did not release the connection, the corrected code as follows

Private list<shop> parsejsonshop (String url) {
		httpclient httpclient = new Defaulthttpclient ();
		HttpGet httpget = new HttpGet (URL);
		HttpResponse response;
		try {
			response = Httpclient.execute (httpget);
			httpentity entity = response.getentity ();

			if (entity!= null) {

				InputStream in = Entity.getcontent ();

				String result = convertstreamtostring (in);
				In.close ();

				Jsonobject obj = new Jsonobject (result);

				Jsonobject resp = obj.getjsonobject ("response");
				Numfound = Resp.getlong ("Numfound");
				System.out.println ("numfound=" +numfound);

				Jsonarray docs = Resp.getjsonarray ("docs");
				String docs = resp.getstring ("docs");
				System.out.println ("docs=" +docs);

				list<shop> shoplist = Json.parsearray (docs, shop.class);
				Return shoplist
			}
		} catch (Exception e) {
			e.printstacktrace ();
		} finally {
			httpget.releaseconnection ();//Free Connection
		} return
		null;
	}


When you finish using HttpGet, remember to release the connection, or it will continue to occupy resources. In order to ensure that the connection will be released it is recommended to put it in the finally phrase.


One answer to the Sun's Technical forum is this:
Chances are you are forgetting to close a socket, a database connection, or some other connection this uses sockets Ally. Example program below.
The alternative is this your program (or the sum of all programs running on your computer) really needs a lot of connectio Ns. In this case you'll need to the increase the amount of socket buffer spaces that your operating system allocate S. I ' d start by Googling for instructions. Or Maybe you could redesign your program so, that it doesn ' t is very much.


For more answers to this question, see here:
http://forum.java.sun.com/thread.jspa?threadID=556382



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.