Showing posts with label Java-Network. Show all posts
Showing posts with label Java-Network. Show all posts

Monday, January 14, 2013

InetAddress


Creating an InetAddress Instance

InetAddress has no public contructor, so you must obtain instances via a set of static methods.
Here is how to get the InetAddress instance for a domain name:
InetAddress address = InetAddress.getByName("jenkov.com");
And here is how to get the InetAddress matching a String representation of an IP address:
InetAddress address = InetAddress.getByName("78.46.84.171");
And, here is how to obtain the IP address of the localhost (the computer the program is running on):
InetAddress address = InetAddress.getLocalHost();

Monday, November 26, 2012

NetWork Fundation&Java Final Project - Problem Solving Log

TCP is analogous to Telephone.

UDP is analogous to Mailbox.

a Socket is sometimes called a pipe because there are two ends to the communication.

Java: Detecting client disconnects from server side

The best way to detect a disconnection is to have the other end send a heartbeat and have the reader timeout when there is no data.

Q1: 在while(true){}循环中,有那些语句可以暂停这些循环?
EX:
serversocket.accept();
clientSocketList.get(i).getInputStream().read()==-1

Q2: InputStream/ OutputStream的运作原理。


Class InputStream



read

public abstract int read()
                  throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.



Sunday, November 25, 2012

Socket InputStream/ OutputStream



一、获取Socket的信息

  在一个Socket对象中同时包含了远程服务器的IP地址和端口信息,以及客户本地的IP地址和端口信息。此外,从Socket对象中还可以获得输出流和输出流,分别用于向服务器发送数据。一下方法用于获取Socket的有关信息。

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

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

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

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

  getOutputStream():获得输出流。如果Socket还没有连接,或者已经关闭,或者已经通过shutdownOutput()方法关闭输出流,那么此方法会抛出IOException。

二、关闭Socket

  当客户与服务器的通信结束,应当及时关闭Socket,以释放Socket占用的包括端口在内的各种资源。Socket的close()方法负责关闭Socket。当一个Socket对象被关闭。就不能再通过它的输入流和输出流进行I/O操作,否则会导致IOException。

  为了确保关闭Socket的操作总是被执行,强烈建议吧这个操作放在finally代码块中。

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

  isClose():如果Socket已经连接到了远程主机,并且还没关闭,则返回true,否则返回false。

  isConnected():如果Socket曾经连接到远程主机,则返回true,否则返回false。

  isBound():如果Socket已经与一个本地端口绑定,则返回true,否则返回false。

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

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

三、半关闭Socket

  如果仅仅是希望关闭输入流和输出流,可以采取Socket提供的半关闭方法。

  shutdownInput():关闭输入流。

  shutdownOuput():关闭输出流。 
//一旦调用了socket对象的shutdownOutput/shutdownInput方法,那么在此之后该socket对象的输出流将再也无法被打开。

  Socket还提供了两种方法来测试输入流和输出流是否关闭:

  public  boolean  isInputShutdown():输入流关闭成功返回true,否则false;

  public  boolean  isOutputShutdown():输出流关闭成功返回true,否则false;

http://www.java3z.com/cwbwebhome/article/article8/81210.html
可以引起网络连接关闭的情况有以下4种:
1. 直接调用Socket类的close方法。

2. 只要Socket类的InputStream和OutputStream有一个关闭,网络连接自动关闭(必须通过调用InputStream和OutputStream的close方法关闭流,才能使网络可爱接自动关闭)。
For example:

if(clientSocketList.size()!=0)
{
for(int i=0;i<clientSocketList.size();i++)
{
try {
PrintWriter pw = new PrintWriter(clientSocketList.get(i).getOutputStream());
pw.println(out); //PS: 这里如果用pw.print需要close了才会发出信息
pw.flush();
//如果这里用pw.close(),整个socket 就会close.
} catch (IOException e1) {
e1.printStackTrace();
}

3. 在程序退出时网络连接自动关闭。

4. 将Socket对象设为null或未关闭使用new Socket(…)建立新对象后,由JVM的垃圾回收器回收为Socket对象分配的内存空间后自动关闭网络连接。 

   虽然这4种方法都可以达到同样的目的,但一个健壮的网络程序最好使用第1种或第2种方法关闭网络连接。这是因为第3种和第4种方法一般并不会马上关闭网络连接,如果是这样的话,对于某些应用程序,将会遗留大量无用的网络连接,这些网络连接会占用大量的系统资源。

   在Socket对象被关闭后,我们可以通过isClosed方法来判断某个Socket对象是否处于关闭状态。然而使用isClosed方法所返回的只是Socket对象的当前状态,也就是说,不管Socket对象是否曾经连接成功过,只要处于关闭状态,isClosed就返回true。如果只是建立一个未连接的Socket对象,isClose则会返回false。如下面的代码将输出false。
Socket socket = new Socket();
System.out.println(socket.isClosed());

  除了isClose方法,Socket类还有一个isConnected方法来判断Socket对象是否连接成功。看到这个名字,也许读者会产生误解。其实isConnected方法所判断的并不是Socket对象的当前连接状态,而是Socket对象是否曾经连接成功过,如果成功连接过,即使现在isClose返回true,isConnected仍然返回true。因此,要判断当前的Socket对象是否处于连接状态,必须同时使用isClose和isConnected方法,即只有当isClose返回false,isConnected返回true的时候Socket对象才处于连接状态。下面的代码演示了上述Socket对象的各种状态的产生过程。

参考信息:

java socket传输中关闭流而不关闭socket的一些实践