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
Java DataOutputSream只工作一次_Java_Sockets - Fatal编程技术网

Java DataOutputSream只工作一次

Java DataOutputSream只工作一次,java,sockets,Java,Sockets,我有一个基本的聊天应用程序。有3个类:服务器、客户端和clientHandler。问题是服务器只能在第一次读取(使用DataInputStream)时抛出异常 Hello. I have a basic chat app. There are 3 classes: server, client and clientHandler. The problem is that server is able to read (using DataInputStream) only first time

我有一个基本的聊天应用程序。有3个类:服务器、客户端和clientHandler。问题是服务器只能在第一次读取(使用DataInputStream)时抛出异常

Hello. I have a basic chat app. There are 3 classes: server, client and clientHandler. The problem is that server is able to read (using DataInputStream) only first time then it throws exceptions.
Client.java:

package Client;

import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;

import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;


public class Client extends JFrame{

private JPanel contentPane;
private JTextField tf_input;
private static JTextArea ta_chat;
private JButton b_connect;


private static int portNumber = 2309;
private static String ip = null;
private JScrollPane scrollPane;
private JTextField tf_ip;


private Socket s = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
private JTextField tf_port;


private boolean connected = false;
private JLabel lblIp;
private JLabel lblPort;
private JLabel lblName;
private JTextField tf_name;
private JButton btnDc;


/**
 * Launch the application.
 */
public static void main(String[] args) {

    Client frame = new Client();
    frame.setVisible(true);


    System.out.println("Client is running!");


    // TODO Auto-generated method stub



    //Listening to server

    while(!frame.connected)
    {
        System.out.println("Waiting...!");
    }

    while(true)
    {
        System.out.println("Listening started!");
        String line = null;

        try {
            line = frame.dis.readUTF();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ta_chat.append("reading from server failed\n");
            //System.out.println(Thread.currentThread().getName() + " failed listening to server!\n");
        }

        if(line!=null)
            ta_chat.append(line.trim() + "\n");
    }
}

    //


/**
 * Create the frame.
 */
public Client() {
    setTitle("Client");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 530, 458);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton b_send = new JButton("Send");
    b_send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if(!connected)
            {
                JOptionPane.showMessageDialog(null, "You have to connect first!");
                return;
            }

            String message = tf_input.getText();

            try {
                ta_chat.append("writing UTF" + '\n');
                dos.writeUTF(message);
                ta_chat.append(ta_chat.getText().trim() + "writing UTF succeded" + '\n');
            } catch (IOException e) {
                // TODO Auto-generated catch block
                ta_chat.append(ta_chat.getText().trim() + "writing UTF failed" + '\n');
            }

            tf_input.setText(null);
        }
    });
    b_send.setBounds(375, 204, 89, 23);
    contentPane.add(b_send);

    tf_input = new JTextField();
    tf_input.setBounds(89, 205, 258, 20);
    contentPane.add(tf_input);
    tf_input.setColumns(10);

    scrollPane = new JScrollPane();
    scrollPane.setBounds(39, 11, 452, 175);
    contentPane.add(scrollPane);

    ta_chat = new JTextArea();
    scrollPane.setViewportView(ta_chat);
    ta_chat.setEditable(false);

    b_connect = new JButton("Connect");
    b_connect.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {

            connected = true;

            String ip = tf_ip.getText();
            String name = tf_name.getText();
            int portNumber = Integer.parseInt(tf_port.getText());

            try {
                s = new Socket(ip, portNumber);
                ta_chat.append("s=" + s + "\n");
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                dis = new DataInputStream(s.getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ta_chat.append("dis init failed\n");
            }

            try {
                dos = new DataOutputStream(s.getOutputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("DataOutputStraem object init failed!");
                ta_chat.append("dos init failed\n");
            }

            try
            {
                dos.writeUTF("/name " + name);
            }
            catch(Exception e)
            {
                ta_chat.append("dos.writeUTF() failed \n");
            }
        }
    });
    b_connect.setBounds(89, 373, 89, 23);
    contentPane.add(b_connect);

    tf_ip = new JTextField();
    tf_ip.setBounds(83, 269, 123, 20);
    contentPane.add(tf_ip);
    tf_ip.setColumns(10);

    tf_port = new JTextField();
    tf_port.setColumns(10);
    tf_port.setBounds(83, 300, 123, 20);
    contentPane.add(tf_port);

    lblIp = new JLabel("IP:");
    lblIp.setBounds(39, 272, 46, 14);
    contentPane.add(lblIp);

    lblPort = new JLabel("Port");
    lblPort.setBounds(39, 303, 46, 14);
    contentPane.add(lblPort);

    lblName = new JLabel("Name:");
    lblName.setBounds(39, 335, 46, 14);
    contentPane.add(lblName);

    tf_name = new JTextField();
    tf_name.setColumns(10);
    tf_name.setBounds(83, 332, 123, 20);
    contentPane.add(tf_name);

    btnDc = new JButton("dc");
    btnDc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                dis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                s.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    btnDc.setBounds(290, 373, 89, 23);
    contentPane.add(btnDc);

}
}

