服务器无法读取客户端消息

时间:2015-07-18 13:46:33

标签: java multithreading tcp

我需要再次帮助套接字编程。现在,我想在使用该命令单击一个按钮后,将我的textfield的值写入linux中的远程服务器日志文件。

"echo '" +textfieldValue+"' >> filename"

我在Server.java中创建了两个线程

  • 第一个线程是将tail -f /root/log.txt的值输出到我的客户端
  • 线程二是读取客户端发送的命令“echo'”+ textfieldValue +“'>> filename”。

我的线程正在运行,但我的线程2没有执行。

请帮忙。

Client.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class Client
{
    Socket sock;
    String server = "XXX.XXX.XX.XX";
    int port = 5550;
    String filename = "/root/log.txt";
    String command;
    UI gui;

    public static void main(String[] args)
    {
        new Client();
    }

    public Client()
    {
        gui = new UI();
        gui.btnListner(new BtnListener());
        openSocket();
        try {
            // read incoming text from server
            BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            String str;
            while ((str = rd.readLine()) != null) {
                System.out.println(str);
            }
            //rd.close();
        } 
        catch (IOException e) 
        {
            System.err.println(e);
        }
    }

    //after the button is being pressed. send command to server
    class BtnListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            String textfieldValue = gui.getTextFieldValue();
            command = "echo '" +textfieldValue+"' >> filename";
            BufferedWriter wr;
            try {
                wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
                wr.write(command);
                wr.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    }
    private void openSocket()
    {
        // open a socket and connect with a timeout limit
        try
        {
            InetAddress addr = InetAddress.getByName(server);
            SocketAddress sockaddr = new InetSocketAddress(addr, port);
            sock = new Socket();

            // this method will block for the defined number of milliseconds
            int timeout = 2000;
            sock.connect(sockaddr, timeout);
        } 
        catch (UnknownHostException e) 
        {
            e.printStackTrace();
        }
        catch (SocketTimeoutException e) 
        {
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

Server.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server  {
    private int portNo = 0;
    private Socket socket = null;

    @SuppressWarnings("resource")
    public Server(int portNo) {
        this.portNo = portNo;
        ServerSocket sSocket = null;
        try {
            sSocket = new ServerSocket(portNo);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while(true) {
            try {
                socket = sSocket.accept();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Thread t = new Thread(new acceptClient());
            t.start();
        }
    }

    class acceptClient implements Runnable {
        public void run() {

            Thread one = new Thread(new t1());
            one.start();

            //thread two not executing!
            Thread two = new Thread(new t2());
            two.start();
        }
    }

    //reading and writing the log file output to client
    class t1 implements Runnable {

        @Override
        public void run() {
            BufferedWriter wr = null;
            Process p = null;
            System.out.println("A client has connected!");
            try {
                p = Runtime.getRuntime().exec("tail -f /root/log.txt");
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            try {
                wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            BufferedReader rd = new BufferedReader(new InputStreamReader(p.getInputStream()));
            try {   
                //rd.close();
                String s = null;
                while ((s = rd.readLine()) != null) {
                    System.out.println(s);
                    wr.write(s+"\n");
                    wr.flush();
                }

            } catch(IOException exception) {
                System.out.println("Error: " + exception);
            }

        }
    }

    //reading and getting the command "echo 'gui.getTextFieldValue()' >> filename" from client
    class t2 implements Runnable {
        @Override
        public void run() {
            //Process p2 = null;
            try {
                BufferedReader rd2 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String s2 = null;
                while ((s2 = rd2.readLine()) != null) {
                    System.out.println(s2);
                    //p2 = Runtime.getRuntime().exec(s2);
                    //new BufferedReader(new InputStreamReader(p2.getInputStream()));
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
    public static void main(String[] args) {
        int portNo = 5550;
        new Server(portNo);
    }
}

1 个答案:

答案 0 :(得分:1)

服务器正在读取行,但客户端没有写行。在邮件中添加行终止符,或使用BufferedWriter.newLine()