caoprom在线,97香蕉久久夜色精品国产,天天夜夜亚洲http://www.aygfsteel.com/leekiang/category/34765.htmlMDA/MDD/TDD/DDD/DDDDDDDzh-cnSun, 18 Apr 2010 22:13:49 GMTSun, 18 Apr 2010 22:13:49 GMT60Adding Socket Timeout to java.net.URLConnection (JDK 1.2)http://www.aygfsteel.com/leekiang/archive/2010/04/14/318278.htmlleekiangleekiangWed, 14 Apr 2010 09:09:00 GMThttp://www.aygfsteel.com/leekiang/archive/2010/04/14/318278.htmlhttp://www.aygfsteel.com/leekiang/comments/318278.htmlhttp://www.aygfsteel.com/leekiang/archive/2010/04/14/318278.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/318278.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/318278.htmlAdding Socket Timeout to java.net.URLConnection (JDK 1.2)

found a bug , see "connected = true;" in public void connect() throws IOException {


Note: 05/11/01 Sam Found a patch for borland

As I got the email:

Just writing to inform you theis patch for 1.3 works with the 1.3 shipped with borland JBuilder 4 (not sure which excat version it is)

the only problems I had where that the code was a bit messed up, following are the changes made to it to make it work.

				
public void SetTimeout(int i)
throws SocketException
{
this.timeout = i; // Should be i not -1 <------------ERROR
serverSocket.setSoTimeout(i) ;
}

public boolean parseHTTP(MessageHeader header, ProgressEntry entry)
throws java.io.IOException
{
if( this.timeout != -1 ) {
try {
serverSocket.setSoTimeout(timeout) ; // should be timeout not i <---------------ERROR
} catch( SocketException e ) {
throw new java.io.IOException("unable to set socket timeout!") ;
}
}

return super.parseHTTP(header, entry) ;
}
Sam

Under JDK 1.3, which is HTTP 1.1 compatible, the InterruptedIOException gets caught by the socket I/O routines and ignored. input is read in "chunks". I debugged the existing code under 1.3, the Timeout is getting set properly etc., but the exception gets caught in the underlying I/O routines, which have a single retry if any IOExceptions are thrown. Thanks a lot Sun....

3/22/01: Patch for JDK 1.3 unverified

Patch code for JDK 1.3 from Matt Ho (unverified)

				
----[ snip ]----

import sun.net.www.MessageHeader ;
import sun.net.ProgressEntry ;

.
.
.

private int timeout = -1 ;

public void SetTimeout(int i)
throws SocketException
{
this.timeout = -1 ;
serverSocket.setSoTimeout(i) ;
}

public boolean parseHTTP(MessageHeader header, ProgressEntry entry)
throws java.io.IOException
{
if( this.timeout != -1 ) {
try {
serverSocket.setSoTimeout(i) ;
} catch( SocketException e ) {
throw new java.io.IOException("unable to set socket timeout!") ;
}
}

return super.parseHTTP(header, entry) ;
}

----[ snip ]----

On with the rest of the stuff

The BSD socket API supports a timeout option (the option is SO_TIMEOUT), which is also supported in java.net.socket. Unfortunately, java.net.URLConnection does not expose the underlying socket. So if you have a URL connection that attempts to connect to a dead URL (i.e., the URL is well formed and exists but the site is down), the socket will eventually timeout using the operating system's default timeout (420 seconds on Win NT). The timeout is a very long time, e.g., for spiders or URL checking.

The following files illustrate a technique to introduce a socket timeout to URL connection, based upon the actual java source code itself (see the open source community licensing at JavaSoft).

The Base classes, or URLConnection internals

Java's implementation of networking is protocol independent, as well as object oriented. Therefore the implementation is not as straightfoward as one might imagine.

URLConnection relies upon several internal classes using a client/server model as well as a "factory" design pattern. The client's base class is sun.net.www.http.HttpClient. This class is extended for the purpose of exposing the socket.

The default factory is URLStreamHandlerFactory, which indirectly "handles" the creation of an HTTP client by instantiating a class that is specific to the HTTP protocol: sun.net.www.protocol.http.Handler. The handler actually creates the client.

In practice, the factory is only necessary to mimic java's implementation, but only the Handler is really needed.

Derived Classes

We derive 4 classes so as to preserve the symmetry with the java source code:

HttpURLConnectionTimeout extends sun.net.www.protocol.http.HttpURLConnection
HttpTimeoutHandler extends sun.net.www.protocol.http.Handler
HttpTimeoutFactory implements java.net.URLStreamHandlerFactory
HttpClientTimeout extends sun.net.www.http.HttpClient

On with the source code.


HttpURLConnectionTimeout

// whatever package you want
import sun.net.www.http.HttpClient;
import java.net.*;
import java.io.*;
public class HttpClientTimeout extends HttpClient
{
public HttpClientTimeout(URL url, String proxy, int proxyPort) throws IOException
{
super(url, proxy, proxyPort);
}

public HttpClientTimeout(URL url) throws IOException
{
super(url, null, -1);
}

public void SetTimeout(int i) throws SocketException {
serverSocket.setSoTimeout(i);
}

/* This class has no public constructor for HTTP. This method is used to
* get an HttpClient to the specifed URL. If there's currently an
* active HttpClient to that server/port, you'll get that one.
*
* no longer syncrhonized -- it slows things down too much
* synchronize at a higher level
*/
public static HttpClientTimeout GetNew(URL url)
throws IOException {
/* see if one's already around */
HttpClientTimeout ret = (HttpClientTimeout) kac.get(url);
if (ret == null) {
ret = new HttpClientTimeout (url); // CTOR called openServer()
} else {
ret.url = url;
}
// don't know if we're keeping alive until we parse the headers
// for now, keepingAlive is false
return ret;
}

public void Close() throws IOException
{
serverSocket.close();
}

public Socket GetSocket()
{
return serverSocket;
}


}

HttpTimeoutFactory

import java.net.*;

public class HttpTimeoutFactory implements URLStreamHandlerFactory
{
int fiTimeoutVal;
public HttpTimeoutFactory(int iT) { fiTimeoutVal = iT; }
public URLStreamHandler createURLStreamHandler(String str)
{
return new HttpTimeoutHandler(fiTimeoutVal);
}

}

HttpTimeoutHandler

import java.net.*;
import java.io.IOException;

public class HttpTimeoutHandler extends sun.net.www.protocol.http.Handler
{
int fiTimeoutVal;
HttpURLConnectionTimeout fHUCT;
public HttpTimeoutHandler(int iT) { fiTimeoutVal = iT; }

protected java.net.URLConnection openConnection(URL u) throws IOException {
return fHUCT = new HttpURLConnectionTimeout(u, this, fiTimeoutVal);
}

String GetProxy() { return proxy; } // breaking encapsulation
int GetProxyPort() { return proxyPort; } // breaking encapsulation

public void Close() throws Exception
{
fHUCT.Close();
}

public Socket GetSocket()
{
return fHUCT.GetSocket();
}
}

HttpURLConnectionTimeout

import java.net.*;
import java.io.*;
import sun.net.www.http.HttpClient;

public class HttpURLConnectionTimeout extends sun.net.www.protocol.http.HttpURLConnection
{
int fiTimeoutVal;
HttpTimeoutHandler fHandler;
HttpClientTimeout fClient;
public HttpURLConnectionTimeout(URL u, HttpTimeoutHandler handler, int iTimeout) throws IOException
{
super(u, handler);
fiTimeoutVal = iTimeout;
}

public HttpURLConnectionTimeout(URL u, String host, int port) throws IOException
{
super(u, host, port);
}

public void connect() throws IOException {
if (connected) {
return;
}
try {
if ("http".equals(url.getProtocol()) /* && !failedOnce <- PRIVATE */ ) {
// for safety's sake, as reported by KLGroup
synchronized (url)
{
http = HttpClientTimeout.GetNew(url);
}
fClient = (HttpClientTimeout)http;
((HttpClientTimeout)http).SetTimeout(fiTimeoutVal);
} else {
// make sure to construct new connection if first
// attempt failed
http = new HttpClientTimeout(url, fHandler.GetProxy(), fHandler.GetProxyPort());
}
ps = (PrintStream)http.getOutputStream();
} catch (IOException e) {
throw e; }
// this was missing from the original version
connected = true;
}

/**
* Create a new HttpClient object, bypassing the cache of
* HTTP client objects/connections.
*
* @param url the URL being accessed
*/
protected HttpClient getNewClient (URL url)
throws IOException {
HttpClientTimeout client = new HttpClientTimeout (url, (String)null, -1);
try {
client.SetTimeout(fiTimeoutVal);
} catch (Exception e)
{ System.out.println("Unable to set timeout value"); }
return (HttpClient)client;
}

/**
* opens a stream allowing redirects only to the same host.
*/
public static InputStream openConnectionCheckRedirects(URLConnection c)
throws IOException
{
boolean redir;
int redirects = 0;
InputStream in = null;

do {
if (c instanceof HttpURLConnectionTimeout) {
((HttpURLConnectionTimeout) c).setInstanceFollowRedirects(false);
}

// We want to open the input stream before
// getting headers, because getHeaderField()
// et al swallow IOExceptions.
in = c.getInputStream();
redir = false;

if (c instanceof HttpURLConnectionTimeout) {
HttpURLConnectionTimeout http = (HttpURLConnectionTimeout) c;
int stat = http.getResponseCode();
if (stat >= 300 && stat <= 305 &&
stat != HttpURLConnection.HTTP_NOT_MODIFIED) {
URL base = http.getURL();
String loc = http.getHeaderField("Location");
URL target = null;
if (loc != null) {
target = new URL(base, loc);
}
http.disconnect();
if (target == null
|| !base.getProtocol().equals(target.getProtocol())
|| base.getPort() != target.getPort()
|| !HostsEquals(base, target)
|| redirects >= 5)
{
throw new SecurityException("illegal URL redirect");
}
redir = true;
c = target.openConnection();
redirects++;
}
}
} while (redir);
return in;
}

// Same as java.net.URL.hostsEqual


static boolean HostsEquals(URL u1, URL u2)
{
final String h1 = u1.getHost();
final String h2 = u2.getHost();

if (h1 == null) {
return h2 == null;
} else if (h2 == null) {
return false;
} else if (h1.equalsIgnoreCase(h2)) {
return true;
}
// Have to resolve addresses before comparing, otherwise
// names like tachyon and tachyon.eng would compare different
final boolean result[] = {false};

java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
try {
InetAddress a1 = InetAddress.getByName(h1);
InetAddress a2 = InetAddress.getByName(h2);
result[0] = a1.equals(a2);
} catch(UnknownHostException e) {
} catch(SecurityException e) {
}
return null;
}
});