Server.java:

package Server;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Server extends JFrame{

private JPanel contentPane;
private JTextField tf_input;

private static int portNumber = 2309;
private static JTextArea ta_ServerLog;
private JTextField tf_IP;
private JButton btnRefresh;
private JTextField textField;
private JLabel lblPort;
private static ServerSocket ss = null;

/**
 * Launch the application.
 */

public static void addTextToServerLog(String text)
{
    if(text.equals(null))
        return;
    else
        ta_ServerLog.setText(ta_ServerLog.getText().trim() + "\n" + text.trim());
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Server frame = new Server();
                frame.setVisible(true);
            } catch (Exception e) {

            }
        }
    });


    try {
        ss = new ServerSocket(portNumber);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
    }


    while(true)
    {
        Socket s = null;
        try {
            s = ss.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block

        }

        System.out.println("Creating new ClientListener!");
        new ClientHandler(s).start();
    }

}

/**
 * Create the frame.
 */
public Server() {
    setTitle("Server");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 377);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    tf_input = new JTextField();
    tf_input.setBounds(125, 209, 109, 20);
    contentPane.add(tf_input);
    tf_input.setColumns(10);

    JButton b_send = new JButton("Send");
    b_send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    b_send.setBounds(278, 209, 86, 20);
    contentPane.add(b_send);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(49, 26, 294, 136);
    contentPane.add(scrollPane);

    ta_ServerLog = new JTextArea();
    ta_ServerLog.setEditable(false);
    scrollPane.setViewportView(ta_ServerLog);

    JLabel lblYouAreHosting = new JLabel("Your IP:");
    lblYouAreHosting.setBounds(49, 277, 44, 14);
    contentPane.add(lblYouAreHosting);

    tf_IP = new JTextField();
    tf_IP.setEditable(false);
    tf_IP.setBounds(96, 274, 103, 20);
    contentPane.add(tf_IP);
    tf_IP.setColumns(10);

    btnRefresh = new JButton("Refresh IP");
    btnRefresh.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tf_IP.setText(getIP());
        }
    });
    btnRefresh.setBounds(212, 273, 89, 23);
    contentPane.add(btnRefresh);

    textField = new JTextField();
    textField.setText(Integer.toString(portNumber));
    textField.setColumns(10);
    textField.setBounds(96, 305, 103, 20);
    contentPane.add(textField);

    lblPort = new JLabel("Port:");
    lblPort.setBounds(49, 308, 44, 14);
    contentPane.add(lblPort);

    JButton btnNewButton = new JButton("dc");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            ss = null;
        }
    });
    btnNewButton.setBounds(10, 185, 51, 23);
    contentPane.add(btnNewButton);
}

