Java 将以下客户端服务器应用程序更改为从文件读取

Java 将以下客户端服务器应用程序更改为从文件读取,java,sockets,networking,client-server,Java,Sockets,Networking,Client Server,目前,我有两个应用程序,一个客户端和一个服务器。服务器创建一个套接字,客户端连接到服务器套接字 我希望自定义应用程序,以便客户端可以键入“register”,然后请求提供在服务器上注册帐户所需的信息 问题是,我不知道从哪里开始 这就是我目前拥有的: 客户: import java.io.*; import java.net.*; public class Requester{ Socket requestSocket; ObjectOutputStream out; ObjectInputStr

目前,我有两个应用程序,一个客户端和一个服务器。服务器创建一个套接字,客户端连接到服务器套接字

我希望自定义应用程序,以便客户端可以键入“register”,然后请求提供在服务器上注册帐户所需的信息

问题是,我不知道从哪里开始

这就是我目前拥有的:

客户:

import java.io.*;
import java.net.*;
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run()
{
    try{
        //1. creating a socket to connect to the server
        requestSocket = new Socket("localhost", 2004);
        System.out.println("Connected to localhost in port 2004");
        //2. get Input and Output streams
        out = new ObjectOutputStream(requestSocket.getOutputStream());
        out.flush();
        in = new ObjectInputStream(requestSocket.getInputStream());
        //3: Communicating with the server
        do{
            try{
                message = (String)in.readObject();
                System.out.println("server>" + message);
                sendMessage("Hi my server");
                message = "bye";
                sendMessage(message);
            }
            catch(ClassNotFoundException classNot){
                System.err.println("data received in unknown format");
            }
        }while(!message.equals("bye"));
    }
    catch(UnknownHostException unknownHost){
        System.err.println("You are trying to connect to an unknown host!");
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
    finally{
        //4: Closing connection
        try{
            in.close();
            out.close();
            requestSocket.close();
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
}
void sendMessage(String msg)
{
    try{
        out.writeObject(msg);
        out.flush();
        System.out.println("client>" + msg);
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
}
public static void main(String args[])
{
    Requester client = new Requester();
    client.run();
}
}
服务器:

import java.io.*;
import java.net.*;
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
void run()
{
    try{
        //1. creating a server socket
        providerSocket = new ServerSocket(2004, 10);
        //2. Wait for connection
        System.out.println("Waiting for connection");
        connection = providerSocket.accept();
        System.out.println("Connection received from " + connection.getInetAddress().getHostName());
        //3. get Input and Output streams
        out = new ObjectOutputStream(connection.getOutputStream());
        out.flush();
        in = new ObjectInputStream(connection.getInputStream());
        sendMessage("Connection successful");
        //4. The two parts communicate via the input and output streams
        do{
            try{
                message = (String)in.readObject();
                System.out.println("client>" + message);
                if (message.equals("bye"))
                    sendMessage("bye");
            }
            catch(ClassNotFoundException classnot){
                System.err.println("Data received in unknown format");
            }
        }while(!message.equals("bye"));
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
    finally{
        //4: Closing connection
        try{
            in.close();
            out.close();
            providerSocket.close();
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
}
void sendMessage(String msg)
{
    try{
        out.writeObject(msg);
        out.flush();
        System.out.println("server>" + msg);
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
}
public static void main(String args[])
{
    Provider server = new Provider();
    while(true){
        server.run();
    }
}
}

您的问题(从哪里开始)的答案是,您可能应该从的输入/输出循环开始:

  • 服务器,检查是否接收到“注册”
  • 客户端,添加读取输入的方法,然后将其发送到服务器

  • 作业如果是的话,请给它贴上这样的标签。不是家庭作业,只是在业余时间自学java。自学java、网络编程、协议设计和系统设计是一个令人钦佩的目标。然而,你在这里重新发明了很多轮子。尽管编写代码很有趣,但要认识到在现有框架上构建是软件工程的一个组成部分。如果您的最终目标是编写一个可供他人使用的“真实”应用程序,那么到目前为止,您的代码表明您还有很多需要学习的地方。没问题,我们一开始都是绿色的。不幸的是,这不是一个获得教程级帮助的地方。如果(message.equals(“register”)和(signedin==false))发送消息(“First Name?”),然后等待输入?我该如何等待输入?服务器启动得很好。它已经在循环中等待输入(直到收到带有
    “bye”
    的消息)谢谢,我对i/o的理解是最低限度的。我该如何等待来自客户端的输入?你是说来自客户端的输入(在服务器代码中)或者您的意思是在客户机代码中输入?因为您刚刚处理的服务器代码中的客户机代码!在cleint中,您必须学习如何使用
    系统从控制台读取。在
    中。例如,使用readLine作为初学者。