小白教程

 找回密码
 立即注册
小白教程 首页 系列教程 Java系列教程 查看内容

Java 网络编程

发布者: 小白教程



Socket 客户端实例

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

  1. // 文件名 GreetingClient.java
  2. import java.net.*;
  3. import java.io.*;
  4. public class GreetingClient
  5. {
  6. public static void main(String [] args)
  7. {
  8. String serverName = args[0];
  9. int port = Integer.parseInt(args[1]);
  10. try
  11. {
  12. System.out.println("Connecting to " + serverName
  13. + " on port " + port);
  14. Socket client = new Socket(serverName, port);
  15. System.out.println("Just connected to "
  16. + client.getRemoteSocketAddress());
  17. OutputStream outToServer = client.getOutputStream();
  18. DataOutputStream out =
  19. new DataOutputStream(outToServer);
  20. out.writeUTF("Hello from "
  21. + client.getLocalSocketAddress());
  22. InputStream inFromServer = client.getInputStream();
  23. DataInputStream in =
  24. new DataInputStream(inFromServer);
  25. System.out.println("Server says " + in.readUTF());
  26. client.close();
  27. }catch(IOException e)
  28. {
  29. e.printStackTrace();
  30. }
  31. }
  32. }

Socket 服务端实例

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

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

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

  1. $ java GreetingServer 6066
  2. Waiting for client on port 6066...
像下面一样开启客户端:
  1. $ java GreetingClient localhost 6066
  2. Connecting to localhost on port 6066
  3. Just connected to localhost/127.0.0.1:6066
  4. Server says Thank you for connecting to /127.0.0.1:6066
  5. Goodbye!
12
上一篇:Java 序列化下一篇:Java 发送邮件

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-11-24 13:21 , Processed in 0.020486 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

返回顶部