private String getIP()
{
    URL site = null;
    try {
        site = new URL("http://checkip.amazonaws.com");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(site.openStream()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try
    {
        return InetAddress.getLocalHost().getHostAddress().toString();
        //return reader.readLine();
    }
    catch(Exception e)
    {

    }

    return null;
}
}

ClientHandler.java:

package Server;
import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JOptionPane;


public class ClientHandler extends Thread{

private Socket clientSocket = null;
private DataInputStream dis = null;
private static ArrayList<Socket> users = null;


private String getIP()
{
    URL website = null;
    try {
        website = new URL("http://checkip.amazonaws.com");
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(website.openStream()));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        return reader.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}


public ClientHandler(Socket clientSocket)
{

    this.clientSocket = clientSocket;

    if(users==null)
        users = new ArrayList<Socket>();

    try
    {
        users.add(clientSocket);
    }
    catch(Exception ex)
    {
        System.out.println("Adding client's socket to socket list failed!");
    }


    try
    {
        dis = new DataInputStream(clientSocket.getInputStream());
    }
    catch(Exception e)
    {

    }

    Server.addTextToServerLog("Trying to add a new client!");
    /*
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    */
    Server.addTextToServerLog("Client added!"); 
}

public void tellEveryone(String message, String name)
{
    for (Socket socket : users) {
        Socket s = socket;

        try {
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            dos.writeUTF(name + ":" + message);

            dos.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

public void disconnect()
{

}


// reciving messages from a specific client and send to everyone else
public void run()
{   
    while(true)
    {
        String line = null;

        try {
            System.out.println("Reading client input");

            Server.addTextToServerLog("-line-");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            line = dis.readUTF();
            Server.addTextToServerLog("Line=" + line);

            if(line!=null)
            {

                if(line.startsWith("/name "))
                {
                    String name = line.replaceFirst("/name", "");
                    tellEveryone("joined room!", name);
                    Server.addTextToServerLog(name + "joined room!");
                }
                else
                {
                    tellEveryone(line, null);
                    Server.addTextToServerLog(line);
                }

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }   

    }
}
Server.java:(此部分可能是相关部分)

ClientHandler.run():

public void run()
{   
while(true)
{
字符串行=null;
试一试{
System.out.println(“读取客户端输入”);
addTextToServerLog(“-line-”);
试一试{
//睡眠(1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}

line=dis.readUTF();//至少部分问题存在于客户端的
告诉所有人(2)
方法中:

DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(name + ":" + message);
dos.close();
每次发送消息时都会关闭流。因为
dos
是使用客户端的
Socket
实例的输出流创建的,所以每次调用
close()时都会关闭套接字
dos
上的
方法。您可能应该将
dos
存储为客户端类的一个成员变量,并且只有在真正想断开连接时才关闭它

此外,您可能应该在每次发送消息时调用
dos.flush()
,以确保将整个消息写入套接字。有时,
OutputStream
实现将缓存数据,直到达到某个缓冲区大小,然后将整个缓冲区刷新到底层流(在本例中,底层流是套接字本身)。如果不调用
flush()
,则有可能由于缓冲而无法将某些消息写入套接字


确保在整个代码中,除非确实要断开连接,否则不要关闭任何打开的套接字,这可能会解决您的问题。

发布整个异常StackTraceEyes.I removed:I removed dos.close()。现在服务器正在接收消息。但有时客户端不会从服务器接收回输入。这似乎是随机的。即使该客户端没有从服务器接收到消息,他也可以将其发送到服务器,并可以将其重播到其他客户端(这很有效)。如果客户端一次都没有收到消息,他将永远不会收到消息。我的意思是,有些客户端可以工作,但其他客户端无法获得服务器的输出。您现在是否每次发送消息后都调用
dos.flush()
while(true)
    {
        Socket s = null;
        try {
            s = ss.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block

        }

        System.out.println("Creating new ClientListener!");
        new ClientHandler(s).start();
    }
public void run()
{   
    while(true)
    {
        String line = null;

        try {
            System.out.println("Reading client input");

            Server.addTextToServerLog("-line-");
            try {
                //Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            line = dis.readUTF();   // <----------- this works only 1st time  
            Server.addTextToServerLog("Line=" + line);

            if(line!=null)
            {

                if(line.startsWith("/name "))
                {
                    String name = line.replaceFirst("/name", "");
                    tellEveryone("joined room!", name);
                    Server.addTextToServerLog(name + "joined room!");
                }
                else
                {
                    tellEveryone(line, null);
                    Server.addTextToServerLog(line);
                }

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }   

    }
}
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(name + ":" + message);
dos.close();