网络编程笔记1

TCPtransmission cotrol proctocol UDP(user data proctocol)

     Socket 两个Java应用程序可通过一个双向的网络通信连接实现数据交换,这个双向链路的一端称为一个Socket.

     Socket通常用来实现client-server连接。

     java.net包中定义的两个类SocketServerSocket,分别用来实现双向连接的clientserver

     建立连接时所需的寻址信息为远程计算机的IP地址和端口号(Port number)

     Socket   The class implements client sockets (also called just  “sockets”). A socket is an endpoint for communication betwween two machines. The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.

public Socket(String host, int port)  Creates a stream socket and connects it to the specified port number on the named host.If the specified host is null it is equivalent of specifying the address as InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface. If the application has specified a server socket fatory, that factory’s createSocketImpl method is called to create the actual socket implementation. Otherwise s “plain” socket is created.

If there is a security mananger, its checkConnect method is called with the host address and port as its arguments.This could result in a SecurityException.

public OutputStream getOutputStream() throws IOException

Returns an output stream for this socket. If this socket has an associated channel then the resulting output stream delegates all of its opearations to the channel.If the channel is in non-blocking mode then the output stream’s write opearionts will throw an Exception.

11/27/2009 22:43:59

     public class InetAddress extends Object implements Serializable

This class represents an Internet Protocol(IP) address. An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built, The IP address architecture is defined by RFC790: An instance of an InetAddress consists of an IP address and possibly its corresponding host name (depending on whether it is constucted with with a host name or whether it has already done reverse host name relolution).

 

     ServerSocket   The class implements server sockets, A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester. The actual work of the server socket is performed by an instance of the SocketImpl class. An application can change the socket fatory that creates the socket implementation to configure itselt to cretate sockets appropriate to the local firewall.

 

 

     ServerSocket

public Socket accept()   Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made .A new Socket is created and, if there is a security manager, the security manager’s checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

Return: the new Socket

public int getLocalPort()   Returns the port on which this socket is listening.

public void close()  Closes this socket. Any thread currently blocked in accept() will throw a SocketException. If this socket has an associated channel then the channel is closed as well.

     SocketImpl  The abstract class SocketImpl is a common superclass of all classes that actually implement sockets.It is used to create both client and server sockets. A ‘plain’ socket implements these methods exactly as described,without attempting to go through a firewall or proxy.

     java.io

     InputStream,OutputStream,BufferedInputStream, BufferedOutputStream BufferedReader,BufferedWriter,

     ByteArrayInputStream ByteArrayOutputStream DataInputStream

     DataOutputStream FileInputSteam FileOutputStream FileOutputStreqam

     java.io.OutputStream java.io.FilterOutputStream java.io.PrintStream

A PrintStream adds functionality to anoter output stream, namely the ability to print representations of various data values conventiently,Two other features are provided as well.Unlike other output streams, a PrintStream never throws an IOException,instead, exceptionsl situations merely set an internal flag that can be tested via the checkError method.Optionally,a PrintStream can be created so as to flush automatically, this means that the flush method is automatically invoked after a byte array is written,one of the println methods is invoked, or a newline charater or by (‘/n’) is written.

All characters printed by a PrintStream are convered into bytes using the platform’s default character encoding. The PrintWriter class should be used in situations that requrie writing characters rather than bytes.

public void flush() Flush the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushign that stream.

public void close() Close the stream. This is done by flushing the stream and then closing the underlyging output stream.

BufferedRead public String readLine()

  Read a line of text. A line is considered to be terminated by any one of a line feed(‘/n’), a carriage return(‘/r’), or a carraiage return followed immediately by a linefeed.

Returns: A String containing the contents of the line, not including any line-terminate characters, or null if the end of the stream has been reached.  

 

RFC(Request For Comments)-意即请求注解或征询建议,包含了

Internet的几乎所有重要的文字资料。如果你想成为成为网络方面

的专家,那么RFC无疑是最重要也是最经常需要用到的资料之一,

