0

ファイルからテキストを取得し、サーバーに送信して大文字にしてから、クライアントに送信して印刷したいと考えています。どうすればこれを達成できますか?

1行を読み取ってクライアントに送り返すことができ、サーバーが読み取るために複数の行を出力ストリームに書き込むことができましたが、今何をすべきかわかりません..

ファイルからテキストを読み取るクライアントがあります。

import java.io.*;
import java.net.*;
import java.util.Date;

public class Client 
{
    public static void main(String[] args)
    {
        try
        {
            // First create the input from keyboard
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Client Program");

            // Next we need to find out the IP address and port number of the server
            System.out.print("Enter IP Address of server: ");
            String ip = input.readLine();
            System.out.print("Enter port number of server: ");
            String port_string = input.readLine();

            // The port number needs to be an int, so convert the string to an int
            int port = Integer.parseInt(port_string);

            // Connect to the server
            Socket sock = new Socket(ip, port);


            // Create the incoming stream to read messages from
            DataInputStream network = new DataInputStream(sock.getInputStream());

            //Create the output stream to the client
            DataOutputStream message = new DataOutputStream(sock.getOutputStream());

            //Send message
            //message.writeUTF("some text");
            FileReader file = new FileReader("text.dat");
            BufferedReader input_file = new BufferedReader(file);

            // Loop until EOF
            while (input_file.ready()){
            // Read next line from the file
                String line = input_file.readLine();
                // Write line to server
                message.writeUTF(line + "\n");
                //System.out.println(line);
            }

            // Display our address
            System.out.println("Address: " + sock.getInetAddress());
            String line;

            // Loop until the connection closes, reading from the network
            while ((line = network.readUTF()) != null)
            {
                // Display the received message
                System.out.println(line);
            }
        }
        catch (IOException ioe)
        {
            // This is expected when the server closes the network connection
            System.err.println("Error in I/O");
            System.err.println(ioe.getMessage());
            System.exit(-1);
        }
    }
}

そして、それらの文字列を取得して大文字にするサーバー:

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

public class Server 
{
    public static void main(String[] args)
    {
        try
        {
            // First create the input from the keyboard
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Server Program");

            // Get the port to listen on
            System.out.print("Enter port number to listen on: ");
            String port_string = input.readLine();

            // The port number needs to be an int, so convert the String to an int
            int port = Integer.parseInt(port_string);

            // Create a ServerSocket to listen on this address
            ServerSocket server = new ServerSocket(port);

            // Accept an incoming client connection on the server socket
            Socket sock = server.accept();

            // Create the output stream to the client
            DataOutputStream network = new DataOutputStream(sock.getOutputStream());

            //Create the incoming stream to read messages from
            DataInputStream message =  new DataInputStream(sock.getInputStream());



            String newLine = inFromClient.readLine();

            //Line to read
            String line;
            line = message.readUTF();
            line = line.toUpperCase();




            // Send message
            network.writeUTF(newLine);

            // Close sockets.  This will cause the client to exit
            sock.close();
            server.close();
        }
        catch (IOException ioe)
        {
            System.err.println("Error in I/O");
            System.err.println(ioe.getMessage());
            System.exit(-1);
        }
    }
}
4

2 に答える 2

2

問題は、サーバーがストリームを 1 回だけ読み取り、ソケット接続を閉じていることです。サーバーソケットへのクライアントデータの送信が完了したことをサーバーはどのように知るのでしょうか? テキスト全体の送信が完了するまで、ポートをリッスンするようにサーバーを変更する必要があります。そのためには、次のようにします-

String newLine;
while ( true )
newLine = inFromClient.readLine();              
          if (newLine.equalsIgnoreCase("END"))
{
    break;
}

          newLine = newLine.toUpperCase();                 

// Send message             
          network.writeUTF(newLine);
}
// Close sockets.  This will cause the client to exit

sock.close();               
server.close();

そして、すべての行が送信された後、クライアントから「END」が送信されます。

于 2012-10-05T13:41:46.050 に答える
1

最も簡単な方法は、Apache CommonsのIOUtilsを使用することです。IOUtils.readLines は文字列のリストを返します

まったく同じ質問: InputStream を文字列に読み取り/変換する

于 2012-10-05T12:49:53.537 に答える