当前位置:编程学习 > JAVA >>

我用java写的C/S结构聊天室,为什么无法发送消息(另一端收不到)

//客户端
import java.net.*;
import java.io.*;
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
public class chatClient extends JFrame implements ActionListener{
Container container;
JScrollPane scrollpane;
JScrollPane scrollpane2;
JPanel panel;
JTextArea ta1;
JTextArea ta2;
JButton button;
Socket socket;
DataInputStream in;
DataOutputStream out;
String ta1str;
String ta2str;
Boolean  done;
public chatClient(){
try{
socket=new Socket("localhost",50000);
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
done=false;
while(!done){
while((ta1str=in.readUTF())!=null){
ta1.append(ta1str);
}
in.close();
out.close();
done=true;
break;
}
}catch(Exception e){}
}
public void makeWindow(){
this.setTitle("jokerQQ"+"客户端");
this.setSize(550,350);
container=this.getContentPane();
container.setLayout(new BorderLayout());
ta1=new JTextArea();
ta2=new JTextArea();
ta1.setEditable(false);
scrollpane=new JScrollPane(ta1);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel=new JPanel();
button=new JButton("发送");
panel.setLayout(new BorderLayout());
container.add(scrollpane,BorderLayout.CENTER);
container.add(panel,BorderLayout.SOUTH);
panel.add(ta2,BorderLayout.CENTER);
panel.add(button,BorderLayout.EAST);
button.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
ta2str=ta2.getText();
try{
out.writeUTF(ta2str);
}catch(Exception ee){}
}
public static void main(String args[]){
new chatClient().makeWindow();
}
}
//服务端

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class chatServer extends JFrame implements ActionListener{
Container container;
JScrollPane scrollpane;
JScrollPane scrollpane2;
JPanel panel;
JTextArea ta1;
JTextArea ta2;
JButton button;
ServerSocket serversocket;
Socket socket;
DataInputStream in;
DataOutputStream out;
String ta1str;
String ta2str;
Boolean done;
public chatServer(){
try{
serversocket=new ServerSocket(50000);
while(true){//一直在监听
try{
socket=serversocket.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
done=false;
while(!done){
while((ta1str=in.readUTF())!=null){
ta1.append(ta1str);
}
in.close();
out.close();
done=true;
}
}catch(Exception e){}
}


}catch(Exception e){}
}
public void makeWindow(){//聊天界面
this.setTitle("jokerQQ"+"服务器端");
container=this.getContentPane();
this.setSize(550,350);
container.setLayout(new BorderLayout());
button=new JButton("发送");
ta1=new JTextArea();
ta2=new JTextArea();
ta1.setEditable(false);
scrollpane2=new JScrollPane(ta2);

scrollpane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollpane=new JScrollPane();
scrollpane.setViewportView(ta1);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
container.add(scrollpane,BorderLayout.CENTER);
panel=new JPanel();
panel.setLayout(new BorderLayout());
container.add(panel,BorderLayout.SOUTH);
panel.add(scrollpane2,BorderLayout.CENTER);
panel.add(button,BorderLayout.EAST);
button.addActionListener(this);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e){
ta2str=ta2.getText();
try{
out.writeUTF(ta2str);
}catch(Exception ee){}
}




public static void main(String args[]){
new chatServer().makeWindow();
}
}
--------------------编程问答-------------------- 问题太大,代码太乱,不想看了,这是一个简单的demo,群聊(聊天室),可以下源码研究下。注释都很全:http://bbs.csdn.net/topics/390612738 --------------------编程问答-------------------- 那我的代码问题出在哪啊
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,