gpt4 book ai didi

Java解析问题-如何从服务器接收数据?

转载 作者:行者123 更新时间:2023-11-30 07:25:46 25 4
gpt4 key购买 nike

我有一个客户端代码,它正在发送请求Push 567,我正在操纵服务器中的号码并发回客户端。我的代码适用于示例整数,但是当我包含带有数字的字符串(例如 PUSH 567)时,我遇到了问题。当我只在服务器程序中放入数字 567 并取出整数值时,它工作正常。

编辑:它仅适用于字符串或整数,但在我解析它时不起作用。

客户端代码

 public class Client 
{
public static void main(String[] args) throws IOException
{
Socket s=new Socket("127.0.0.1",1342);
Scanner sc1=new Scanner(s.getInputStream());
String req = "PUSH 567";

PrintStream p = new PrintStream(s.getOutputStream());
p.println(req);

int temp=sc1.nextInt();
System.out.println(temp);

}
}

服务器代码

  public class NewServer 
{
public static void main(String[] args) throws IOException
{
ServerSocket k=new ServerSocket(1342);
Socket ss=k.accept();
Scanner sc=new Scanner(ss.getInputStream());

String s = sc.next();
int i = Integer.valueOf(s);
int temp=i+2;

PrintStream p=new PrintStream(ss.getOutputStream());
p.println(temp);

}
}

我收到错误

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Tester.Client.main(Client.java:24)

最佳答案

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class NewServer {

public static final void main(final String[] args) {
int portNumber = 1342;

try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String s = "SERVER: Started.";
out.println(s);
while ((s = in.readLine()) != null) {
System.out.println("RECV: "+s);
String[] ss = s.split("PUSH\\s");
out.println(Integer.parseInt(ss[1]) + 2 + "");
System.out.println("SEND: " + Integer.parseInt(ss[1]) + 2);
}
} catch (IOException e) {
e.printStackTrace();
}

}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

public static final void main(final String[] args) {
String hostName = "127.0.0.1";
int portNumber = 1342;

try (
Socket client = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
) {
String fromServer = null;
client.setKeepAlive(true);
while ((fromServer = in.readLine()) != null) {
System.out.println(fromServer);
out.println("PUSH 567");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

关于Java解析问题-如何从服务器接收数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829375/

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