当前位置:   article > 正文

计网实验第二章:TCP与UDP实验_计算机网络实验-第2关:udp协议分析 csdn

计算机网络实验-第2关:udp协议分析 csdn

TCP

1.服务器绑定端口号启动服务器欢迎socket

2. 等待客户端发送请求。

3.等到客户端发送请求之后,服务器与客户端重新建立一个socket,但是客户端的目标端口不变。

java代码如下

客户端

客户端代码图解

  1. import java.io.*;
  2. import java.net.*;
  3. class TCPClient {
  4. public static void main(String argv[]) throws Exception
  5. {
  6. String sentence;
  7. String modifiedSentence;
  8. BufferedReader inFromUser = new BufferedReader(
  9. new InputStreamReader(System.in));
  10. //开一个socket
  11. Socket clientSocket = new Socket("localhost", 6789);
  12. DataOutputStream outToServer = new DataOutputStream(
  13. clientSocket.getOutputStream());
  14. BufferedReader inFromServer =
  15. new BufferedReader(new InputStreamReader(
  16. clientSocket.getInputStream()));
  17. sentence = inFromUser.readLine();
  18. //发送请求
  19. outToServer.writeBytes(sentence + '\n');
  20. //接受响应
  21. modifiedSentence = inFromServer.readLine();
  22. System.out.println("FROM SERVER: " +
  23. modifiedSentence);
  24. //关闭客户端
  25. clientSocket.close();
  26. }
  27. }

服务端 

  1. import java.io.*;
  2. import java.net.*;
  3. class TCPServer {
  4. public static void main(String argv[]) throws Exception
  5. {
  6. String clientSentence;
  7. String capitalizedSentence;
  8. //开一个socket用做于欢迎socket
  9. ServerSocket welcomeSocket = new ServerSocket
  10. (6789);
  11. while(true) {
  12. //接受客户端的请求
  13. Socket connectionSocket = welcomeSocket.
  14. accept();
  15. BufferedReader inFromClient =
  16. new BufferedReader(new InputStreamReader(
  17. connectionSocket.getInputStream()));
  18. DataOutputStream outToClient =
  19. new DataOutputStream(
  20. connectionSocket.getOutputStream());
  21. //从客户端接受到信息
  22. clientSentence = inFromClient.readLine();
  23. //改变客户端发送的信息
  24. capitalizedSentence =
  25. clientSentence.toUpperCase() + '\n';
  26. //发回给客户端
  27. outToClient.writeBytes(capitalizedSentence);
  28. }
  29. }
  30. }

UDP 

1.udp的客户端可以比udp的服务端先开启

2.服务器绑定端口 

java代码如下:

客户端 

  1. import java.io.*;
  2. import java.net.*;
  3. class UDPClient {
  4. public static void main(String args[]) throws Exception
  5. {
  6. BufferedReader inFromUser =
  7. new BufferedReader(new InputStreamReader
  8. (System.in));
  9. //为客户端进程创建了一个门,但没有在两个进程之间创建管道
  10. DatagramSocket clientSocket = new DatagramSocket();
  11. //调用DNS查找
  12. InetAddress IPAddress =
  13. InetAddress.getByName("localhost");
  14. byte[] sendData = new byte[1024];
  15. byte[] receiveData = new byte[1024];
  16. String sentence = inFromUser.readLine();
  17. sendData = sentence.getBytes();
  18. //与TCP发送流不同,udp是打一个包发过去,这个就是udp的包
  19. //这一行构造数据包sendPacket,客户机将通过套接字将其弹出到网络中。
  20. // 这个包包括包中包含的数据、sendData、该数据的长度、服务器的IP地址和应用程序的端口号(我们将其设置为9876)。
  21. DatagramPacket sendPacket =
  22. new DatagramPacket(sendData, sendData.length,
  23. IPAddress, 9876);
  24. clientSocket.send(sendPacket);
  25. //接受服务器响应包
  26. DatagramPacket receivePacket =
  27. new DatagramPacket(receiveData,
  28. receiveData.length);
  29. clientSocket.receive(receivePacket);
  30. //将回复的包中的数据拿出来
  31. String modifiedSentence =
  32. new String(receivePacket.getData());
  33. System.out.println("FROM SERVER:" +
  34. modifiedSentence);
  35. //关闭客户端,释放资源
  36. clientSocket.close();
  37. }
  38. }

 服务器

  1. import java.io.*;
  2. import java.net.*;
  3. public class UDPServer {
  4. public static void main(String args[]) throws Exception
  5. {
  6. DatagramSocket serverSocket = new
  7. DatagramSocket(9876);
  8. byte[] receiveData = new byte[1024];
  9. byte[] sendData = new byte[1024];
  10. while(true)
  11. {
  12. //接受客户端发来的请求
  13. DatagramPacket receivePacket =
  14. new DatagramPacket(receiveData,
  15. receiveData.length);
  16. serverSocket.receive(receivePacket);
  17. //将客户端的请求的信息转为字符数组
  18. String sentence = new String(
  19. receivePacket.getData());
  20. //从请求里拿到客户端IP地址
  21. InetAddress IPAddress =
  22. receivePacket.getAddress();
  23. //拿到客户端端口
  24. int port = receivePacket.getPort();
  25. String capitalizedSentence =
  26. sentence.toUpperCase();
  27. sendData = capitalizedSentence.getBytes();
  28. //将要返回的响应打包,里面需要客户端的IP地址,客户端端口
  29. DatagramPacket sendPacket =
  30. new DatagramPacket(sendData,
  31. sendData.length, IPAddress, port);
  32. //服务器发送响应包
  33. serverSocket.send(sendPacket);
  34. }
  35. }
  36. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/660449
推荐阅读
相关标签
  

闽ICP备14008679号