java网络编程socket的使用

Socket 客户端实例

如下的 GreetingClient 是一个客户端程序,该程序通过 socket 连接到服务器并发送一个请求,然后等待一个响应。

GreetingClient.java 文件代码:

 1 // 文件名 GreetingClient.java
 2  
 3 import java.net.*;
 4 import java.io.*;
 5  
 6 public class GreetingClient
 7 {
 8    public static void main(String [] args)
 9    {
10       String serverName = args[0];
11       int port = Integer.parseInt(args[1]);
12       try
13       {
14          System.out.println("连接到主机:" + serverName + " ,端口号:" + port);
15          Socket client = new Socket(serverName, port);
16          System.out.println("远程主机地址:" + client.getRemoteSocketAddress());
17          OutputStream outToServer = client.getOutputStream();
18          DataOutputStream out = new DataOutputStream(outToServer);
19  
20          out.writeUTF("Hello from " + client.getLocalSocketAddress());
21          InputStream inFromServer = client.getInputStream();
22          DataInputStream in = new DataInputStream(inFromServer);
23          System.out.println("服务器响应: " + in.readUTF());
24          client.close();
25       }catch(IOException e)
26       {
27          e.printStackTrace();
28       }
29    }
30 }

Socket 服务端实例

如下的GreetingServer 程序是一个服务器端应用程序,使用 Socket 来监听一个指定的端口。

GreetingServer.java 文件代码:

 1 // 文件名 GreetingServer.java
 2  
 3 import java.net.*;
 4 import java.io.*;
 5  
 6 public class GreetingServer extends Thread
 7 {
 8    private ServerSocket serverSocket;
 9    
10    public GreetingServer(int port) throws IOException
11    {
12       serverSocket = new ServerSocket(port);
13       serverSocket.setSoTimeout(10000);
14    }
15  
16    public void run()
17    {
18       while(true)
19       {
20          try
21          {
22             System.out.println("等待远程连接,端口号为:" + serverSocket.getLocalPort() + "...");
23             Socket server = serverSocket.accept();
24             System.out.println("远程主机地址:" + server.getRemoteSocketAddress());
25             DataInputStream in = new DataInputStream(server.getInputStream());
26             System.out.println(in.readUTF());
27             DataOutputStream out = new DataOutputStream(server.getOutputStream());
28             out.writeUTF("谢谢连接我:" + server.getLocalSocketAddress() + "\nGoodbye!");
29             server.close();
30          }catch(SocketTimeoutException s)
31          {
32             System.out.println("Socket timed out!");
33             break;
34          }catch(IOException e)
35          {
36             e.printStackTrace();
37             break;
38          }
39       }
40    }
41    public static void main(String [] args)
42    {
43       int port = Integer.parseInt(args[0]);
44       try
45       {
46          Thread t = new GreetingServer(port);
47          t.run();
48       }catch(IOException e)
49       {
50          e.printStackTrace();
51       }
52    }
53 }

编译以上两个 java 文件代码,并执行以下命令来启动服务,使用端口号为 6066:

$ javac GreetingServer.java 
$ java GreetingServer 6066
等待远程连接,端口号为:6066...

新开一个命令窗口,执行以上命令来开启客户端:

1 $ javac GreetingClient.java 
2 $ java GreetingClient localhost 6066
3 连接到主机:localhost ,端口号:6066
4 远程主机地址:localhost/127.0.0.1:6066
5 服务器响应: 谢谢连接我:/127.0.0.1:6066
6 Goodbye!

 

posted on 2020-05-27 15:24  芝麻开花——节节高  阅读(384)  评论(0编辑  收藏  举报

导航