return result[0];
}

void Close() throws Exception
{
fClient.Close();
}

Socket GetSocket()
{
return fClient.GetSocket();
}
}

Sample Usage #1

import java.net.*;
public class MainTest
{

public static void main(String args[])
{
int i = 0;
try {
URL theURL = new URL((URL)null, "http://www.snowball.com", new HttpTimeoutHandler(150)); // timeout value in milliseconds

// the next step is optional
theURL.setURLStreamHandlerFactory(new HttpTimeoutFactory(150));


URLConnection theURLconn = theURL.openConnection();
theURLconn.connect();
i = theURLconn.getContentLength();
}
catch (InterruptedIOException e)
{
System.out.println("timeout on socket");
}
System.out.println("Done, Length:" + i);
}
}

Sample Usage #2

		try
{
HttpTimeoutHandler xHTH = new HttpTimeoutHandler(10); // timeout value in milliseconds
URL theURL = new URL((URL)null, "http://www.javasoft.com", xHTH);
HttpURLConnection theUC = theURL.openConnection();
.
.
.
}
catch (InterruptedIOException e)
{
// socket timed out

}

Some remarks: this code is thread safe.

More to come

來源:http://www.logicamente.com/sockets.html

???? http://www.edevs.com/java-programming/15068/


Thanks Felipe!