所以RFC享有网络知识圣经之美誉。通常,当某家机构或团

体开发出一套标准或提出对某种标准的设想,想要征询外界的

意见时,就会在Internet上发放一份RFC,对这一问题感兴趣的人可以阅读该RFC并提出自己的意见;绝大多数网络标准的指定都是以RFC的形式开始,经过大量的论证或修改过程,有主要的标准化组织所指定的,但在RFC所收录的文件并不都是正在使用或大家所公认的,也有很大一部分只是在某个局部领域被使用或并没有被采用,一份RFC具体处于什么状态都在文件中作了明确的标识。截止到2001年中期,公布的RFC大约有3000余篇,http://www.rfc.net RFc的官方站点,可以检查RFC最及时的更新情况。http://www.ietf.org 最重要的Internet组织之一  http://sunsite.dk RFC查询非常强大(可以以FTP登录下载全部的RFC文档) http://www.iso.ch ISO-国际标准化组织。http://standards.ieee.org  IEEE-电气与电子工程师协会。 http://web.ansi.org ANSI 美国国家标准化组织 http://www.itu.int ITU-国际电信同盟

                 

     

     TCP 端口,UDP 端口分开的。每一个端口有65536个端口,本地操作系统会给那些有需求的进程分配协议端口(protocal port,即)每个协议端口有一个正整数标示,如80139 445,等等,当目的主机接收到数据报后,将根据报文首部的目的端口号,把数据发送到相应端口,而与此端口相应的那个进程将会领取数据并等待下一组数据的到来。端口其实就是队,操作系统为每个进程分配了不同的队,数据报按照目的端口被推入相应的队中,等待被进程取用,在极特殊的情况下,这个队也是可能溢出的,不过操作系统允许各进程指定和调整自己的对的大小。

     不光接收数据报的进程需要开启它自己的端口,发送数据报的进程也需要开启端口,这样,数据报中将会标示有源端口,以便接受方能顺利的回传数据报到这个端口。默认情况下windows有很多端口都是开放的,在你上网的时候,网络病毒和黑客可以通过这些端口连上你的电脑,所以应该关闭。

     查看本机开放的端口:在命令提示符界面下,输入CMD命令“netstat -an”,回车。

netstat的全部参数及说明如下:显示协议统计信息和当前TCP/IP网络连接。

-a   显示所有连接和监听端口。

-b    显示包含于创建每个连接或监听端口的可执行组件。

-e    显示以太网统计信息。此选项可以与-s选项组合使用。

-n    以数字形式显示地址和端口号。

-o    显示与每个连接相关的所属进程ID.

-p proto 显示proto指定的协议的连接:proto可以是下列协议之一:

           TCP UDP TCPv6 UDPv6.如果与-s选项一起使用以显示按协议统计信息,proto可以是下列协议之一: IP  Ipv6  ICMP  ICMPv6

TCP  TCPv6  UDPUDPv6

-r   显示路由表。

-s   显示按协议统计信息。默认地,显示IP Ipv6 ICMP ICMPv6 TCP TCPv6 UDP UDPv6的统计信息。

客户连接服务器时可能抛出的异常

Socket的构造方法请求连接服务器时,可能抛出以下异常:

   UnknownHostException:如果无法识别主机的名字或IP地址,就会抛出这种异常。

   ConnectException:如果没有服务器进程监听指定的端口,或者服务器进程拒绝连接。就会抛出这种异常。

   SocketTimeException:如果等待连接超时,就会抛出这种异常

   BindException:如果无法把Socket对象与指定的本地IP地址或端口绑定,就会抛出这种异常。

  

