当前位置: 代码迷 >> Eclipse >> java 网络编程的一个有关问题
  详细解决方案

java 网络编程的一个有关问题

热度:18   发布时间:2016-04-23 13:57:24.0
java 网络编程的一个问题
我用的是英文版的eclipse,jdk1.6
做到是基于C/S的一个聊天软件,代码如下:

package client;

import java.io.IOException;
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

import common.TextMessage;

/**
 * simple chat client
 */
public class Client implements Runnable {
public static void main(String args[]) throws IOException {
if (args.length != 2)
throw new RuntimeException("Syntax: ChatClient <host> <port>");

Client client = new Client(args[0], Integer.parseInt(args[1]));
new Gui("Chat " + args[0] + ":" + args[1], client);
}

protected ObjectInputStream inputStream;

protected ObjectOutputStream outputStream;

protected Thread thread;

public Client(String host, int port) {
try {
System.out.println("Connecting to " + host + " (port " + port
+ ")...");
Socket s = new Socket(host, port);
this.outputStream = new ObjectOutputStream((s.getOutputStream()));
this.inputStream = new ObjectInputStream((s.getInputStream()));
System.out.println("AAA");
thread = new Thread(this);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* main method. waits for incoming messages.
*/
public void run() {
try {
Thread thisthread = Thread.currentThread();
while (thread == thisthread) {
try {
Object msg = inputStream.readObject();
handleIncomingMessage(msg);
} catch (EOFException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
thread = null;
try {
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

/**
* decides what to do with incoming messages

* @param msg
* the message (Object) received from the sockets
*/
protected void handleIncomingMessage(Object msg) {
if (msg instanceof TextMessage) {
fireAddLine(((TextMessage) msg).getContent() + "\n");
}
}

public void send(String line) {
send(new TextMessage(line));
}

public void send(TextMessage msg) {
try {
outputStream.writeObject(msg);
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
this.stop();
}
}

/**
* listener-list for the observer pattern
*/
private ArrayList listeners = new ArrayList();

/**
* addListner method for the observer pattern
*/
public void addLineListener(ChatLineListener listener) {
listeners.add(listener);
}

/**
* removeListner method for the observer pattern
*/
public void removeLineListener(ChatLineListener listner) {
listeners.remove(listner);
}

/**
* fire Listner method for the observer pattern
*/
public void fireAddLine(String line) {
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
ChatLineListener listener = (ChatLineListener) iterator.next();
listener.newChatLine(line);
}
}

public void stop() {
thread = null;
}
}

报错:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
  相关解决方案