If I understand information at http://www.logicamente.com/sockets.html correctly there are 2 problems with timeout when using HttpURLConnection in JDK 1.3:

1. HttpURLConnection does not allow changing the default timeout that is in order of few minutes.

2. If actual HTTP stream is chunked then HttpURLConnection ignores even the default timeout and tries to read what it perceives as a continued stream resulting in indefinite read wait.

The patch shown at the above URL, consisting of subclassing of 4 system classes (1 from java.net... and 3 from sun.net.www...), is aimed to resolve problem 1 above but does not help in problem 2.

My main problem is to have timeout when reading chunked stream (system default timeout will be ok to beginning with) and therefore the question is if this bug has been corrected in later versions of JDK? Thanks.

-----

I have seen much chat about this "problem", that is setSoTimeout not available or not working properly.

how about you write your own Timer (resettable) or 1.4 has Timer class

you just reset it anytime you detect network activity and close the Socket if the Timer finishes its cycle?



leekiang 2010-04-14 17:09 發(fā)表評論
]]>
HttpURLConnection timeout solutionhttp://www.aygfsteel.com/leekiang/archive/2010/04/14/318274.htmlleekiangleekiangWed, 14 Apr 2010 09:04:00 GMThttp://www.aygfsteel.com/leekiang/archive/2010/04/14/318274.htmlhttp://www.aygfsteel.com/leekiang/comments/318274.htmlhttp://www.aygfsteel.com/leekiang/archive/2010/04/14/318274.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/318274.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/318274.htmlFrom: Niels Campbell (niels_campbell_at_lycos.co.uk)
Date: 01/23/04
Date: 23 Jan 2004 09:14:16 -0800
After spending nearly 3 days on this problem to come up with a

solution I think it is only right to post the solution.

I found that you can't set the soTimeout on an HttpURLConnection as
the sockets are encapsulated within the HttpURLConnection
implementation.

I found Mike Reiche solution in which he uses a handler to set a
timeout value. This nearly worked. Looking at the code in the rt.jar I
found that the initial timeout was working, but the call
parseHTTP(...) in HttpClient was then attempting a second connection
which had a time out value of 0(infinite).

I modified the code to override the doConnect() in the NetworkClient
and managed to get a timeout occurring. To be exact two timeouts
occur.

It works on
java version "1.4.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_03-b04)
Java HotSpot(TM) Client VM (build 1.4.0_03-b04, mixed mode)
and
java version "1.2.2"
Classic VM (build JDK-1.2.2_013, native threads, symcjit)

Anyway here is the code, excuse the formatting.

/* HttpTimeoutURLConnection.java */
import java.net.*;
import java.io.*;
import sun.net.www.http.HttpClient;

// Need to override any function in HttpURLConnection that create a
new HttpClient
// and create a HttpTimeoutClient instead. Those functions are
// connect(), getNewClient(), getProxiedClient()

public class HttpTimeoutURLConnection extends
sun.net.www.protocol.http.HttpURLConnection
{

????public HttpTimeoutURLConnection(URL u, HttpTimeoutHandler handler,
int iSoTimeout)
????????throws IOException
????{
????????super(u, handler);
????????HttpTimeoutClient.setSoTimeout(iSoTimeout);
????}

????public void connect() throws IOException
????{
????????if (connected)
????????{
????????????return;
????????}

????????try
????????{
????????????if ("http".equals(url.getProtocol())) // && !failedOnce <-
PRIVATE
????????????{
????????????????// for safety's sake, as reported by KLGroup
????????????????synchronized (url)
????????????????{
????????????????????http = HttpTimeoutClient.getNew(url);
????????????????}
????????????}
????????????else
????????????{
????????????????if (handler instanceof HttpTimeoutHandler)
????????????????{
????????????????????http = new HttpTimeoutClient(super.url,
((HttpTimeoutHandler)handler).getProxy(),
((HttpTimeoutHandler)handler).getProxyPort());
????????????????}
????????????????else
????????????????{
????????????????????throw new IOException("HttpTimeoutHandler
expected");
????????????????}
????????????}

????????????ps = (PrintStream)http.getOutputStream();
????????}
????????catch (IOException e)
????????{
????????????throw e;
????????}

????????connected = true;
????}

????protected HttpClient getNewClient(URL url)
????????throws IOException
????{
????????HttpTimeoutClient httpTimeoutClient = new HttpTimeoutClient
(url, (String)null, -1);
????????return httpTimeoutClient;
????}

????protected HttpClient getProxiedClient(URL url, String s, int i)
????????throws IOException
????{
????????HttpTimeoutClient httpTimeoutClient = new HttpTimeoutClient
(url, s, i);
????????return httpTimeoutClient;
????}

}

/* HttpTimeoutHandler.java */
import java.net.*;
import java.io.IOException;

public class HttpTimeoutHandler extends
sun.net.www.protocol.http.Handler
{
????private int iSoTimeout=0;

????public HttpTimeoutHandler(int iSoTimeout)
????{
????????// Divide the time out by two because two connection attempts
are made
????????// in HttpClient.parseHTTP()

????????if (iSoTimeout%2!=0)
????????{
????????????iSoTimeout++;
????????}
????????this.iSoTimeout = (iSoTimeout/2);
????}

????protected java.net.URLConnection openConnection(URL u) throws
IOException
????{
????????return new HttpTimeoutURLConnection(u, this, iSoTimeout);
????}

????protected String getProxy()
????{
????????return proxy;
????}

????protected int getProxyPort()
????{
????????return proxyPort;
????}
}

/* HttpTimeoutFactory.java */
import java.net.*;

public class HttpTimeoutFactory implements URLStreamHandlerFactory
{
????private int iSoTimeout=0;

????public HttpTimeoutFactory(int iSoTimeout)
????{
????????this.iSoTimeout = iSoTimeout;
????}

????public URLStreamHandler createURLStreamHandler(String str)
????{
????????return new HttpTimeoutHandler(iSoTimeout);
????}
}

/* HttpTimeoutClient.java */
import sun.net.www.http.HttpClient;
import java.net.*;
import sun.net.*;
import sun.net.www.*;
import java.io.*;

public class HttpTimeoutClient extends HttpClient
{
????private static int iSoTimeout=0;

????public HttpTimeoutClient(URL url, String proxy, int proxyPort)
throws IOException
????{
????????super(url, proxy, proxyPort);
????}

????public HttpTimeoutClient(URL url) throws IOException
????{
????????super(url, null, -1);
????}

????public static HttpTimeoutClient getNew(URL url)
????????throws IOException
????{
????????HttpTimeoutClient httpTimeoutClient = (HttpTimeoutClient)
kac.get(url);

????????if (httpTimeoutClient == null)
????????{
????????????httpTimeoutClient = new HttpTimeoutClient (url); // CTOR
called openServer()
????????}
????????else
????????{
????????????httpTimeoutClient.url = url;
????????}

????????return httpTimeoutClient;
????}

????public static void setSoTimeout(int iNewSoTimeout)
????{
????????iSoTimeout=iNewSoTimeout;
????}

????public static int getSoTimeout()
????{
????????return iSoTimeout;
????}

????// Override doConnect in NetworkClient

????protected Socket doConnect(String s, int i)
????????throws IOException, UnknownHostException, SocketException
????{
????????Socket socket=super.doConnect(s,i);

????????// This is the important bit
????????socket.setSoTimeout(iSoTimeout);
????????return socket;
????}

}

/* Example use */
import java.util.*;
import java.io.*;
import java.net.*;

public class SystemProperty
{
????public static void main(String[] args)
????{
????????String sSoapUrl=" ????????System.out.println("Connecting to [" + sSoapUrl + "]");

????????URLConnection urlConnection = null;
????????URL url=null;

????????try
????????{
????????????url = new URL((URL)null, sSoapUrl, new
HttpTimeoutHandler(10000));
????????????urlConnection = url.openConnection();

????????????// Optional
????????????url.setURLStreamHandlerFactory(new
HttpTimeoutFactory(10000));

????????????System.out.println("Url class
["+urlConnection.getClass().getName()+"]");
????????}
????????catch (MalformedURLException mue)
????????{
????????????System.out.println(">>MalformedURLException<<");
????????????mue.printStackTrace();
????????}
????????catch (IOException ioe)
????????{
????????????System.out.println(">>IOException<<");
????????????ioe.printStackTrace();
????????}

????????HttpURLConnection httpConnection =
(HttpURLConnection)urlConnection;
????????System.out.println("Connected to [" + sSoapUrl + "]");

????????byte[] messageBytes=new byte[10000];
????????for (int i=0; i<10000; i++)
????????{
????????????messageBytes[i]=80;
????????}

????????try
????????{
????????????httpConnection.setRequestProperty("Connection", "Close");
????????????httpConnection.setRequestProperty("Content-Length",
String.valueOf(messageBytes.length));
????????????httpConnection.setRequestProperty("Content-Type",
"text/xml; charset=utf-8");
????????????httpConnection.setRequestMethod("POST");
????????????httpConnection.setDoOutput(true);
????????????httpConnection.setDoInput(true);
????????}
????????catch (ProtocolException pe)
????????{
????????????System.out.println(">>ProtocolException<<");
????????????pe.printStackTrace();
????????}

????????OutputStream outputStream=null;

????????try
????????{
????????????System.out.println("Getting output stream");
????????????outputStream =httpConnection.getOutputStream();
????????????System.out.println("Got output stream");

????????????outputStream.write(messageBytes);
????????}
????????catch (IOException ioe)
????????{
????????????System.out.println(">>IOException<<");
????????????ioe.printStackTrace();
????????}

????????try
????????{
????????????System.out.println("Getting input stream");
????????????InputStream is=httpConnection.getInputStream();
????????????System.out.println("Got input stream");

????????????byte[] buf = new byte[1000];
????????????int i;

????????????while((i = is.read(buf)) > 0)
????????????{
????????????????System.out.println(""+new String(buf));
????????????}
????????????is.close();
????????}
????????catch (Exception ie)
????????{
????????????ie.printStackTrace();
????????}

????}
}

Cheers,
Niels

來源:http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2004-01/3271.html
???? http://www.weblogicfans.net/viewthread.php?tid=1101
???? http://forums.sun.com/thread.jspa?threadID=568948
備注:在HttpTimeoutClient類中的第二個構(gòu)造函數(shù)中的:super(url,null,-1)改為super(url, (String)null,-1)即可。


leekiang 2010-04-14 17:04 發(fā)表評論
]]>
HttpURLConnection設(shè)置網(wǎng)絡(luò)超時http://www.aygfsteel.com/leekiang/archive/2010/04/13/318185.htmlleekiangleekiangTue, 13 Apr 2010 11:00:00 GMThttp://www.aygfsteel.com/leekiang/archive/2010/04/13/318185.htmlhttp://www.aygfsteel.com/leekiang/comments/318185.htmlhttp://www.aygfsteel.com/leekiang/archive/2010/04/13/318185.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/318185.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/318185.htmlHttpURLConnection對象不能直接構(gòu)造,需要通過 URL.openConnection()來獲得HttpURLConnection對象,示例代碼如下:
String urlStr= www.ttt.org;
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();

?

HttpURLConnection是基于HTTP協(xié)議的,其底層通過socket通信實 現(xiàn)。如果不設(shè)置超時(timeout),在網(wǎng)絡(luò)異常的情況下,可能會導(dǎo)致程序僵死而不繼續(xù)往下執(zhí)行。可以通過以下兩個語句來設(shè)置相應(yīng)的超時:
System.setProperty("sun.net.client.defaultConnectTimeout", 超時毫秒數(shù)字符串);
System.setProperty("sun.net.client.defaultReadTimeout", 超時毫秒數(shù)字符串);

其中: sun.net.client.defaultConnectTimeout:連接主機的超時時間(單位:毫秒)
sun.net.client.defaultReadTimeout: 從主機讀取數(shù)據(jù)的超時時間(單位:毫秒)

例如:
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");

JDK 1.5以前的版本,只能通過設(shè)置這兩個系統(tǒng)屬性來控制網(wǎng)絡(luò)超時。在1.5中,還可以使用HttpURLConnection的父類 URLConnection的以下兩個方法:
setConnectTimeout:設(shè)置連接主機超時(單位:毫秒)
setReadTimeout: 設(shè)置從主機讀取數(shù)據(jù)超時(單位:毫秒)

例如:
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
urlCon.setConnectTimeout(30000);
urlCon.setReadTimeout(30000);

