我有一个名为ClientPeer的类,另外两个是Message和PrivateMessage.I想要将我发送到服务器的文本发送回客户端,但我不知道如何,我尝试了不同的想法,但它没有工作。 这是代码:
import java.net.*;
import java.io.*;
public class ClientPeer extends Thread{
String _username;
Socket _Socket;
public ClientPeer(String _username, Socket _Socket)
{
this._username = _username;
this._Socket = _Socket;
}
public void display()throws Exception
{
ObjectInputStream _ois=new ObjectInputStream(_Socket.getInputStream());
// Message m=(Message)_ois.readObject();
ObjectOutputStream _obs=new ObjectOutputStream(_Socket.getOutputStream());
}
public void run()
{
try {
display();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMessage(String _message) throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(
_Socket.getOutputStream());
_obj.writeObject(new Message(_username, _message));
_obj.flush();
}
public void sendMessage(String _message, String _receiver)
throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(
_Socket.getOutputStream());
_obj.writeObject(new PrivateMessage(_username, _message, _receiver));
_obj.flush();
}
}