客户端连接到服务器,每连一个客户端则起一个线程,现在想把客户端的线程放在队列里,使用队列管理这些客户端线程,求思路,最好有个demo,谢谢。我的客户端、服务器端的代码如下:
客户端:public class ChatClient extends JFrame{
/**
*
*/
private static final long serialVersionUID = -5823809678314632583L;
JTextField tfTxt=new JTextField();
JTextArea taContent=new JTextArea();
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bConnected=false;
private Thread thread=new Thread(new ReceThread() );
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ChatClient().launchFrame();
}
public void launchFrame(){
setLocation(400,300);
this.setSize(300,300);
add(tfTxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
//pack();
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
thread.start();
}
//connect to server
public void connect(){
try {
s=new Socket("10.2.135.42",8800);
bConnected=true;
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
//System.out.println("connected!");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disconnect(){
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//内部类
private class TFListener implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str=tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
// dos.close();
}catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
//接收消息,连接到服务器以后启动该线程
private class ReceThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
try{
while(bConnected){
String str=dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText()+str+"\n");
}
}catch(SocketException e){
System.out.println("退出了,bye");
}catch(EOFException e){
System.out.println("退出了,bye-bye");
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
服务器端代码:public class CharServer { /**
* @param guoyang
*/
ServerSocket ss=null;
boolean started=false;
List<client>list=new ArrayList<client>();//存储client端
//Queue<Thread> queue=new LinkedList<Thread>();
int a=0;//统计在线人数
public static void main(String[] args) {
new CharServer().charServer();
}
public void charServer(){
try{
ss=new ServerSocket(8888);
started=true;
}catch(Exception e){
System.out.println("服务器已经启动");
System.exit(0);
}
try {
while(started){
Socket s=ss.accept();
a++;
System.out.println("当前在线人数"+a);
client c=new client(s);
new Thread(c).start();
list.add(c); }
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class client implements Runnable{
private Socket s=null;
private DataInputStream br=null;
private DataOutputStream bos=null;
boolean start=false;
public client(Socket s){
this.s=s;
try {
br=new DataInputStream(s.getInputStream());
bos=new DataOutputStream(s.getOutputStream());
start=true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str){
try {
bos.writeUTF(str);
bos.flush();
} catch (IOException e) {
list.remove(this);
}
}
@Override
public void run() {
try {
while(start){
String str1=br.readUTF();
for(int i=0;i<list.size();i++){
client c=list.get(i);
c.send(str1);
}
}
}catch (IOException e) {
System.out.println("client closed");
--a;
System.out.println("当前在线数"+a);
}finally{
try {
br.close();
s.close();
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
客户端:public class ChatClient extends JFrame{
/**
*
*/
private static final long serialVersionUID = -5823809678314632583L;
JTextField tfTxt=new JTextField();
JTextArea taContent=new JTextArea();
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bConnected=false;
private Thread thread=new Thread(new ReceThread() );
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ChatClient().launchFrame();
}
public void launchFrame(){
setLocation(400,300);
this.setSize(300,300);
add(tfTxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
//pack();
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
thread.start();
}
//connect to server
public void connect(){
try {
s=new Socket("10.2.135.42",8800);
bConnected=true;
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
//System.out.println("connected!");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disconnect(){
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//内部类
private class TFListener implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str=tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
// dos.close();
}catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
//接收消息,连接到服务器以后启动该线程
private class ReceThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
try{
while(bConnected){
String str=dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText()+str+"\n");
}
}catch(SocketException e){
System.out.println("退出了,bye");
}catch(EOFException e){
System.out.println("退出了,bye-bye");
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
服务器端代码:public class CharServer { /**
* @param guoyang
*/
ServerSocket ss=null;
boolean started=false;
List<client>list=new ArrayList<client>();//存储client端
//Queue<Thread> queue=new LinkedList<Thread>();
int a=0;//统计在线人数
public static void main(String[] args) {
new CharServer().charServer();
}
public void charServer(){
try{
ss=new ServerSocket(8888);
started=true;
}catch(Exception e){
System.out.println("服务器已经启动");
System.exit(0);
}
try {
while(started){
Socket s=ss.accept();
a++;
System.out.println("当前在线人数"+a);
client c=new client(s);
new Thread(c).start();
list.add(c); }
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class client implements Runnable{
private Socket s=null;
private DataInputStream br=null;
private DataOutputStream bos=null;
boolean start=false;
public client(Socket s){
this.s=s;
try {
br=new DataInputStream(s.getInputStream());
bos=new DataOutputStream(s.getOutputStream());
start=true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str){
try {
bos.writeUTF(str);
bos.flush();
} catch (IOException e) {
list.remove(this);
}
}
@Override
public void run() {
try {
while(start){
String str1=br.readUTF();
for(int i=0;i<list.size();i++){
client c=list.get(i);
c.send(str1);
}
}
}catch (IOException e) {
System.out.println("client closed");
--a;
System.out.println("当前在线数"+a);
}finally{
try {
br.close();
s.close();
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
解决方案 »
- java版考试系统,单机版和网络版都行
- List 泛型参数动态的问题
- 紧急求助文件操作的问题!
- InetAddress与URL 区别 java
- Q币悬赏+40分(把adsl的主页的html文件下载保存到本地?)
- 请问,在LINUX和WINDOWS操作系统上运行的JAVA程序性能一样吗?
- 用蚂蚁算法求解排课问题,做过或有该方面资料的朋友给点提示或发到[email protected]
- Need your Help!
- 请问如何生成一个对话框?
- NetBeans中编译出现Encoding问题如何解决(程序中含有中文字符)?
- 有关于java swing的问题,各位大神帮帮忙
- 求教:我的程序引入jxl包,我把我的程序打成jar包后,运行报NoClassFoundError:jxl/write/WritableCell
服务器端在队列取出连接,处理这个连接.
客户端产生与服务器的连接是多线程产生的,这个好像不用管.就是服务器按顺序处理每个连接?不知道我理解得对不对