【发布时间】:2012-01-13 03:16:51
【问题描述】:
我有 2 个客户端套接字..每个都有自己的 AWT.Frame 作为 GUI 用于聊天.. 在服务器端,我有一个带有 2 个线程的 ServerSocket,用于处理每个客户端。
msg 到流的写入已正确完成,但我无法读取它。在我单击“后,两个线程也终止(我认为主要是由于一些异常。两个客户端窗口上的“发送”按钮..
ChatServer.main() 的代码
public static void main(String args[])throws IOException
{
boolean listening=true;
try
{
try
{
server=new ServerSocket(12591);
}catch(IOException e)
{
System.out.println("Couldn't listen to specified port as it might be already used by some other service");
System.exit(1);
}
System.out.println("Waiting for some client to initiate connection...");
//while (listening)
//{
new ChatServerThread(server.accept()).start();
System.out.println("Connected to User1!");
new ChatServerThread(server.accept()).start();
System.out.println("Connected to User2!");
//}
}catch(SocketException e)
{
System.out.println(e.getMessage());
}
server.close();
}
ChatServerThread.constructor() 和 run() 方法的代码(ChatServerThread 扩展线程)
public ChatServerThread(Socket s)
{
super("ChatServerThread"+(++count));
socket = s;
try
{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}catch(IOException e)
{
System.out.println("Problem getting I/O connection");
System.exit(1);
}
}
public void run()
{
while(true)
{
try
{
String s = in.readUTF();
if(s.equals("DISCONNECT~!@#"))
{
break;
}else
{
ChatServer.chatMsgs.add(s);
System.out.println(s);
//makeClients.c1.display.append(s);
//makeClients.c2.display.append(s);
ChatClient.addMsg2Disp(s);
}
}catch(IOException e)
{
System.out.println("IOException occured");
}
}
}
ChatClient的方法(有GUI):它的构造函数,实现了Listener方法:
public ChatClient()
{
setLayout(new BorderLayout());
bottomPanel=new Panel(new FlowLayout());
bottomPanel.add(txtEntry=new TextArea(4,80));
bottomPanel.add(send=new Button("Send"));
bottomPanel.add(disconnect=new Button("Disconnect"));
add(bottomPanel, BorderLayout.SOUTH);
display=new TextArea();
//display.setEditable(false);
add(display, BorderLayout.CENTER);
try
{
client=new Socket(InetAddress.getLocalHost(), 12591);
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
}catch(UnknownHostException e)
{
System.out.println("Local Host cannot be resolved on which the server is runnig");
System.exit(1);
}catch(IOException e)
{
System.out.println("Problem acquiring I/O Connection.");
System.exit(1);
}
send.addActionListener(this);
disconnect.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(send))
{
try
{
if(!(txtEntry.getText().trim().equals("")))
{
out.writeUTF(txtEntry.getText());
out.flush();
}
}catch(IOException e)
{
System.out.println("IOException occured");
}
}
else if(ae.getSource().equals(disconnect))
{
}
}
static void addMsg2Disp(String msg)
{
display.append(msg);
}
最后还有 1 个名为 makeClients 的类,它实例化 ChatClient 类的 2 个 obj,并设置框架的大小、可见性等......
我认为这是一个大问题,但无法弄清楚为什么无法接收.. 任何可以帮助我的人..提前谢谢! :)
PS:它不是一个真正的应用程序..我正在学习 JAVA 套接字..所以只是尝试编写这样的代码..
【问题讨论】:
-
客户端连接到服务器了吗????目前的输出是什么?
-
@sonuthomas 嗯是的..正在显示接受后说“已连接”的 2 个 SOPln 语句..(这是控制台上服务器端的输出).. 控制台没有输出客户端,因为它只有一个 GUI ..所以我在指定的文本区域中写了一些消息..然后单击发送..在两个客户端上单击发送后,服务器线程终止..我不知道为什么..没有任何例外..并且用于在
displaytextarea 中显示文本的注释行给了我NullPointerException....所以我将 textarea 设为静态并尝试过..但现在仍然无法解决..
标签: java sockets networking serversocket event-listener