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

📄 example3sender.java

📁 JAVA分布式程序学习的课件(全英文)
💻 JAVA
字号:
import java.net.*;

/**
 * This example illustrates the basic syntax for connection-oriented
 * datagram socket.
 * @author M. L. Liu
 */
public class Example3Sender {

// An application which uses a connection-oriented datagram
// socket to send multiple messages, then receives one.
// Four command line arguments are expected, in order: 
//    <domain name or IP address of the receiver>
//    <port number of the other process' datagram socket>
//    <port number of this process's datagram socket>
//    <message, a string, to send>

   public static void main(String[] args) {
      if (args.length != 4)
         System.out.println
            ("This program requires four command line arguments");
      else {
         try {      
  		      InetAddress receiverHost = InetAddress.getByName(args[0]);
            int receiverPort = Integer.parseInt(args[1]);
  		      int myPort = Integer.parseInt(args[2]);
            String message = args[3];
            // instantiates a datagram socket for the connection
   	      MyDatagramSocket	mySocket = new MyDatagramSocket(myPort); 
            // make the connection
            mySocket.connect(receiverHost, receiverPort);
            for (int i=0; i<10; i++)  
               mySocket.sendMessage( receiverHost, receiverPort, message);
            // now receive a message from the other end
            System.out.println(mySocket.receiveMessage());
            // cancel the connection, the close the socket
            mySocket.disconnect( );
				mySocket.close( );
         } // end try
	      catch (Exception ex) {
            ex.printStackTrace( );
	      }
      } // end else
   } // end main
} // end class

⌨️ 快捷键说明

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