Java的UDP通信编写的聊天程序中多线程的问题

老冯_f 发布于 2014/01/10 15:54
阅读 1K+
收藏 2
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

class Text
{
	public static void main(String[]a)
	{
	ChatGui c=new ChatGui();
	}
}
//定义聊天窗口
class ChatGui
{
	private Frame f;
	private TextArea t1;
	private TextArea t2;
	private Button b;
	private Thread th;
	ChatGui()
	{
		info();
	}
	public void info()
	{
		 f=new Frame("聊天,不匹配的端口");
	     f.setBounds(300,100,500,600);
	     f.setResizable(false);
	     f.setLayout(new FlowLayout());
	     f.setVisible(true);
         t1=new TextArea("",20,60,TextArea.SCROLLBARS_VERTICAL_ONLY);
		 t1.setEditable(false);
         t2=new TextArea("",5,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
	     b=new Button("发送");
	     f.add(t1);
	     f.add(t2);
	     f.add(b);
	     myEvent();
         reThread();//开启接收线程

	}
	public void myEvent()
	{
		f.addWindowListener(new WindowAdapter()
			{
		         public void windowClosing(WindowEvent e)
                   {
			           System.exit(0);
		           }
		    });
		b.addActionListener(new ActionListener()
			{
		         public void actionPerformed(ActionEvent e)
		            {
		           seThread();
                   System.out.println(th.getName());//通过测试,这里每次点击发送按钮都会产生新的线程
                    }
		    });

	}
	//线程
	public void reThread()
	{
	      try
			{
		      DatagramSocket recS=new DatagramSocket(10016);
			  new Thread(new Rce(recS)).start();
		     // DatagramSocket sendS=new DatagramSocket();
		     // new Thread(new Sen(sendS)).start();
		    
			}
		   catch (Exception w)
	         {
		throw new RuntimeException("创建线程失败");
	         }
	}
	public void seThread()
	{DatagramSocket sendS=null;
	System.out.println("xiancheng");
						  try
						  { 
							   t1.append("我说:"+"\r\n"+t2.getText()+"\r\n");
				             if(sendS==null)
							  sendS=new DatagramSocket();
							
		                       th=new Thread(new Sen(sendS));
					           th.start();	
							   System.out.println(th.isAlive());
							   } 
							  
						 
						  catch (Exception ew)
	                       {
		                      throw new RuntimeException("xiancheng失败");
	                      }
						  
}
//俩线程内部类
class Sen implements Runnable
{
	private DatagramSocket ds;
    Sen(DatagramSocket ds)
	{
		this.ds=ds;
	}
	public void run()
	{
		try
	      {
		
		
		     byte[] b=t2.getText().getBytes();
			 System.out.println(t2.getText());
			 System.out.println(b.length);
		     DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getByName("192.168.192.255"),10010);
			 System.out.println(dp.getData());
			 System.out.println("vvvvvvvvvvvvv");
		     ds.send(dp);
			 System.out.println("--------");
		    t2.setText("");
		
	      }
	    catch (Exception e)
	      {
		     throw new RuntimeException("发送失败");
	      }
	}
}

class Rce implements Runnable
{
	private DatagramSocket ds;
    Rce(DatagramSocket ds)
	{
		this.ds=ds;
	}
	public void run()
	{
		try
		{
			while(true)
			{
				byte[] b=new byte[1024*64];
				DatagramPacket dp=new DatagramPacket(b,b.length);
				System.out.println("xccccccccccccccccccc");
				ds.receive(dp);
				System.out.println("xssssssssssssssssssssg");
				String ip=dp.getAddress().getHostAddress();
				String data=new String(dp.getData(),0,dp.getLength());
				//System.out.println(ip+"-----"+data);
				t1.append(ip+"-----"+data+"\r\n");
			}
		}
		catch (Exception e)
	{
		throw new RuntimeException("接受失败");
	}
	}
}
}

这里这个聊天窗口的发送线程会随着“发送”按钮随时建立新的线程,这该怎么优化呢



加载中
0
Xingyu_Liu
Xingyu_Liu
将发送线程提升到字段级别,所有的发送操作均使用该线程来完成,线程通过一个消息队列来知晓是否有消息需要发送。
返回顶部
顶部