当前位置:   article > 正文

Java实现TCP客户端和服务器端相互通信_java tcp client

java tcp client

解决TCP客户端和服务器端通信读不到数据的问题

 解决:

服务器端和客户端读完后加上client.shutdownInput();

服务器端和客户端写完后加上client.shutdownOutput();

服务器端代码:

  1. public class TCPTestServer2 {
  2. public static void main(String[] args) throws IOException {
  3. System.out.println("---------------服务器--------------");
  4. ServerSocket server = new ServerSocket(9999);
  5. Socket client = server.accept();
  6. System.out.println("有一个用户连接。。。。。。。");
  7. InputStream is = client.getInputStream();
  8. byte[] msg = is.readAllBytes();
  9. System.out.println(new String(msg));
  10. is.close();
  11. client.close();
  12. server.close();
  13. }
  14. }

客户端代码:

  1. public class TCPTestClient2 {
  2. public static void main(String[] args) throws IOException {
  3. System.out.println("-------------客户端------------");
  4. Socket client = new Socket("127.0.0.1", 9999);
  5. Scanner sc = new Scanner(System.in);
  6. byte[] msg = sc.nextLine().getBytes();
  7. OutputStream os = client.getOutputStream();
  8. os.write(msg);
  9. os.flush();
  10. os.close();
  11. client.close();
  12. }
  13. }

运行服务器端再运行客户端,在客户端中输入要发送的信息,回车

服务器收到信息,over

 然后!!!!!!!!!!!!!!!

想让服务器端回信息,结果翻车了。。。。。。

翻车全过程↓

客户端代码:

加了一个

        InputStream is = client.getInputStream();
        System.out.println(new String(is.readAllBytes()));

用来读服务器端发过来的信息

  1. public class TCPTestClient2 {
  2. public static void main(String[] args) throws IOException {
  3. System.out.println("-------------客户端------------");
  4. Socket client = new Socket("127.0.0.1", 9999);
  5. Scanner sc = new Scanner(System.in);
  6. byte[] msg = sc.nextLine().getBytes();
  7. OutputStream os = client.getOutputStream();
  8. os.write(msg);
  9. InputStream is = client.getInputStream();
  10. System.out.println(new String(is.readAllBytes()));
  11. is.close();
  12. os.flush();
  13. os.close();
  14. client.close();
  15. }
  16. }

服务器端:

只是加了个输出流

  1. public class TCPTestServer2 {
  2. public static void main(String[] args) throws IOException {
  3. System.out.println("---------------服务器--------------");
  4. ServerSocket server = new ServerSocket(9999);
  5. Socket client = server.accept();
  6. System.out.println("有一个用户连接。。。。。。。");
  7. InputStream is = client.getInputStream();
  8. OutputStream os = client.getOutputStream();
  9. byte[] msg = is.readAllBytes();
  10. System.out.println(new String(msg));
  11. Scanner sc = new Scanner(System.in);
  12. byte[] msg2 = sc.nextLine().getBytes();
  13. os.write(msg2);
  14. os.flush();
  15. os.close();
  16. is.close();
  17. client.close();
  18. server.close();
  19. }
  20. }

运行,程序未停止运行且服务器端也没有输出,客户端发过来的消息

也就是System.out.println(new String(msg));没执行到

  1. package homework0526.login1;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.Scanner;
  8. public class TCPTestServer2 {
  9. public static void main(String[] args) throws IOException {
  10. System.out.println("---------------服务器--------------");
  11. ServerSocket server = new ServerSocket(9999);
  12. Socket client = server.accept();
  13. System.out.println("有一个用户连接。。。。。。。");
  14. InputStream is = client.getInputStream();
  15. OutputStream os = client.getOutputStream();
  16. byte[] msg = is.readAllBytes();
  17. client.shutdownInput();
  18. String s = new String(msg);
  19. System.out.println(s);
  20. Scanner sc = new Scanner(System.in);
  21. byte[] msg2 = sc.nextLine().getBytes();
  22. os.write(msg2);
  23. client.shutdownOutput();
  24. os.flush();
  25. os.close();
  26. is.close();
  27. client.close();
  28. server.close();
  29. }
  30. }

 

应该也许的原因:

 解决:

服务器端和客户端读完后加上client.shutdownInput();

服务器端和客户端写完后加上client.shutdownOutput();

服务器端:

  1. public class TCPTestServer2 {
  2. public static void main(String[] args) throws IOException {
  3. System.out.println("---------------服务器--------------");
  4. ServerSocket server = new ServerSocket(9999);
  5. Socket client = server.accept();
  6. System.out.println("有一个用户连接。。。。。。。");
  7. InputStream is = client.getInputStream();
  8. OutputStream os = client.getOutputStream();
  9. byte[] msg = is.readAllBytes();
  10. client.shutdownInput();
  11. String s = new String(msg);
  12. System.out.println(s);
  13. Scanner sc = new Scanner(System.in);
  14. byte[] msg2 = sc.nextLine().getBytes();
  15. os.write(msg2);
  16. client.shutdownOutput();
  17. os.flush();
  18. os.close();
  19. is.close();
  20. client.close();
  21. server.close();
  22. }
  23. }

客户端:

  1. package homework0526.login1;
  2. import java.io.*;
  3. import java.net.Socket;
  4. import java.util.Scanner;
  5. public class TCPTestClient2 {
  6. public static void main(String[] args) throws IOException {
  7. while (true) {
  8. System.out.println("-------------客户端------------");
  9. Socket client = new Socket("127.0.0.1", 9999);
  10. Scanner sc = new Scanner(System.in);
  11. byte[] msg = sc.nextLine().getBytes();
  12. OutputStream os = client.getOutputStream();
  13. os.write(msg);
  14. client.shutdownOutput();
  15. InputStream is = client.getInputStream();
  16. System.out.println(new String(is.readAllBytes()));
  17. client.shutdownInput();
  18. is.close();
  19. os.flush();
  20. os.close();
  21. client.close();
  22. }
  23. }
  24. }
 

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

闽ICP备14008679号