我有一个程序,它是客户端和服务器之间的聊天程序。我根据点击的按钮设置了套接字:
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o==startServer)
{
startServer.enable(false);
try{
listenSocket = new ServerSocket(port);
Socket client = listenSocket.accept();
InputStreamReader is = new InputStreamReader(client.getInputStream());
BufferedReader buffRdr = new BufferedReader(is);
PrintWriter printWrt = new PrintWriter(client.getOutputStream(),true);
service = 1;
sendButton.enable(true);
disconnectButton.enable(true);
sendText.enable(true);
more = true;
}catch(IOException ie){
ie.printStackTrace();
}
}
if(o==connectButton)
{
try{
startServer.enable(false);
connectButton.enable(false);
Socket server = new Socket(host,port);
InputStreamReader is = new InputStreamReader(server.getInputStream());
BufferedReader buffRdr = new BufferedReader(is);
PrintWriter printWrt = new PrintWriter(server.getOutputStream(),true);
service = 2;
sendButton.enable(true);
disconnectButton.enable(true);
sendText.enable(true);
more = true;
}catch(IOException ie){
ie.printStackTrace();
}
}
我运行程序的两个实例,一切似乎连接,但我不知道程序如何知道从我已设置的sendText textBox获取文本。这是我如何设置我的运行但它永远不会到达System.out.println(“服务已更改”)或在程序等待服务从0更改后附加“侦听套接字”。
while(more)
{
statusArea.append("Chat Running");
String line;
try{
System.out.println(service);
while(service==0 && more);
System.out.println("Service Changed");
if(buffRdr!=null)
{
statusArea.append("Listening to socket");
while(more && (line=buffRdr.readLine())!=null)
{
chatArea.append(line);
}
}
}catch(IOException e)
{
}
}
对此的任何帮助都会受到赞赏,我非常喜欢java编程