來源:http://www.xd-tech.com.cn/blog/article.asp?id=37

另外可參考java中處理http連接超時的方法

JDK中的URLConnection參數(shù)詳解

linux下設(shè)置connect連接超時的方法

java socket 用法(一)

Linux,可以修改/proc/sys/net/ipv4/tcp_syn_retries的值,缺省是72,大約5分鐘左右,改小點時間就短些



leekiang 2010-04-13 19:00 發(fā)表評論
]]>
網(wǎng)絡(luò)協(xié)議筆記http://www.aygfsteel.com/leekiang/archive/2009/03/22/261336.htmlleekiangleekiangSun, 22 Mar 2009 10:31:00 GMThttp://www.aygfsteel.com/leekiang/archive/2009/03/22/261336.htmlhttp://www.aygfsteel.com/leekiang/comments/261336.htmlhttp://www.aygfsteel.com/leekiang/archive/2009/03/22/261336.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/261336.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/261336.htmlOSI七層模型是一個很好的理論模型,但是在實際應(yīng)用中都做了裁剪。尤其是TCP/IP的盛行,把7層結(jié)構(gòu)壓成了4層,
所以很多人都批評OSI七層模型過于復(fù)雜,但是作為一個完整的全面的網(wǎng)絡(luò)模型,還是被大家非常認可的。OSI的7層從上到下分別是應(yīng)用層、表示層、會話層、傳輸層、網(wǎng)絡(luò)層、數(shù)據(jù)鏈路層、物理層。
7層的功能描述:
(1) 應(yīng)用層:與其他計算機進行通訊的一個應(yīng)用,它是對應(yīng)應(yīng)用程序的通信服務(wù)的。例如,一個沒有通信功能的字處理程序就不能執(zhí)行通信的代碼,從事字處理工作的程 序員也不關(guān)心OSI的第7層。但是,如果添加了一個傳輸文件的選項,那么字處理器的程序員就需要實現(xiàn)OSI的第7層。示 例:telnet,HTTP,FTP,WWW,NFS,SMTP等。
(2)表示層:這一層的主要功能是定義數(shù)據(jù)格式及加密。例如,F(xiàn)TP允許 你選擇以二進制或ASII格式傳輸。如果選擇二進制,那么發(fā)送方和接收方不改變文件的內(nèi)容。如果選擇ASII格式,發(fā)送方將把文本從發(fā)送方的字符集轉(zhuǎn)換成 標準的ASII后發(fā)送數(shù)據(jù)。在接收方將標準的ASII轉(zhuǎn)換成接收方計算機的字符集。示例:加密,ASII等。
(3)會話層:他定義了如何開始、控制和結(jié)束一個會話,包括對多個雙向小時的控制和管理,以便在只完成連續(xù)消息的一部分時可以通知應(yīng)用,從而使表示層看到的數(shù)據(jù)是連續(xù)的,在某些情況下,如果表示層收到了所有的數(shù)據(jù),則用數(shù)據(jù)代表表示層。示例:RPC,SQL等。
(4)傳輸層:這層的功能包括是否選擇差錯恢復(fù)協(xié)議還是無差錯恢復(fù)協(xié)議,及在同一主機上對不同應(yīng)用的數(shù)據(jù)流的輸入進行復(fù)用,還包括對收到的順序不對的數(shù)據(jù)包的重新排序功能。示例:TCP,UDP,SPX。
(5)網(wǎng)絡(luò)層:這層對端到端的包傳輸進行定義,他定義了能夠標識所有結(jié)點的邏輯地址,還定義了路由實現(xiàn)的方式和學(xué)習(xí)的方式。為了適應(yīng)最大傳輸單元長度小于包長度的傳輸介質(zhì),網(wǎng)絡(luò)層還定義了如何將一個包分解成更小的包的分段方法。示例:IP,IPX等。
(6)數(shù)據(jù)鏈路層:他定義了在單個鏈路上如何傳輸數(shù)據(jù)。這些協(xié)議與被討論的歌種介質(zhì)有關(guān)。示例:ATM,F(xiàn)DDI等。
(7)物理層:OSI的物理層規(guī)范是有關(guān)傳輸介質(zhì)的特性標準,這些規(guī)范通常也參考了其他組織制定的標準。連接頭、針、針的使用、電流、電流、編碼及光調(diào)制等都屬于各種物理層規(guī)范中的內(nèi)容。物理層常用多個規(guī)范完成對所有細節(jié)的定義。示例:Rj45,802.3等。
來源:http://blog.csdn.net/wanghao72214/archive/2009/03/18/4000806.aspx


