是啥导致我的基本客户端/服务器套接字程序中的循环在第二次尝试时失败? [关闭]
Posted
技术标签:
【中文标题】是啥导致我的基本客户端/服务器套接字程序中的循环在第二次尝试时失败? [关闭]【英文标题】:What is causing the loop in my basic client/server socket program to fail on second attempt? [closed]是什么导致我的基本客户端/服务器套接字程序中的循环在第二次尝试时失败? [关闭] 【发布时间】:2021-11-11 22:05:59 【问题描述】:我制作了一个基本的套接字程序,用于在同一主机上的服务器和客户端之间发送消息。我可以发送一条消息,但在第二条消息上它退出。问题是它应该只在我输入“Bye”时退出。我知道我的 while 循环有问题,但我不知道为什么。这是我的代码:
客户:
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client3
public static void main(String[] args)
Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try
socket = new Socket("localhost", 4321);
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);
Scanner scanner = new Scanner(System.in);
boolean loopIT;
loopIT = true;
while (loopIT)
String msgToSend = scanner.nextLine();
bufferedWriter.write(msgToSend);
bufferedWriter.newLine();
bufferedWriter.flush();
System.out.println("Server:" + bufferedReader.readLine());
if(msgToSend.equalsIgnoreCase("Bye"))
loopIT = false;
else
loopIT = true;
catch (IOException e)
e.printStackTrace();
finally
try
if(socket != null)
socket.close();
if(inputStreamReader != null)
inputStreamReader.close();
if(outputStreamWriter != null)
outputStreamWriter.close();
if (bufferedReader != null)
bufferedReader.close();
if (bufferedWriter != null)
bufferedWriter.close();
catch (IOException e)
e.printStackTrace();
服务器(这应该是正确的,但我会添加它作为上下文):
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2
public static void main(String[] args) throws IOException
Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(4321);
while(true)
try
socket = serverSocket.accept();
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);
while(true)
String msgFromClient = bufferedReader.readLine();
System.out.println("Client:" + msgFromClient);
bufferedWriter.write("MSG received!");
bufferedWriter.newLine();
bufferedWriter.flush();
if (msgFromClient.equalsIgnoreCase("Bye"));
break;
socket.close();
inputStreamReader.close();
outputStreamWriter.close();
bufferedReader.close();
bufferedWriter.close();
catch (IOException e)
e.printStackTrace();
【问题讨论】:
您不需要五次关闭。您只需要关闭BufferedWriter
。当你关闭它时,其他所有东西都会自动关闭。
【参考方案1】:
在您的服务器中,您在此声明中有一个错误的;
:
if (msgFromClient.equalsIgnoreCase("Bye")); // <-- HERE
break;
因此break;
语句总是无条件执行,在收到并响应第一条消息后终止连接。
你需要删除那个;
:
if (msgFromClient.equalsIgnoreCase("Bye"))
break;
【讨论】:
天哪,我真是瞎了眼!我快疯了,因为我知道循环应该有效,哈哈。我想我需要更仔细地检查我的工作以上是关于是啥导致我的基本客户端/服务器套接字程序中的循环在第二次尝试时失败? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章