example4connectionrequestor.java
来自「JAVA分布式程序学习的课件(全英文)」· Java 代码 · 共 45 行
JAVA
45 行
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 which sends a message using stream-mode socket.
// A command line argument is expected:
// <host name 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) {
System.out.println(ex);
}
} // end else
} // end main
} // end class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?