您好,欢迎访问一九零五行业门户网

Java网络编程由浅入深二 Socket的构造和连接服务端的相关异常 的详细介绍

socket构造方法和socket的设置与异常
本文将介绍socket的构造方法和socket的相关属性设置与异常处理
构造socket
设置等待超时时间
设置服务器地址
设置客户端地址
客户端连接服务器可能出现的异常
构造socketsocket的构造方法有如下几种重载方式:
socket();socket(inetaddress address, int port)socket(inetaddress address, int port, inetaddress localaddr, int localport)socket(proxy proxy)socket(socketimpl impl)socket(string host, int port) socket(string host, int port, inetaddress localaddr, int localport)
除了第一个构造器外,其他构造器都会尝试与服务器建立连接,如果连接成功返回socket对象;如果因为某些原因连接失败,就抛出ioexception。
如下代码扫描主机上从1到1024之间的端口,判断这些端口是否已经被服务器程序监听。
public class portscanner { public static void main(string[] args) { string host = "localhost"; new portscanner().scan(host); } public void scan(string host){ socket socket = null; for(int i=0;i<1024;i++){ try { socket = new socket(host, i); system.out.println("there is a server on port "+i); } catch (ioexception e) { system.out.println("can't connect to port "+i); }finally{ if(socket!=null){ try { socket.close(); } catch (ioexception e) { e.printstacktrace(); } } } } } }
设置等待建立连接的超时时间使用不带参数的构造方法,设置socket连接超时时间:
socket socket = new socket(); socketaddress endpoint = new inetsocketaddress("localhost", 8000);socket.connect(endpoint, 60000);
以上代码表示用于连接本机上监听的8000端口,等待连接的最长时间为1分钟。如果在1分钟内连接成功,则connect()方法顺利返回;如果在1分钟之内出现异常,则抛出异常;如果超过1分钟,既没有连接成功,也没有抛出异常,那么会抛出sockettimeoutexception。socket. connect(socketaddress endpoint, int timeout);负责连接服务器,参数endpoint指定服务器地址,参数timeout设定超时时间,以毫秒为单位。如果参数timeout为0,表示永远不超时。
设置服务器地址socket的构造方法中,除了第一个不带参数的构造方法,其他构造方法都需要指定服务器地址,包括服务器的ip或主机名,以及端口:
socket(inetaddress address, int port)
socket(string host, int port)
inetaddress类表示服务器的ip地址,inetaddress提供了很多静态方法:
// 返回本地主机的ip地址 inetaddress.getlocalhost(); // 返回代表10.202.164.65的ip地址 inetaddress.getbyname("10.202.164.65"); // 返回域名为'www.csdn.net'的ip地址 inetaddress.getbyname("www.csdn.net");
设置客户端的地址:默认情况下,客户端的ip地址来自于客户端程序所在的主机,客户端的端口则由操作系统随机分配。但是socket类还是提供了构造方法允许显式地设置客户端的ip和端口:
//参数localaddr和localport用来设置客户端的ip和端口。 socket(inetaddress address, int port, inetaddress localaddr, int localport) socket(string host, int port, inetaddress localaddr, int localport)
客户端连接服务器可能抛出的异常当socket构造方法请求连接服务器时,可能会抛出下面的异常:
• unknownhostexception:如果无法识别主机的名字或ip地址,就会抛出这种异常
• connectexception:如果没有服务器进程监听指定的端口,或者服务器拒绝连接,就会抛出这种异常。
• sockettimeoutexception:如果等待连接超时,就会抛出这种异常。
• bindexception:如果无法把socket对象与指定的本地ip地址或端口绑定,就会抛出这种异常。
通过下面测试类为例,演示抛出异常的原因。
public class connecttester { public static void main(string[] args) { string host = "www.csdn.net"; int port = 12312; new connecttester().connect(host, port); } public void connect(string host,int port){ socketaddress remoteaddress = new inetsocketaddress(host, port); socket socket = null; string result = null; try{ socket = new socket(); long start = system.currenttimemillis(); socket.connect(remoteaddress,1000); long end = system.currenttimemillis(); result = (end-start)+"ms"; }catch(bindexception bindexception){ result = "bindexception,socket对象与指定的本地ip地址或端口绑定,异常"; }catch (unknownhostexception unknownhostexception) { result = "unknownhostexception,无法识别的主机"; }catch (connectexception connectexception) { result = "connectexception,连接被拒绝"; }catch (sockettimeoutexception sockettimeoutexception) { result = "sockettimeoutexception,连接超时"; }catch(ioexception e){ result = "ioexception,连接失败"; }finally{ if(socket!=null){ try { socket.close(); } catch (ioexception e) { e.printstacktrace(); } } } system.out.println(remoteaddress+" : "+result); } }
• 抛出unknownhostexception情况:
如果无法识别主机的名字或ip地址,就会抛出这种异常。例如:host为:’ somehost11’。socket的connect方法就会抛出unknownhostexception异常。
• 抛出connectexception的情况:
在以下两种情况会抛出connectexception。
1) 没有服务器进程监听指定的端口。例如:host为 ‘localhost’ port为 12321 。如果本机的12321端口没有被任何进程监听,则socket连接方法会抛出connectexception。
2) 服务器进程拒绝连接。介绍服务器进程拒绝客户的连接请求的情形。如下示例代码,一个简单的服务程序serversocket构造方法中的第二个参数表示请求队列的长度。如果队列的请求已满,服务器就会拒绝其余的请求。抛出connectexception
public class simplserver { public static void main(string[] args) throws exception{ serversocket serversocket = new serversocket(8888, 2); thread.sleep(3600000); } }
public class simpleclient { public static void main(string[] args) throws exception{ string host = "localhost"; int port = 8888; socket s1 = new socket(host, port); system.out.println("第一次连接成功"); socket s2 = new socket(host, port); system.out.println("第二次连接成功"); socket s3 = new socket(host, port); system.out.println("第三次连接成功"); } }
• 抛出sockettimeoutexception的情形
如果客户端连接超时,就会抛出这种异常。修改 socket.connect(remoteaddress, 1);由原来的1000毫秒修改为1毫秒,这样增加了超时的可能性。
• 抛出bindexception的情形:
将代码
socket = new socket();socket.connect(remoteaddress, 1000);
修改为:
socket = new socket(); socket.bind(new inetsocketaddress(inetaddress.getbyname("222.34.5.6"), 5678)); socket.connect(remoteaddress, 1000);
修改后的代码试图把socket的本地ip地址设为222.34.5.6,把本地端口设置为5678。如果本机不具有该ip,或者端口被占用,那么就会出现bindexception。
以上就是java网络编程由浅入深二 socket的构造和连接服务端的相关异常 的详细介绍的内容。
其它类似信息

推荐信息