Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—使用套接字通过不同的方法读取和写入文件_Java_Sockets - Fatal编程技术网

Java—使用套接字通过不同的方法读取和写入文件

Java—使用套接字通过不同的方法读取和写入文件,java,sockets,Java,Sockets,我基本上有一个应用程序,让客户为电影预订电影院。它还拥有对用户数据库进行控制的管理员和负责电影和观看数据库的内容管理员 此时,我们不需要使用实际的数据库,而是需要读取和写入文件。我们必须通过插座来实现这一点。文件位于服务器上,客户端通过套接字之间的连接获取或发送数据 管理员、内容管理员和用户有不同的java文件,每个文件都包含每个用户类使用的方法。所有这些java文件都在客户端 我有以下问题 程序的所有“逻辑”(包含方法的.java文件)都在客户端,这正确吗 我必须为服务器端使用一个套接字,为客

我基本上有一个应用程序,让客户为电影预订电影院。它还拥有对用户数据库进行控制的管理员和负责电影和观看数据库的内容管理员

此时,我们不需要使用实际的数据库,而是需要读取和写入文件。我们必须通过插座来实现这一点。文件位于服务器上,客户端通过套接字之间的连接获取或发送数据

管理员、内容管理员和用户有不同的java文件,每个文件都包含每个用户类使用的方法。所有这些java文件都在客户端

我有以下问题

  • 程序的所有“逻辑”(包含方法的.java文件)都在客户端,这正确吗

  • 我必须为服务器端使用一个套接字,为客户端使用一个套接字吗?在这种情况下,来自不同java文件的所有方法都通过一个客户端套接字进行通信和传递数据

  • 不同的方法读取和写入不同的文件。如何告诉通过套接字从客户端传递到服务器的数据需要写入特定文件?(从适当的文件中读取时相同)

  • 还有两个更具体的例子

    a) 有一种方法允许管理员将用户添加到users.txt文件中。admin通过main方法将userId(用于区分管理员、内容管理员和用户)、name、username和password作为参数提供给addUser()方法。如何通过套接字发送数据并将其写入正确的文件?服务器端是否有一个correspondind方法,该方法有一个writer将数据写入文件?此外,我是将数据全部作为一行发送,还是作为不同的部分发送

    public void createUser(int userId, String name, String username, String password){
    
        try{
    
            PrintWriter Writer = new PrintWriter(new BufferedWriter(new FileWriter("Users.txt", true)));
    
            boolean appendToFile = true;
    
            if (appendToFile) {
                Writer.println(userId + " " + name + " " + username + " " + password);
            }
    
            System.out.println("The user "+getUsername()+" was created!");
            Writer.close();
    
            }
    
            catch (IOException e) {
    
            }
    
    }
    
    b) 另一种方法使客户能够根据关键字搜索电影。我们需要搜索Films.txt文件的每一行(它的结构为filmId(int);filmCategory(enum);filmTitle(string);filmDesription;)。searchViews()方法通过套接字发送用户提供的关键字,在服务器端,它需要在文件的每一行中搜索电影标题中的关键字。同样,是否有一个correspondind方法包含搜索文件每一行的逻辑?否则,只有关键字可用的服务器端如何知道如何处理它

    public void searchViewings(String keyword){
    
        File inputFile = new File("Provoles.txt");
    
        String currentLine = null;
    
        boolean flag = false;
    
        try{
    
            BufferedReader Reader = new BufferedReader(new FileReader(inputFile));
    
            System.out.println("The movies that contain the keyword '"+keyword+"' have the following available viewings:");
    
            while ((currentLine = Reader.readLine()) != null) {
    
                String s = currentLine;
                String delims = ";";
                String[] tokens = s.split(delims);
    
                if (tokens[1].indexOf(keyword) != -1){
    
                    flag = true;
    
                    System.out.println("The movie with Film ID '"+tokens[0]+"' and Film Title '"+tokens[1]+"' has an available viewing at the cinema with ID '"+tokens[2]+"'.");
    
                }
    
            }   
    
            if (flag == false){
    
                System.out.println("There is no movie containing the current keyword.");
    
            }
    
            Reader.close();
    
        }
    
        catch (IOException e) {
    
        }
    }
    
    在实现任何套接字之前,上面的代码都是这样的

  • 最后,是否有一个示例使用多个我可以读取或运行的文件
  • 程序的所有“逻辑”(包含方法的.java文件)都在客户端,这正确吗

    不,服务器端也必须有一些逻辑,用Java或其他语言编写。否则,服务器无法读取文件并执行逻辑或将文件发送到客户端

    我必须为服务器端使用一个套接字,为客户端使用一个套接字吗?在这种情况下,来自不同java文件的所有方法都通过一个客户端套接字进行通信和传递数据

    您不必这样做,为此您当然需要至少一个套接字,但您也可以使用多个套接字。例如,管理员使用的套接字和客户端使用的套接字

    不同的方法读取和写入不同的文件。如何告诉通过套接字从客户端传递到服务器的数据需要写入特定文件?(从适当的文件中读取时相同)

    您可以通过套接字发送一个标题,如:read/file1或write/file4。或者类似的,关键是发送一些数据告诉服务器该做什么

    最后,是否有一个示例使用多个我可以读取或运行的文件

    我现在还没有,但我会在谷歌上搜索一下,如果我找到了,我会在以后更新


    我希望这有帮助:)

    要回答第一个问题,您可以将应用程序逻辑存储在客户端(也称为胖/胖客户端)或服务器(瘦客户端)上。据我所知,没有一个确切的答案是“更好”。这取决于你的应用程序。如果希望大部分处理由服务器完成,请使用瘦客户机(存储在服务器上的逻辑)(对于处理能力有限的设备,如智能手机,需要使用瘦客户机)。如果您希望在客户端上执行更多的处理,或者如果您访问服务器的带宽有限,请使用胖客户端。有很多关于胖客户端和瘦客户端的文章。这是你可能会发现有用的


    我对套接字有点生疏,所以您可能希望采纳另一个答案的建议,但根据我的记忆,每个客户端都有1+套接字,服务器也有1+(取决于您选择如何实现连接)套接字。客户端和服务器将使用相同的端口号。有关一些简单的示例,请参见本文。当我对它们进行实验时,我使用ServerSocket作为服务器,Socket作为客户端。查看上面链接中的不同示例,了解想法。如果您还有其他问题,请告诉我,我会尽力详细说明。

    这是一个简单的项目,使用套接字和文件进行读写,我刚才做过这个项目。希望通过查看此代码可以澄清您对自己代码的疑问。由于时间限制,我没有将其转换为包含您提供的代码段,但是如果您在查看了我的代码后仍然坚持使用自己的代码,我会看看我能做些什么

    如果运行此代码,请注意以下几点:

    • 这个项目使用的服务器和客户端GUI有点凌乱(我不太擅长设计GUI),但功能应该仍然有用
    • 您需要提供与命令行参数相同的端口号
      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.io.EOFException;
      import java.io.File;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.net.ServerSocket;
      import java.net.Socket;
      import java.util.Scanner;
      
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.JTextField;
      import javax.swing.SwingUtilities;
      
      
      public class FileServer extends JFrame {
      //main function
      public static void main(String[] args) {
          int port = Integer.parseInt(args[0]);
          //String file = args[1];
          FileServer server = new FileServer(port);
          server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          server.runServer();
      }
      
      //FileServer class declarations
      private File file; //for this example, the file is stored in the root project directory and includes the file extension (ie: .txt, etc)
      private ServerSocket ss;
      private Socket connection;
      private JTextField field;
      private JTextArea displayArea;
      private int portNum;
      private ObjectOutputStream oos;
      private ObjectInputStream ois;
      
      public FileServer(int port) {
          super("Server");
          //file = new File(f); //sent from client or done here?
          portNum = port;
          field = new JTextField();
          field.setEditable(false);
          field.addActionListener(
                  new ActionListener() {
                      public void actionPerformed(ActionEvent event) {
                          sendData(event.getActionCommand());
                          field.setText("");
                      }
                  }); //ends addActionListener
          add(field, BorderLayout.NORTH);
      
          displayArea = new JTextArea();
          add(new JScrollPane(displayArea));
      
          setSize(500, 300);//size of JFrame
          setVisible(true);
      }//end constructor
      
      /**
       * code that executes the server obj.  Think of it as FileServer's main().  This is called from main().
       */
      public void runServer() {
          try {
              ss = new ServerSocket(portNum, 100); //creates server socket with port # specified by user and a queue of 100
              while (true) { //infinite loop to continually listen for client connections
                  try {
                      waitForConnection(); //wait for client connections
                      getStreams(); //get I/O streams
                      processConnection(); 
                  }
                  catch (EOFException e) {
                      e.printStackTrace();
                  }
                  finally {
                      closeConnection();
                  }
              }
          }
          catch (IOException e) {
              e.printStackTrace();
          }
      }//end runServer
      
      /**
       * creates socket obj to interact with client.  Socket created when connection with client made
       * @throws IOException 
       */
      public void waitForConnection() throws IOException {
          displayMessage("Waiting for connection\n");
          connection = ss.accept(); //returns socket obj when connection made with client
          displayMessage("Connection made with " + connection.getInetAddress().getHostName());
      }//end waitForConnection
      
      /**
       * gets IO stream objs for FileServer class
       */
      public void getStreams() {
          try {
              oos = new ObjectOutputStream(connection.getOutputStream());
              oos.flush();
              ois = new ObjectInputStream(connection.getInputStream());
          }
          catch (IOException e) {
              e.printStackTrace();
          }
          displayMessage("\n Got both IO streams \n");
      }//end getStreams
      
      /**
       * receives filename sent from client and creates file
       */
      public void processConnection() throws IOException {
          sendData("Connection successful");
          setTextFieldEditable(true);
          do { //added do while for testing
              try {
                  String message = (String) ois.readObject(); //should read in filename then create file obj
                  displayMessage("\n " + message + " received from client\n");
                  displayMessage("Type in absolute path of file in text field above");
                  file = new File(message);
                  if(!file.exists()) {
                      sendData("File not found");
                  }
                  else {
                      Scanner cin = new Scanner(file);
                      while(cin.hasNextLine()) {
                          message = cin.nextLine();
                          sendData(message);
                      }
                      cin.close();
                  }
      
              }
              catch (ClassNotFoundException e) {
                  e.printStackTrace();
              }
          } while(true);
      }//end processConnection
      
      /**
       * closes IO streams
       */
      public void closeConnection() throws IOException {
          displayMessage("\nClosing connections\n");
          setTextFieldEditable(false); 
          oos.close();
          ois.close();
          connection.close();
      }//end closeConnection
      
      /**
       * sends message to client
       * @param message
       */
      public void sendData(String message) {
          try{
              oos.writeObject(message);//this is what sends message
              oos.flush();
              displayMessage("\n in sendData: " + message + "\n");
          }
          catch(IOException e) {
              displayArea.append("\n Error writing object");
              e.printStackTrace();
          }
      }//end sendData
      
      public void displayMessage(final String message) {
          SwingUtilities.invokeLater(
                  new Runnable() {
                      public void run() {
                          displayArea.append(message);
                      }
                  });//end SwingUtilties
      }
      
      private void setTextFieldEditable( final boolean editable )
         {
            SwingUtilities.invokeLater(
               new Runnable() 
               {
                  public void run() // sets enterField's editability
                  {
                     field.setEditable( editable );
                  } // end method run
               } // end anonymous inner class
            ); // end call to SwingUtilities.invokeLater
         } // end method setTextFieldEditable
      
      }
      
      import java.io.EOFException;
      import java.io.File;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.net.InetAddress;
      import java.net.Socket;
      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.JTextField;
      import javax.swing.SwingUtilities;
      
      public class FileClient extends JFrame 
      {
          public static void main(String[] args) {
          int port = Integer.parseInt(args[0]);
          String fileName = args[1]; //must use absolute path for filename
          FileClient app = new FileClient("127.0.0.1", port, fileName);
          app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          app.runClient();
      }
      
       private JTextField enterField; // enters information from user
       private JTextArea displayArea; // display information to user
       private ObjectOutputStream output; // output stream to server
       private ObjectInputStream input; // input stream from server
      private String message = ""; // message from server
      private String chatServer; // host server for this application
      private Socket client; // socket to communicate with server
      private int portNum;
      private String fileName;
      private File file;
      
      // initialize chatServer and set up GUI
      public FileClient( String host, int port, String fileName )
      {
        super( "Client" );
        portNum = port;
        this.fileName = fileName;
        //file = new File(fileName);
        chatServer = host; // set server to which this client connectsS
      
        enterField = new JTextField(); // create enterField
        enterField.setEditable( false );
        enterField.addActionListener( //need to find a way to send fileName to server without having to type it in
           new ActionListener() 
           {
              // send message to server
              public void actionPerformed( ActionEvent event )
              {
                 sendData( event.getActionCommand() );
                 enterField.setText( "Messages will be displayed in other text box" );
              } // end method actionPerformed
           } // end anonymous inner class
        ); // end call to addActionListener
      
        add( enterField, BorderLayout.NORTH );
      
        displayArea = new JTextArea(); // create displayArea
        add( new JScrollPane( displayArea ), BorderLayout.CENTER );
      
        setSize( 500, 300 ); // set size of window
        setVisible( true ); // show window
      } // end Client constructor
      
      // connect to server and process messages from server
      public void runClient() 
      {
        try // connect to server, get streams, process connection
        {
           connectToServer(); // create a Socket to make connection
           getStreams(); // get the input and output streams
           processConnection(); // process connection
           sendData(fileName); //sends name of file to server to retrieve
           displayMessage("Sent request for " + fileName + " to server.");
        } // end try
        catch ( EOFException eofException ) 
        {
           displayMessage( "\nClient terminated connection" );
        } // end catch
        catch ( IOException ioException ) 
        {
           ioException.printStackTrace();
        } // end catch
        finally 
        {
           closeConnection(); // close connection
        } // end finally
      } // end method runClient
      
      // connect to server
      private void connectToServer() throws IOException
      {      
        displayMessage( "Attempting connection\n" ); //not getting here
      
        // create Socket to make connection to server
        client = new Socket( InetAddress.getByName( chatServer ), portNum );
      
        // display connection information
        displayMessage( "Connected to: " + 
           client.getInetAddress().getHostName() );
      } // end method connectToServer
      
      // get streams to send and receive data
      private void getStreams() throws IOException
      {
        // set up output stream for objects
        output = new ObjectOutputStream( client.getOutputStream() );      
        output.flush(); // flush output buffer to send header information
      
        // set up input stream for objects
        input = new ObjectInputStream( client.getInputStream() );
      
        displayMessage( "\nGot I/O streams\n" );
       } // end method getStreams
      
       // process connection with server
       private void processConnection() throws IOException //problem possibly due to processConnection being in infinite loop?
      {
        // enable enterField so client user can send messages
        setTextFieldEditable( true );
      
        do // process messages sent from server
        { 
           try // read message and display it
           {
              message = input.readObject().toString(); // read new message
              displayMessage( "\n" + message ); // display message
           } // end try
           catch ( ClassNotFoundException classNotFoundException ) 
           {
              displayMessage( "\nUnknown object type received" );
              classNotFoundException.printStackTrace();
           } // end catch
      
        } while ( !message.equals( "SERVER>>> TERMINATE" ) );
      } // end method processConnection
      
      // close streams and socket
      private void closeConnection() 
      {
        displayMessage( "\nClosing connection" );
        setTextFieldEditable( false ); // disable enterField
      
        try 
        {
           output.close(); // close output stream
           input.close(); // close input stream
           client.close(); // close socket
        } // end try
        catch ( IOException ioException ) 
        {
           ioException.printStackTrace();
        } // end catch
      } // end method closeConnection
      
      // send message to server
      private void sendData( String message ) //need to send filename to server
      {
        try // send object to server
        {
           output.writeObject(message);
           //output.writeObject(fileName); //is writeObject the appropriate write method?
           output.flush(); // flush data to output
           displayMessage( "\nCLIENT>>> " + message );
        } // end try
        catch ( IOException ioException )
        {
           displayArea.append( "\nError writing object" );
           ioException.printStackTrace();
        } // end catch
      } // end method sendData
      
      // manipulates displayArea in the event-dispatch thread
      private void displayMessage( final String messageToDisplay )
      {
        SwingUtilities.invokeLater(
           new Runnable()
           {
              public void run() // updates displayArea
              {
                 displayArea.append( messageToDisplay );
              } // end method run
           }  // end anonymous inner class
        ); // end call to SwingUtilities.invokeLater
      } // end method displayMessage
      
      // manipulates enterField in the event-dispatch thread
      private void setTextFieldEditable( final boolean editable )
      {
        SwingUtilities.invokeLater(
           new Runnable() 
           {
              public void run() // sets enterField's editability
              {
                 enterField.setEditable( editable );
              } // end method run
           } // end anonymous inner class
        ); // end call to SwingUtilities.invokeLater
      } // end method setTextFieldEditable
      } // end class Client