java TCP客户端服务器不能正确发送和接收数据

时间:2013-02-02 19:10:06

标签: java tcp

我制作了一个客户端服务器聊天程序。问题在于客户端只能发送第一个数据,下次客户端不会收到数据。哪部分不正确?


服务器类:


的ChatServer:

public class ChatServer extends Constants implements Runnable
{

private ServerSocket server;
private ExecutorService executor;
private static ArrayList<String[]> IPsAndNames;
private static ArrayList<Object[]> registeredConnections;
private ObjectOutputStream outputStream;

public ChatServer()
{
    try
    {
        IPsAndNames = new ArrayList();
        server = new ServerSocket(SERVER_PORT, 3);
        registeredConnections = new ArrayList();
        executor = Executors.newCachedThreadPool();
    } catch (IOException ex)
    {
        Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void run()
{
    try
    {
        while (true)
        {
            Socket receivedConnection = server.accept();

            outputStream = new ObjectOutputStream(receivedConnection.getOutputStream());
            outputStream.flush();
            registeredConnections.add(new Object[]
                    {
                        receivedConnection, outputStream
                    });
            executor.execute(new SocketManager(this, receivedConnection));
        }
    } catch (IOException ex)
    {
        Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public synchronized void addNewClient(String name, String IP)
{
    IPsAndNames.add(new String[]
            {
                name, IP
            });
}

public synchronized void removeClient(String name, String IP)
{
    IPsAndNames.remove(new String[]
            {
                name, IP
            });
}

public synchronized static ArrayList<ObjectOutputStream> getStreams()
{
    ArrayList<ObjectOutputStream> temp = new ArrayList();
    for (Object[] current : registeredConnections)
    {
        temp.add((ObjectOutputStream) current[1]);
    }
    return temp;
}
}

SocketManager:

public class SocketManager extends Constants implements Runnable
{

private static ObjectInputStream inputStream;
private Object[] objectArrayReceived;
private String clientName;
private ChatServer serverClass;
private String clientIP;
private Calendar calendar;
private SimpleDateFormat timeFormat;
private String serverTime;
private Object[] completeObjects;
private Socket connection;

public SocketManager(ChatServer sourceServer, Socket incomeConnection)
{
    try
    {
        connection = incomeConnection;
        serverClass = sourceServer;
        inputStream = new ObjectInputStream(connection.getInputStream());
        clientIP = incomeConnection.getInetAddress().getHostAddress();
    } catch (IOException ex)
    {
    }
}

@Override
public void run()
{
    try
    {
        clientName = (String) inputStream.readObject();
        serverClass.addNewClient(clientName, clientIP);

        while (true)
        {
            objectArrayReceived = (Object[]) inputStream.readObject();

            //*********
            broadcastReceivedData();

        }
    } catch (IOException ex)
    {
        Logger.getLogger(SocketManager.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex)
    {
        Logger.getLogger(SocketManager.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private void broadcastReceivedData()
{
    ArrayList<ObjectOutputStream> streams = ChatServer.getStreams();
    for (ObjectOutputStream currentStream : streams)
    {
        try
        {
            currentStream.writeObject(objectArrayReceived);
            currentStream.flush();
        } catch (IOException ex)
        {
            Logger.getLogger(SocketManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
}

客户端类:


ClientManager:

public class ClientManager extends Constants implements Runnable
{

private Socket connection;
private String clientName;
private ExecutorService executor;
private static ObjectOutputStream outputStream;
private static ObjectInputStream inputStreaam;

public ClientManager(String name)
{
    clientName = name;
    executor = Executors.newCachedThreadPool();
}

@Override
public void run()
{
    try
    {
        connection = new Socket(InetAddress.getByName(SERVER_ADDRESS), SERVER_PORT);
        inputStreaam = new ObjectInputStream(connection.getInputStream());
        outputStream = new ObjectOutputStream(connection.getOutputStream());
        outputStream.flush();
        outputStream.writeObject(clientName);
        outputStream.flush();
        executor.execute(new DataReceiver());
    } catch (IOException ex)
    {
    }
}

public static ObjectInputStream getInput()
{
    return inputStreaam;
}

public static ObjectOutputStream getOutput()
{
    return outputStream;
}
}

DataReceiver:

public class DataReceiver implements Runnable
{

private ObjectInputStream inputStream;
private Object receivedObject;
private ExecutorService executor = Executors.newCachedThreadPool();

@Override
public void run()
{
    while (true)
    {
        try
        {
            receivedObject = ClientManager.getInput().readObject();
            executor.execute(new AppendIntoTextPane(receivedObject)); // show received data
        } catch (IOException ex)
        {
            Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex)
        {
            Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
}

DataSender:

public class DataSender implements Runnable
{

private Object object;
private static ObjectOutputStream outputStream;

public DataSender(Object receivedObjects)
{
    object = receivedObjects;
}

@Override
public void run()
{
    try
    {
        ClientManager.getOutput().writeObject(object);
        ClientManager.getOutput().flush();
    } catch (IOException ex)
    {
        Logger.getLogger(DataSender.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

0 个答案:

没有答案