如何在java TCP网络编程中编写代码

编程入门 行业动态 更新时间:2024-10-26 04:22:11
本文介绍了如何在java TCP网络编程中编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好。我是网络编程的初学者。我将为客户编写代码并在客户端询问用户一些问题,用户也将根据整数输入选择。据推测,服务器应该将用户的输入返回给客户端。但它不起作用。我希望你们能帮助我并举例说明。提前谢谢。 我的尝试: 服务器代码

import java.io.IOException; import java.io.PrintWriter; import java.ServerSocket; import java.Socket; import java.util.Scanner; public class TestServer { private static ServerSocket servSock; private static final int PORT = 1234 ; public static void main( String [] args) { System.out.println( 打开端口... \ n); 尝试 { servSock = new ServerSocket(PORT); // 第1步。 } catch (IOException ioEx) { System.out.println( 无法连接到端口!); System.exit( 1 ); } do { handleClient(); } while (true); } private static void handleClient() { Socket link = null; // 第2步。 尝试 { link = servSock.accept(); // 第2步。 扫描仪输入= new Scanner(link.getInputStream()); // 第3步。 PrintWriter输出= new PrintWriter(link.getOutputStream(),true); // 第3步。 int numMessages = 50 ; String message = input.nextLine(); // 第4步。 (!message.equals( ***关闭***)) { System.out.println( 收到消息。); numMessages ++; output.println( 消息 + numMessages + : + message); // 第4步。 message = input.nextLine(); } output.println(numMessages + 收到的消息。); // 第4步。 } catch (IOException ioEx) { ioEx.printStackTrace(); } 最后 { 尝试 { System.out.println( \ n *关闭连接... *); link.close(); // 第5步。 } catch (IOException ioEx) { System.out.println( 无法断开连接!); System.exit( 1 ); } } } }

客户代码

package testclient; import java.io.IOException; import java.io.PrintWriter; import java.InetAddress; import java.Socket; import java.UnknownHostException; import java.util.Scanner; public class TestClient { private static InetAddress host; private static final int PORT = 1234 ; public static void main( String [] args) { try { host = InetAddress.getLocalHost(); } catch (UnknownHostException uhEx) { System.out.println( 找不到主机ID); System.exit( 1 ); } accessServer(); } private static void accessServer() { Socket link = null; 尝试 { link = new 套接字(主机,端口); 扫描仪输入= 新扫描仪(link.getInputStream()); PrintWriter output = new PrintWriter(link.getOutputStream(),true); 扫描仪userEntry = 新扫描仪(System.in); String 消息,响应; do { System.out.println( 你健康吗?); message = userEntry.nextLine(); System.out.println( 你是否处于开心模式?); message = userEntry.nextLine(); System.out.println( 你是否处于悲伤状态?); message = userEntry.nextLine(); System.out.println( 你生病了吗?); message = userEntry.nextLine(); response = input.nextLine(); System.out.println( \ nServer + response); output.println(message); response = input.nextLine(); System.out.println( \ nServer + response); } while (!message.equals( *** *** CLOSE)); } catch (IOException ioEx) { ioEx.printStackTrace(); } 最后 { 尝试 { System.out.println( \ n *关闭连接.. 。*); link.close(); } catch (IOException ioEx) { System.out.println( 无法断开连接......); System.exit( 1 ); } } } }

解决方案

客户端上的序列每个问题应该是:
  • 在屏幕上打印
  • 读取用户输入
  • 发送至服务器
  • 从服务器接收回复
  • 在屏幕上打印回复
但您的代码不会这样做。它首先打印问题并读取用户输入的四个问题,然后等待服务器响应但不发送到服务器。 它应该像

System.out.println( 你身体健康吗? ); message = userEntry.nextLine(); output.println(message); response = input.nextLine(); System.out.println( \ nServer + response); // 下一个问题在这里重复上述

Hello. I am beginner in network programming. I am gonna write code for client and ask user for some question in client and user will also input the choice based on integer. Supposedly, the server should return the user's input to the client. But it not works. I hope you guys can help me and give example. Thank you in advance. What I have tried: Server code

import java.io.IOException; import java.io.PrintWriter; import java.ServerSocket; import java.Socket; import java.util.Scanner; public class TestServer { private static ServerSocket servSock; private static final int PORT = 1234; public static void main(String[] args) { System.out.println("Opening port...\n"); try { servSock = new ServerSocket(PORT); //Step 1. } catch(IOException ioEx) { System.out.println("Unable to attach to port!"); System.exit(1); } do { handleClient(); } while (true); } private static void handleClient() { Socket link = null; //Step 2. try { link = servSock.accept(); //Step 2. Scanner input = new Scanner(link.getInputStream()); //Step 3. PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step 3. int numMessages = 50; String message = input.nextLine(); //Step 4. while (!message.equals("***CLOSE***")) { System.out.println("Message received."); numMessages++; output.println("Message " + numMessages + ": " + message); //Step 4. message = input.nextLine(); } output.println(numMessages + " messages received."); //Step 4. } catch(IOException ioEx) { ioEx.printStackTrace(); } finally { try { System.out.println( "\n* Closing connection... *"); link.close(); //Step 5. } catch(IOException ioEx) { System.out.println("Unable to disconnect!"); System.exit(1); } } } }

Client code

package testclient; import java.io.IOException; import java.io.PrintWriter; import java.InetAddress; import java.Socket; import java.UnknownHostException; import java.util.Scanner; public class TestClient { private static InetAddress host; private static final int PORT = 1234; public static void main(String[] args) { try { host = InetAddress.getLocalHost(); } catch(UnknownHostException uhEx) { System.out.println("Host ID not found"); System.exit(1); } accessServer(); } private static void accessServer() { Socket link = null; try { link = new Socket(host,PORT); Scanner input = new Scanner(link.getInputStream()); PrintWriter output = new PrintWriter(link.getOutputStream(),true); Scanner userEntry = new Scanner(System.in); String message, response; do { System.out.println("Are you in healthy condition?"); message = userEntry.nextLine(); System.out.println("Are you in happy mode?"); message = userEntry.nextLine(); System.out.println("Are you in sad condition?"); message = userEntry.nextLine(); System.out.println("Are you in sick condition?"); message = userEntry.nextLine(); response = input.nextLine(); System.out.println("\nServer " +response); output.println(message); response = input.nextLine(); System.out.println("\nServer " +response); }while(!message.equals("***CLOSE***")); } catch(IOException ioEx) { ioEx.printStackTrace(); } finally { try { System.out.println("\n*Closing connection...*"); link.close(); } catch(IOException ioEx) { System.out.println("Unable to disconnect..."); System.exit(1); } } } }

解决方案

The sequence on the client for each question should be:
  • Print on screen
  • Read user input
  • Send to server
  • Receive response from server
  • Print response on screen
But your code does not do that. It first prints questions and reads the user input for four questions and then waits for a server response but without sending to the server. It should be like

System.out.println("Are you in healthy condition?"); message = userEntry.nextLine(); output.println(message); response = input.nextLine(); System.out.println("\nServer " +response); // Next question goes here repeating the above

更多推荐

如何在java TCP网络编程中编写代码

本文发布于:2023-11-22 20:15:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网络编程   代码   如何在   TCP   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!