gpt4 book ai didi

java - 客户端服务器网络问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:50 24 4
gpt4 key购买 nike

我正在创建一个客户端服务器架构。

客户端输入一个数字,服务器返回该数字的乘法表。

一旦客户端输入结束,服务器就会停止响应。

服务器不接受用户的号码。

建议对代码进行更改,使其完全正常工作。

//服务器

package p1;

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

public class Server
{
public static void main(String[] args) throws Exception
{
System.out.println("Server Signing On");
ServerSocket ss=new ServerSocket(9000);

Socket soc=ss.accept();
// System.out.println("Done");
BufferedReader nis = new BufferedReader(
new InputStreamReader(
soc.getInputStream()
)
);
// System.out.println("Till");
PrintWriter nos = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
soc.getOutputStream()
)
), true);
System.out.println("here");
String s = nis.readLine();
String ans = "";
int no = 0;

while(!s.equalsIgnoreCase("end"))
{

no = Integer.parseInt(s); System.out.println("no = " + no);
for (int i = 1; i <= 10; i++)
ans += no + " * " + i + " = " + (no * i) + "\n";
// System.out.println(ans);
nos.println(ans);
System.out.println("Data sent");
s=nis.readLine();
ans = "";

}
System.out.println("Server Signing off");
ss.close();
}
}

//客户端

    package p1;

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

public class Client
{

public static void main(String[] args) throws Exception
{
Socket soc=new Socket("127.0.0.1", 9000);
System.out.println("Client Signing On");

PrintWriter nos = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
soc.getOutputStream()
)
), true);

BufferedReader nis = new BufferedReader(
new InputStreamReader(
soc.getInputStream()
)
);

BufferedReader kin = new BufferedReader(
new InputStreamReader(
System.in
)
);

System.out.println("Enter a number");
String a = kin.readLine();
String s = "";

while(!a.equalsIgnoreCase("end"))
{
nos.println(a);
while((s = nis.readLine()) != null)
System.out.println(s);
System.out.println("Enter a number");
a = kin.readLine();
// System.out.println("a = " + a);
}
nos.println(a);

System.out.println("Client Signing Off");
soc.close();
}

}

最佳答案

对代码进行了以下更改并且它起作用了。

//服务器

package p1;

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

public class Server
{
public static void main(String[] args) throws Exception
{
System.out.println("Server Signing On");
ServerSocket ss=new ServerSocket(9000);

Socket soc=ss.accept();

BufferedReader nis = new BufferedReader(
new InputStreamReader(
soc.getInputStream()
)
);

PrintWriter nos = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
soc.getOutputStream()
)
), true);

String ans = "";
String s = nis.readLine();

while(!s.equalsIgnoreCase("end"))
{
int no = Integer.parseInt(s);

for (int i = 1; i <= 10; i++)
ans += no + " * " + i + " = " + (no * i) + " ";
nos.println(ans);
ans = "";
s = nis.readLine();
}
nos.println(s);

System.out.println("Server Signing off");
ss.close();
}
}

//客户端

package p1;

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

public class Client
{

public static void main(String[] args) throws Exception
{
Socket soc=new Socket("127.0.0.1", 9000);
System.out.println("Client Signing On");

PrintWriter nos = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
soc.getOutputStream()
)
), true);

BufferedReader nis = new BufferedReader(
new InputStreamReader(
soc.getInputStream()
)
);

BufferedReader kin = new BufferedReader(
new InputStreamReader(
System.in
)
);

System.out.println("Enter a number");
String a = kin.readLine();

while(!a.equalsIgnoreCase("end"))
{
nos.println(Integer.parseInt(a));
String[] s = nis.readLine().split(" ");

for(String x: s)
System.out.println(x);


System.out.println("Enter a number");
a = kin.readLine();
}

nos.println(a);
System.out.println("Client Signing Off");
soc.close();
}

}

关于java - 客户端服务器网络问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49601507/

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