聊天室程序的问题
我现在正在写一个聊天室程序,我现在只实现了它的部分功能,为什么我只能够把我第一次写的数据发送过去,然后读回来,后面的就不行呢.请高手指点一下下面是我的程序.
//这是客户端的程序前面的大致都可以不看,只看actionperformed里的就可以了,实现的功能大致就是有一个进入聊天室的jb1和发送信息的jb3,两个textarea用来存放接收到的信息和发送信息jrecept和jsend状况是我第一次点发送信息,能够被服务器端收到,第二次就不可以了.
package liaotianshi;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.net.InetAddress;
import java.io.*;
import java.io.IOException;
import java.awt.event.*;
public class Myclient extends JFrame implements ActionListener,Runnable{
/**
* @param args
*/
Socket socket;
Thread thread;
DataInputStream in;
DataOutputStream out;
boolean bool=false;
String name,chtext,chin;
String ip=null;
public static final int PORT=8765;
//设置面板
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JPanel jpanel3=new JPanel();
JPanel jpanel4=new JPanel();
JScrollPane jspane=new JScrollPane();
JScrollPane jspane1=new JScrollPane();
//创建按钮
JButton jb1=new JButton("进入聊天室");
JButton jb2=new JButton("退出聊天室");
JButton jb3=new JButton("发送");
//创建用户输入信息组件
JTextField jname=new JTextField("用户名输入");
JTextField jpw=new JTextField("密码");
JTextArea jsend=new JTextArea();
JTextArea jrecept=new JTextArea();
JTextField jservice=new JTextField();
JTextField jport=new JTextField();
//标签组件
JLabel jl1=new JLabel("用户名");
JLabel jl4=new JLabel("密码");
JLabel jl2=new JLabel("服务器地址:");
JLabel jl3=new JLabel("端口号:");
public Myclient(){
this.setSize(400, 600);
Container content=getContentPane();
content.setLayout(null);
jpanel1.setLayout(null);
jpanel1.setBounds(new Rectangle(0,5,400,35));
jpanel2.setBounds(new Rectangle(5,45,380,230));
jpanel2.setLayout(new GridLayout());
jpanel3.setBounds(new Rectangle(5,180,400,150));
//jpanel3.setLayout(new GridLayout(2,1));
jpanel3.setLayout(null);
jpanel4.setBounds(new Rectangle(5,340,380,180));
jpanel4.setLayout(new GridLayout());
jl1.setBounds(new Rectangle(10,0,80,30));
jpanel1.add(jl1);
jname.setBounds(new Rectangle(55,0,70,25));
jl4.setBounds(new Rectangle(130,0,80,30));
jpanel1.add(jl4);
jpanel1.add(jname);
jpw.setBounds(new Rectangle(160,0,70,25));
jpanel1.add(jpw);
jb1.setBounds(240, 0, 60, 25);
jpanel1.add(jb1);
jb2.setBounds(320, 0, 60, 25);
jpanel1.add(jb2);
content.add(jpanel1,null);
jrecept.setLineWrap(true);
jspane.getViewport().add(jrecept);
jpanel2.add(jspane,null);
content.add(jpanel2,null);
jl2.setBounds(new Rectangle(5,100,100,20));
jpanel3.add(jl2);
jservice.setBounds(new Rectangle(80,100,280,20));
jpanel3.add(jservice);
jl3.setBounds(new Rectangle(5,130,100,20));
jpanel3.add(jl3);
jport.setBounds(new Rectangle(80,130,280,20));
jpanel3.add(jport);
content.add(jpanel3,null);
jsend.setLineWrap(true);
jsend.setBounds(new Rectangle(5,200,350,300));
jspane1.getViewport().add(jsend);
jpanel4.add(jspane1,null);
jb3.setBounds(new Rectangle(250,530,80,25));
content.add(jb3);
content.add(jpanel4,null);
WindowListener windl=new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
Myclient.this.dispose();
}};
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
this.addWindowListener(windl);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1)
{
System.out.println("这是进入聊天室");
try{
socket=new Socket("localhost",1888);
System.out.println("这是进入聊天室");
//in=new DataInputStream(socket.getInputStream());
//out=new DataOutputStream(socket.getOutputStream());
//out.writeUTF("这是我的东东这是从客户端发来的东西");
//String s=in.readUTF();
//if(s!=null)
//System.out.println("读到了服务器"+s);
//else
//System.out.println("读到了服务器"+s);
//jrecept.setText(s);
}
catch(IOException e1)
{
e1.printStackTrace();
}
thread=new Thread(this);
thread.start();
bool=true;
}
if(e.getSource()==jb2)
{
System.out.println("这是退出聊天室");
try{
socket.close();
}
catch(IOException e1){
}
}
if(e.getSource()==jb3)
{
try{
String s=jsend.getText();
System.out.println("这是发送信息"+s);
out=new DataOutputStream(socket.getOutputStream());
if(s!=null)
out.writeUTF(jsend.getText());
else
out.writeUTF("请说话");
//chtext=in.readUTF();
//jrecept.setText(chtext);
}
catch(IOException e1){}
}
}
public void run(){
while(true){
try{
System.out.println("进入了thread of client");
in=new DataInputStream(socket.getInputStream());
chtext=in.readUTF();
System.out.println("读到了服务器"+chtext);
jrecept.setText(chtext);
}
catch(IOException e1){}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Myclient myclient=new Myclient();
}
}
//service端
package liaotianshi;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class service extends JFrame implements ActionListener{
/**
* @param args
*/
JButton jbs1=new JButton("启动服务器");
JButton jb2=new JButton("关闭服务器");
JTextArea jshow=new JTextArea();
ServerSocket server;
Socket socket;
Thread thread;
boolean bool;
public service(){
this.setSize(300, 300);
Container content=getContentPane();
content.setLayout(null);
jbs1.setBounds(new Rectangle(20,5,100,25));
content.add(jbs1);
jb2.setBounds(new Rectangle(130,5,100,25));
content.add(jb2);
jshow.setBounds(new Rectangle(10,40,270,200));
content.add(jshow);
jbs1.addActionListener(this);
jb2.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbs1)
{
System.out.println("点了启动");
servicestart();
}
if(e.getSource()==jb2)
{
servicestop();
}
}
private void servicestart(){
System.out.println("进入启动");
thread=new Thread(new ServerThread());//下面一个进程实现
thread.start();
}
private void servicestop(){
}
public static void main(String[] args) {
// TODO Auto-generated method stub
service serv=new service();
}
}
//实现数据的传送
package liaotianshi;
import java.net.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class ServerThread implements Runnable{
ServerSocket serverthread;
DataInputStream in;
DataOutputStream out;
public ServerThread() {
try{
serverthread=new ServerSocket(1888);
}
catch(IOException e1){
}
}
/**
* @param args
*/
public void run(){
while(true){
try{
System.out.println("进入了thread of server");
Socket socket=serverthread.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
String s=in.readUTF();
if(s!=null)
out.writeUTF(s) //第一次的能够发送回去,第二次就不行了.感觉
//两面都在等对方的一样,结果都没有
else{
out.writeUTF("这是空的啊");
System.out.println("这是空的啊");
}
}
catch(IOException e1){
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}