📄 example1sender.java
字号:
import java.net.*;
import java.io.*;
/**
* This example illustrates the basic method calls for connectionless
* datagram socket.
* @author M. L. Liu
*/
public class Example1Sender {
// An application which sends a message using connectionless
// datagram socket.
// Three command line arguments are expected, in order:
// <domain name or IP address of the receiver>
// <port number of the receiver's socket>
// <message, a string, to send>
public static void main(String[] args) {
if (args.length != 3)
System.out.println
("This program requires three command line arguments");
else {
try {
InetAddress receiverHost = InetAddress.getByName(args[0]);
int receiverPort = Integer.parseInt(args[1]);
String message = args[2];
// instantiates a datagram socket for sending the data
DatagramSocket mySocket = new DatagramSocket();
byte[ ] buffer = message.getBytes( );
DatagramPacket datagram =
new DatagramPacket(buffer, buffer.length,
receiverHost, receiverPort);
mySocket.send(datagram);
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 + -