简体   繁体   中英

Writing a file using sockets and threads

I have a client/server application using sockets in which the client enters text line by line. On the server side i want that one thread should receive the text one line at a time and the second thread should write the same line to a file. This goes on till client stops. My server is receiving the text lines but the second thread is not writing it to file.

The following is the server thread code that receives the text.

public class EchoServer extends Thread {
    public static String line = "none";

    public EchoServer(int portnum) {
        try {
            server = new ServerSocket(portnum);
        } catch (Exception err) {
            System.out.println(err);
        }
    }

    public void run() {
        try {
            while (true) {
                Socket client = server.accept();
                BufferedReader r = new BufferedReader(
                        new InputStreamReader(client.getInputStream()));
                PrintWriter w = new PrintWriter(client.getOutputStream(),
                        true);
                // w.println("Welcome to the Java EchoServer.  Type 'bye' to close.");
                while ((line = r.readLine()) != null) {
                    // read from Cleint
                    if (line != null) {
                        // p.destroy();
                        System.out.println(line);
                        // sleep(2000);
                    }
                    Thread.yield();
                }
                client.close();
            }
        } catch (Exception err) {
            System.err.println(err);
        }
    }
    private ServerSocket server;
}

The following is the thread that writes to file

public class paste extends Thread {
    // To create producer and consumer as threads
    // Shared variable
    public static int x = 0; // Maximum Limit to simulate a buffer
    public String l;
    public static int j = 1;

    public void run() {
        System.out.println("n");
        if (EchoServer.line != "none") {
            String fileName = "c:/sys prg lab/salfar1.txt";
            try {
                // Assume default encoding.
                FileWriter fileWriter = new FileWriter(fileName, true);
                // Always wrap FileWriter in BufferedWriter.
                BufferedWriter bufferedWriter = new BufferedWriter(
                        fileWriter);
                // Note that write() does not automatically
                // append a newline character.
                bufferedWriter.write(EchoServer.line);
                bufferedWriter.newLine();
                System.out.println("y");
                EchoServer.line = "none";
                // Always close files.
                bufferedWriter.close();
            } catch (IOException ex) {
                System.out.println("Error writing to file '" + fileName
                        + "'");
                // Or we could just do this:
                // ex.printStackTrace();
            }
            Thread.yield();
        }
    }
}

and the following is the main function

public class execute {
    public static void main(String[] args) {
        EchoServer s = new EchoServer(9999);
        paste p = new paste();
        s.start();
        p.start();
    }
}

It seems to me that your second thread runs once and then exits, so when your server receives the text, the writer thread has already returned. Try using a while(true) like you did in the server thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM