gpt4 book ai didi

Java Socket编程客户端服务器通信问题

转载 作者:行者123 更新时间:2023-12-01 17:25:14 26 4
gpt4 key购买 nike

我有3个java类。堆栈 - 实现堆栈。Stackserver - 实现创建 Stack 对象并根据用户操作返回结果的服务器StackClient - 实现发送操作请求并从服务器接收结果的客户端。

Action 有push、pop、display、exit。

当我输入操作时,它总是说无效操作。有人能找出程序中的错误吗?

堆栈类

public class Stack
{
private int maxSize;
private int[] stackArray;
private int top,i;

public Stack(int s)
{
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}

public void push(int j)
{
stackArray[++top] = j;
}

public String display()
{
i=top;
String s="";
while(i>=0)
{
s=s+" "+stackArray[i--];
}
return s;
}

public int pop()
{
return stackArray[top--];
}

public int peek()
{
return stackArray[top];
}

public boolean isEmpty()
{
return (top == -1);
}

public boolean isFull()
{
return (top == maxSize - 1);
}
}

StackServer类

import java.net.*;
import java.io.*;

public class StackServer
{

ServerSocket server;
Socket link;
PrintWriter out;
BufferedReader in;

public void run()
{
try
{

System.out.println("Creating Server Socket.");
server = new ServerSocket(4000);
System.out.println("Successfully created Server Socket.");

Socket link;
System.out.println("Waiting For Connection.");
link = server.accept();
System.out.println("Connection received from " + link.getInetAddress().getHostName());

out = new PrintWriter(link.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(link.getInputStream()));

out.println("Eneter Stack Size.");
int size = Integer.parseInt((in.readLine()).trim());
Stack stack = new Stack(size);

while(true)
{
String action="";
int n;

out.println("Eneter Stack Operation.");
action = in.readLine();

if(action == "push")
{
if(stack.isFull())
{
out.println("Stack Full. What next?");
}
else
{
out.println("Enter element: ");
n = Integer.parseInt((in.readLine()).trim());
stack.push(n);
out.println("Pushed item. What next?");
}

}
else if(action == "pop")
{
if(stack.isFull())
{
out.println("Stack Full. What next?");
}
else
{
n = stack.pop();
out.println(n);
out.println("Pushed item. What next?");
}
}
else if(action == "display")
{
out.println(stack.display());
}
else if(action == "exit")
{
link.close();
break;
}
else
{
out.println("Invalid Operation");
}
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}

public static void main(String [] args)
{
StackServer s = new StackServer();
s.run();}
}

StackClient类

import java.net.*;
import java.io.*;

public class StackClient
{
public static void main(String [] args)
{
try
{
Socket client;
System.out.println("Creating Client Socket.");
client = new Socket("localhost",4000);
System.out.println("Successfully created Client Socket.");

BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
String action ="";
String size,s;

System.out.println(in.readLine());
size = read.readLine();
out.println(size);

while(true)
{

System.out.println(in.readLine());
action = read.readLine();
out.println(action);

if(action.equals("push"))
{
System.out.println(in.readLine());
s=read.readLine();
System.out.println(in.readLine());
}
else if(action == "pop")
{
System.out.println(in.readLine());
}
else if(action == "display")
{
System.out.println(in.readLine());
}
else if(action == "exit")
{
client.close();
break;
}
else
{
out.println("Invalid Operation from Client");
}
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}

最佳答案

使用 String.equals() 比较字符串,而不是 ==:

if(action == "push")
...
else if(action == "pop")
如果两个对象是同一个实例,

== 返回 true。您可以有两个具有相同值的不同 String 实例。

关于Java Socket编程客户端服务器通信问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15462165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com