。这个程序没有处理客户端/服务器请求,也没有分配响应变量...线程保持挂起状态,从客户端向服务器发送用户输入后没有任何反应...任何帮助?
以下代码:
public class NumberClient
{
public static void main(String[] args)
throws IOException
{
final int PORT = 8899;
Scanner console = new Scanner(System.in);
while (true)
{
Socket s = new Socket("localhost", PORT);
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);
System.out.println("Enter a number to process: ");
double newnumber = console.nextDouble();
if (newnumber == 0) System.exit(0);
System.out.print("Sending: " + new number);
//does nothing after here
out.print(newnumber);
out.flush();
String response = in.nextLine();
System.out.println("The square root of " + newnumber + " is: " + response);
s.close();
}
}
}
这是我的服务器程序,使用Squarerooter.java来计算平方根
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
A server that executes the the Squarerooter command.
*/
public class SquareRootServer
{
public static void main(String[] args) throws IOException
{
final int SBAP_PORT = 8899;
ServerSocket server = new ServerSocket(SBAP_PORT);
System.out.println("Waiting for clients to connect . . . ");
while (true)
{
Socket s = server.accept();
System.out.println("Client connected.");
Squarerooter service = new Squarerooter(s);
Thread t = new Thread(service);
t.start();
}
}
}
这是Squarerooter.java / * 计算平方根的类,并将结果写入客户端。 * /
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.lang.*;
/**
Executes Simple square root computation on number from a socket.
*/
public class Squarerooter implements Runnable
{
private Socket s;
private Scanner in;
private PrintWriter out;
/**
Constructs a object that processes commands
from a socket.
@param aSocket the socket
*/
public Squarerooter(Socket aSocket)
{
s = aSocket;
}
public void run()
{
try
{
try
{
in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream());
squareRoot();
}
finally
{
s.close();
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
/**
Executes a command until the end of input.
*/
public void squareRoot() throws IOException
{
while (true)
{
if (!in.hasNext()) return;
double mynumber = in.nextDouble();
if (mynumber == 0) return;
else executeCommand(mynumber);
}
}
/** Executes a single command.
@param mynumber the number to square
*/
public void executeCommand(double mynumber)
{
mynumber = in.nextDouble();
if (mynumber == 0) return;
else
{
mynumber = Math.sqrt(mynumber);
System.out.print(mynumber);
out.print(mynumber);
out.flush();
}
}
}
这是我用来测试连接的ClientServerDemo.java
import java.io.IOException;
public class ClientServerDemo
{
static class ServerRunnable implements Runnable
{
public void run()
{
// start the client
Thread t2 = new Thread(new ClientRunnable());
t2.start();
try
{
SquareRootServer.main(null);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
};
static class ClientRunnable implements Runnable
{
public void run()
{
try
{
NumberClient.main(null);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
};
public static void main(String[] args) throws InterruptedException
{
// start the server
Thread t1 = new Thread(new ServerRunnable());
t1.start();
}
}
答案 0 :(得分:0)
检查Squarerooter.run()方法体。或者在这里分享。
编辑:
问题是你从套接字读取了两次该号码。第一次在SquareRootServer.squareRoot()第二次在SquareRootServer.executeCommand()。数字丢失了吗?
SquareRootServer.run()中的try-catch块也很奇怪。我认为嵌套捕获是没用的。从它做一个块。