获取Socket信息:

   getInetAddress();获得远程服务器的IP地址。

   getLocalAddress():获得客户本地主机的IP地址

   getPort():获得远程服务器的端口

   getLocalPort获得客户本地的端口

   getInputStream();获得输入流。如果Socket还没有连接或着已经关闭,或者已经通过shutdownOutpur()方法关闭输出流,那么此方法会抛出IOException.

   InetAddress的用法如下:

   //返回本地主机的IP地址

   InetAddress addr1=InetAddress.getLocalHost(); 

   //返回代表"222.34.5.7"IP地址

   InetAddress addr2=InetAddress.getByName("222.34.5.7"); 

   //返回域名为"www.javathinker.org"IP地址

   InetAddress addr3=InetAddress.getByName("www.javathinker.org"); 

   设定等待建立连接的超时时间

    

   Socket socket=new Socket();

   SocketAddress remoteAddr=new InetSocketAddress("localhost",8000);

   //等待建立连接的超时时间为1分钟

   socket.connect(remoteAddr, 60000); 

   SocketAddress The class represents a Socket Address with no protocol attachment, As an sbstract class, it is ment to be subclassed with a specific, protocol dependent,  implementation. It provides an immutable object used by sockets for binding, connecting, or as returned values.

   public void close()

              throws IOException

   Closes this socket.

   Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

   Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

   If this socket has an associated channel then the channel is closed as well.

   Throws:

    

   在一个Socket对象中,既包含远程服务器的IP地址和端口信息,也包含本地客户端IP地址和端口信息。默认情况下,客户端的IP地址来自于客户程序所在的主机,客户端的IP地址来由操作系统的随机分配。Socket类还有两个构造方法允许显示的设置客户端的IP地址和端口。

关闭Socket

Socket类提供了三个状态测试方法:

isClosed()   isConnected()   is Bound() 如果要判断一个Socket对象当前是否处于连接状态,可采用以下方式:

String isConnected=socket.isConnected() && !socket.isClosed();

半关闭Socket

有时候,可能仅仅希望关闭输出流和输入流之一。此时可以采用Socket类提供的半关闭方法:

   shutdownInput(): 关闭输入流。shutdownOutput(): 关闭输出流。

   先后调用SocketshutdownInput()shutdownOutput()方法。

仅仅关闭了输入流和输出流,并不等价于调用Socketclose()方法。在调用结束后,仍然要调用Socketclose()方法,因为只有该方法才会释放Socket占用的资源,比如占用的本地端口等。Socket类还提供了两个状态测试方法,用来判断输入流和输出流是否关闭:

   public boolean isInputShutdown()  public boolean isOutputShutdown()

设置Socket的选项

   TCP_NODELAY: 标示立即发送数据

   SO_RESUSEADDR:标示时候允许重用Socket所绑定的本地地址

   SO_TIMEOUT: 标示接受数据时的等待超时时间。

   SO_LINGER: 表示当执行Socketclose()方法时,是否立即关闭低层的Socket.

   SO_SNFBUF :表示发送数据的缓冲区的大小。

   SO_RCVBUF :标示接受数据的缓冲区的大小。

   SO_KEFPALIVE: 表示对于长时间处于空闲状态的Socket,是否要自动把它关闭。

   OONINLINE: 表示是否支持送一个字节的TCP紧急数据。

n     设置该选项:public void setTcpNoDelay(boolean on) throws SocketException

n     读取该选项:public boolean getTcpNoDelay() throws SocketException

n     TCP_NODEALY的默认值为false,表示采用Negale算法。如果调用setTcpNoDelay(true)方法,就会关闭Socket的缓冲,确保数据及时发送:

if(!socket.getTcpNoDelay())socket.setTcpNoDelay(true);

n     如果Socket的底层实现不支持TCP_NODELAY选项,那么getTcpNoDelay()setTcpNoDelay()方法会抛出SocketException

n     设置该选项:public void setResuseAddress(boolean on) throws SocketException

n     读取该选项:public boolean getResuseAddress() throws SocketException

n     为了确保一个进程关闭了Socket后,即使它还没释放端口,同一个主机上的其他进程还可以立刻重用该端口,可以调用SocketsetResuseAddress(true)方法:

if(!socket.getResuseAddress())socket.setResuseAddress(true);

n     值得注意的是socket.setResuseAddress(true)方法必须在Socket还没有绑定到一个本地端口之前调用,否则执行socket.setResuseAddress(true)方法无效。

