使用Java套接字的客户机-服务器通信

使用Java套接字的客户机-服务器通信,java,sockets,Java,Sockets,我用java构建了一个带有客户端和服务器套接字的程序,该程序从客户端获取一个数字,并将其乘以2。 代码不允许我在客户端输入数字。 代码: 客户端: public class cli { public static void main(String args[]) throws UnknownHostException, IOException{ Scanner in = new Scanner(System.in); int number,temp;

我用java构建了一个带有客户端和服务器套接字的程序,该程序从客户端获取一个数字,并将其乘以2。 代码不允许我在客户端输入数字。 代码:

客户端:

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();
    }
}
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();
    }
}
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);
        }
    }
}
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());
        }
    }
}
Enter any number
12
24
服务器:

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();
    }
}
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();
    }
}
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);
        }
    }
}
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());
        }
    }
}
Enter any number
12
24
您应该使用来读取和写入
int
,它比
扫描仪更适合您的情况。还应该考虑使用语句正确关闭资源。

您的代码将更易于阅读和维护,这是避免bug的最佳方法

服务器:

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();
    }
}
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();
    }
}
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);
        }
    }
}
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());
        }
    }
}
Enter any number
12
24
客户端:

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();
    }
}
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();
    }
}
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);
        }
    }
}
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());
        }
    }
}
Enter any number
12
24
输出:

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();
    }
}
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();
    }
}
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);
        }
    }
}
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());
        }
    }
}
Enter any number
12
24

你的问题没有被很好地描述。当你运行它时会发生什么?你说“不让我”是什么意思?你得到的堆栈跟踪是什么。我试过同样的代码,效果很好!