leekiang 2009-03-22 18:31 發(fā)表評論
]]>
java io筆記http://www.aygfsteel.com/leekiang/archive/2009/03/17/260275.htmlleekiangleekiangTue, 17 Mar 2009 08:32:00 GMThttp://www.aygfsteel.com/leekiang/archive/2009/03/17/260275.htmlhttp://www.aygfsteel.com/leekiang/comments/260275.htmlhttp://www.aygfsteel.com/leekiang/archive/2009/03/17/260275.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/260275.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/260275.htmlFile.separatorChar 返回一個字符,表示當前系統(tǒng)默認的文件名分隔符,在Windows中為"\",unix中為"/"
File.separator 與前者相同,但將分隔符作為字符串類型返回。
pathSeparatorChar 返回一個字符,表示當前系統(tǒng)默認的路徑名分隔符,在Windows中為";",unix中為":"
File.pathSeparator 與前者相同,但將分隔符作為字符串類型返回。

leekiang 2009-03-17 16:32 發(fā)表評論
]]>
主站蜘蛛池模板: 德庆县| 兰州市| 沾化县| 绥德县| 无极县| 乌兰县| 江西省| 裕民县| 兴隆县| 安庆市| 抚顺县| 鄂州市| 莆田市| 邵阳县| 上高县| 泗阳县| 通化县| 诏安县| 聊城市| 涡阳县| 梅河口市| 承德县| 山阳县| 连平县| 安新县| 保亭| 万安县| 勐海县| 长顺县| 昌图县| 四会市| 伊宁市| 台湾省| 崇州市| 苏尼特左旗| 海门市| 延津县| 乌拉特后旗| 仙桃市| 合山市| 惠州市|