n     设置该选项:public void setSoTimeout(int milliseconds) throws SocketException

n     读取该选项:public int getSoTimeOut() throws SocketException

 

n     当通过Socket的输入流读数据时,如果还没有数据,就会等待。例Socket类的SO_TIMEOUT选项用于设定接收数据的等待超时时间,单位为毫秒,它的默认值为0,表示会无限等待,永远不会超时。

n     设置该选项:public void setSoLinger(boolean on, int seconds) throws SocketException

n     读取该选项:public int getSoLinger() throws SocketException

n     SO_LINGER选项用来控制Socket关闭时的行为。

n     socket.setSoLinger(true,0):执行Socketclose()方法时,该方法也会立即返回,但底层的Socket也会立即关闭,所有未发送完的剩余数据被丢弃。

n     socket.setSoLinger(true,3600):执行Socketclose()方法时,该方法不会立即返回,而进入阻塞状态,同时,底层的Socket会尝试发送剩余的数据。只有满足以下两个条件之一,close()方法才返回:

n     底层的Socket已经发送完所有的剩余数据。

n     尽管底层的Socket还没有发送完所有的剩余数据,但已经阻塞了3600秒。close()方法的阻塞时间超过3600秒,也会返回,剩余未发送的数据被丢弃。

 

n     设置该选项:public void setReceiveBufferSize(int size) throws SocketException

n     读取该选项:public int getReceiveBufferSize() throws SocketException

n     SO_RCVBUF表示Socket的用于输入数据的缓冲区的大小。

n     如果底层Socket不支持SO_RCVBUF选项,那么setReceiveBufferSize()方法会抛出SocketException

n     设置该选项:public void setSendBufferSize(int size) throws SocketException

n     读取该选项:public int getSendBufferSize() throws SocketException

n     SO_SNDBUF表示Socket的用于输出数据的缓冲区的大小。

n     如果底层Socket不支持SO_SNDBUF选项,setSendBufferSize()方法会抛出SocketException

n     设置该选项:public void setKeepAlive(boolean on) throws SocketException

n     读取该选项:public int getKeepAlive() throws SocketException

n      

n     SO_KEEPALIVE选项为true,表示底层的TCP实现会监视该连接是否有效。

n     SO_KEEPALIVE选项的默认值为false,表示TCP不会监视连接是否有效,不活动的客户端可能会永久存在下去,而不会注意到服务器已经崩溃。

n     IP规定了四种服务类型,用来定性的描述服务的质量:

n     低成本:发送成本低。

n     高可靠性:保证把数据可靠的送达目的地。

n     最高吞吐量:一次可以接收或发送大批量的数据。

n     最小延迟:传输数据的速度快,把数据快速送达目的地。

n     这四种服务类型还可以进行组合,例如,可以同时要求获得高可靠性和最小延迟。Socket类中提供了设置和读取服务类型的方法:

n     设置服务类型:public void setTrafficClass(int trafficClass) throws SocketException

n     读取服务类型:public int getTrafficClass() throws SocketException

n     Socket类用四个整数表示服务类型:

n     低成本:0x02 (二进制的倒数第二位为1

n     高可靠性:0x04(二进制的倒数第三位为1

n     最高吞吐量:0x08(二进制的倒数第四位为1

n     最小延迟:0x10(二进制的倒数第五位为1

public void setPerformancePreferences(int connectionTime,int latency,int bandwidth)

n     以上方法的三个参数表示网络传输数据的三项指标:

n     参数connectionTime:表示用最少时间建立连接。

n     参数latency:表示最小延迟。

n     参数bandwidth:表示最高带宽。

n     setPerformancePreferences()方法用来设定这三项指标之间的相对重要性。可以为这些参数赋予任意的整数,这些整数之间的相对大小就决定了相应参数的相对重要性。例如,如果参数connectionTime2,参数latency1,而参数bandwidth3,就表示最高带宽最重要,其次是最少连接时间,最后是最小延迟。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值