⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example4connectionrequestor.java

📁 JAVA分布式程序学习的课件(全英文)
💻 JAVA
字号:
//**************************************************************
// See BetterExample4ConnectionRequestor.java for an improved
// version of this class.  This class does not allow a timeout
// on connection.  The better version does.
//**************************************************************

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

/**
 * This example illustrates the basic syntax for stream-mode
 * socket.
 * @author M. L. Liu
 */
public class Example4ConnectionRequestor {

// An application that requests a connection and
//  sends a message using stream-mode socket.
// Two command line arguments are expected: 
//
//    <host name of the connection accceptor>
//    <port number of the connection accceptor>

   public static void main(String[] args) {
      if (args.length != 2)
         System.out.println
            ("This program requires two command line arguments");
      else {
         try {
  		      InetAddress acceptorHost = InetAddress.getByName(args[0]);
  		      int acceptorPort = Integer.parseInt(args[1]);
            // instantiates a data socket
   	      Socket mySocket = new Socket(acceptorHost, acceptorPort); 
/**/        System.out.println("Connection request granted"); 
            // get an input stream for reading from the data socket
            InputStream inStream = mySocket.getInputStream();
            // create a BufferedReader object for text line input
            BufferedReader socketInput = 
               new BufferedReader(new InputStreamReader(inStream));
/**/        System.out.println("waiting to read");
            // read a line from the data stream
            String message = socketInput.readLine( );
/**/        System.out.println("Message received:");
            System.out.println("\t" + message);
            mySocket.close( );
/**/        System.out.println("data socket closed");
         } // end try
	      catch (Exception ex) {
            ex.printStackTrace( );
	      } //end catch
      } // end else
   } // end main
} // end class


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -