i built a program with client and server sockets in java that gets a number from the client, and multiply it by 2. the code doesn't let me put a number in the client side. code:
我用java中的客户端和服务器套接字构建了一个程序,它从客户端获取一个数字,然后乘以2.代码不允许我在客户端放一个数字。码:
Client:
public class cli {
public static void main(String args[]) throws UnknownHostException, IOException{
Scanner in = new Scanner(System.in);
int number,temp;
Socket s = new Socket("127.0.0.1", 1342);
Scanner c1 = new Scanner(s.getInputStream());
System.out.println("Enter any number");
number = in.nextInt();
PrintStream p = new PrintStream(s.getOutputStream());
p.println(number);
temp = c1.nextInt();
System.out.println(temp);
in.close();
s.close();
c1.close();
}
}
Server:
public class ser {
public static void main(String args[]) throws IOException{
ServerSocket s1 = new ServerSocket(1342);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
int number = sc.nextInt();
int temp = number * 2;
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
ss.close();
sc.close();
s1.close();
}
}
1 个解决方案
#1
1
You should use DataInputStream to read your int
and DataOutputStream to write it, it is more appropriate in your case than a Scanner
. You should also consider using try-with-resourses statement to properly close your resources.
您应该使用DataInputStream来读取您的int和DataOutputStream来编写它,它在您的情况下比Scanner更合适。您还应该考虑使用try-with-resourses语句来正确关闭资源。
Your code will then be much easier to read and to maintain which is the best way to avoid bugs.
您的代码将更容易阅读和维护,这是避免错误的最佳方法。
Server:
public class ser {
public static void main(String args[]) throws IOException {
try (ServerSocket s1 = new ServerSocket(1342);
Socket ss = s1.accept();
DataInputStream sc = new DataInputStream(ss.getInputStream());
DataOutputStream p = new DataOutputStream(ss.getOutputStream());
) {
p.writeInt(sc.readInt() * 2);
}
}
}
Client:
public class cli {
public static void main(String args[]) throws IOException {
try (Scanner in = new Scanner(System.in);
Socket s = new Socket("127.0.0.1", 1342);
DataInputStream c1 = new DataInputStream(s.getInputStream());
DataOutputStream p = new DataOutputStream(s.getOutputStream());
){
System.out.println("Enter any number");
int number = in.nextInt();
p.writeInt(number);
System.out.println(c1.readInt());
}
}
}
Output:
Enter any number
12
24
#1
1
You should use DataInputStream to read your int
and DataOutputStream to write it, it is more appropriate in your case than a Scanner
. You should also consider using try-with-resourses statement to properly close your resources.
您应该使用DataInputStream来读取您的int和DataOutputStream来编写它,它在您的情况下比Scanner更合适。您还应该考虑使用try-with-resourses语句来正确关闭资源。
Your code will then be much easier to read and to maintain which is the best way to avoid bugs.
您的代码将更容易阅读和维护,这是避免错误的最佳方法。
Server:
public class ser {
public static void main(String args[]) throws IOException {
try (ServerSocket s1 = new ServerSocket(1342);
Socket ss = s1.accept();
DataInputStream sc = new DataInputStream(ss.getInputStream());
DataOutputStream p = new DataOutputStream(ss.getOutputStream());
) {
p.writeInt(sc.readInt() * 2);
}
}
}
Client:
public class cli {
public static void main(String args[]) throws IOException {
try (Scanner in = new Scanner(System.in);
Socket s = new Socket("127.0.0.1", 1342);
DataInputStream c1 = new DataInputStream(s.getInputStream());
DataOutputStream p = new DataOutputStream(s.getOutputStream());
){
System.out.println("Enter any number");
int number = in.nextInt();
p.writeInt(number);
System.out.println(c1.readInt());
}
}
}
Output:
Enter any number
12
24