楼主: Lisrelchen
1509 0

A Simple Network Client using Java [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-6-5 11:06:27 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
A Simple Network Client using Java

Example 5-4 is a simple network client program that does not use the URL class. Instead, it uses java.net.Socket to connect to server. It sends a line of text to the server, and then reads and prints out the server’s response. The main point of interest in this example is the introduction of the Socket class, which creates a stream-based network connection between a client and a server. To create a network connection to another host, you simply create a Socket, specifying the desired host and port. If there is a program (a server) running on the specified host and listening for connections on the specified port, the Socket( ) constructor returns a Socket object you can use to communicate with the server. (If there is not a server listening on the specified host and port, or if anything goes wrong—and many things can go wrong with networking—the Socket( ) constructor throws an exception.)

If you are not familiar with hosts and ports, think of the host as a post office and the port as a post-office box. Just as a post office has many different post-office boxes, any host on the network can run many different servers at a time. Different servers use different ports for their addresses. To establish a connection, you must specify both the correct host and the correct port. Many services have standard default ports. Web servers run on port 80, POP email servers run on port 110, and so on.

Once you have a Socket object, you are connected, across the network, to a server. The getInputStream( ) method of the socket returns anInputStream you can use to read bytes from the server, and getOutputStream( ) returns an OutputStream you can use to write bytes to the server. This is exactly what this Connect program does.

Despite its simplicity, our simple Connect client is actually useful in conjunction with simple network servers like “daytime” (port 13) and “finger” (port 79). For example, here is how you can use the program to find out the current time (daytime) and to find out who is logged on to a server (finger):

java je3.net.Connect time-a.nist.gov 13java je3.net.Connect rtfm.mit.edu 79
  1. Example 5-4. Connect.java
  2. package je3.net;
  3. import java.io.*;
  4. import java.net.*;

  5. /**
  6. * A simple network client that establishes a network connection to a specified
  7. * port on a specified host, sends an optional message across the connection,
  8. * reads the response from the server, and exits.  A suitable client for simple
  9. * network services like the daytime or finger.
  10. **/
  11. public class Connect {
  12.     public static void main(String[  ] args) {
  13.         try {  // Handle exceptions below
  14.             // Get our command-line arguments
  15.             String hostname = args[0];
  16.             int port = Integer.parseInt(args[1]);
  17.             String message = "";
  18.             if (args.length > 2)
  19.                 for(int i = 2; i < args.length; i++) message += args[i] + " ";
  20.             
  21.             // Create a Socket connected to the specified host and port.
  22.             Socket s = new Socket(hostname, port);

  23.             // Get the socket output stream and wrap a PrintWriter around it
  24.             PrintWriter out = new PrintWriter(s.getOutputStream( ));

  25.             // Send the specified message through the socket to the server.
  26.             out.print(message + "\r\n");
  27.             out.flush( );  // Send it now.
  28.             
  29.             // Get an input stream from the socket and wrap a BufferedReader
  30.             // around it, so we can read lines of text from the server.
  31.             BufferedReader in =
  32.                 new BufferedReader(new InputStreamReader(s.getInputStream( )));

  33.             // Before we start reading the server's response, tell the socket
  34.             // that we don't want to wait more than 3 seconds
  35.             s.setSoTimeout(3000);

  36.             // Now read lines from the server until the server closes the
  37.             // connection (and we get a null return indicating EOF) or until
  38.             // the server is silent for 3 seconds.
  39.             try {
  40.                 String line;                          
  41.                 while((line = in.readLine( )) != null) // If we get a line
  42.                     System.out.println(line);         // print it out.
  43.             }
  44.             catch(SocketTimeoutException e) {
  45.                 // We end up here if readLine( ) times out.
  46.                 System.err.println("Timeout; no response from server.");
  47.             }
  48.             
  49.             out.close( );  // Close the output stream
  50.             in.close( );   // Close the input stream
  51.             s.close( );    // Close the socket
  52.         }
  53.         catch(IOException e) {  // Handle IO and network exceptions here
  54.             System.err.println(e);
  55.         }
  56.         catch(NumberFormatException e) {  // Bad port number
  57.             System.err.println("You must specify the port as a number");
  58.         }
  59.         catch(ArrayIndexOutOfBoundsException e) {  // wrong # of args
  60.             System.err.println("Usage: Connect <hostname> <port> message...");
  61.         }
  62.     }
  63. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:network simple Client Using Work interest Network network another between

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-7 05:18