ServerFrame.java
资源名称:javachat.rar [点击查看]
上传用户:hbgsjx
上传日期:2022-06-25
资源大小:3k
文件大小:8k
源码类别:
ICQ/即时通讯
开发平台:
Java
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.border.*;
- //=================主框架=================================
- public class ServerFrame extends JFrame
- {
- private ServerSocket serverSocket;
- private UserList userlist;
- private ServerThread serverThread;
- Node n;
- public ServerFrame()
- {
- try
- {
- serverSocket=new ServerSocket(8000,10);
- System.out.println("server start at 8000 port......n");
- userlist=new UserList();
- while(true){
- n=new Node();
- n.usersocket=serverSocket.accept();
- userlist.add(n);
- serverThread=new ServerThread(n.usersocket,userlist,n);
- serverThread.start();
- }
- }catch(Exception e){}
- }//-----------------------ServerFrame(主框架)里的一些方法-------------------
- public class ServerThread extends Thread
- {
- Socket usersocket;
- UserList userlist;
- boolean k;
- Node n;
- public ServerThread(Socket usersocket,UserList userlist,Node n)
- {
- this.usersocket=usersocket;
- this.n=n;
- this.userlist=userlist;
- }
- public void run()
- {
- try
- {
- k=true;
- n.output=new ObjectOutputStream(n.usersocket.getOutputStream());
- n.output.flush();
- n.input=new ObjectInputStream(n.usersocket.getInputStream());
- n.username=(String)n.input.readObject();
- System.out.println(n.username+"上线"+"n");
- System.out.println("在线人数"+userlist.getCount()+"人");
- String tosombody;
- String message;
- String msg;
- while(this.k)
- {
- tosombody=(String)n.input.readObject();
- message=(String)n.input.readObject();
- msg=n.username+"对"+tosombody+"说:"+message+"n";
- if(tosombody.equalsIgnoreCase("closed"))
- {
- k=false;
- Node node=userlist.find(n.username);
- node.input.close();
- node.output.close();
- node.usersocket.close();
- System.out.println(n.username+"已下线");
- userlist.del(n);
- System.out.println("在线人数"+userlist.getCount()+"人");
- }
- else
- {
- if(tosombody.equalsIgnoreCase("all"))
- {
- int count=userlist.getCount();
- int i=0;
- while(i<count)
- {
- Node node=userlist.find(i);
- if(node!=null)
- {
- try
- {
- node.output.writeObject("list");
- node.output.flush();
- int j=0;
- String ss="";
- while(j<count)
- {
- Node nn=userlist.find(j);
- if(nn==null)
- {
- j++;continue;
- }
- ss+=nn.username;
- ss+="n";
- j++;
- }
- node.output.writeObject(ss);
- node.output.writeObject("message");
- node.output.flush();
- node.output.writeObject(msg);
- node.output.flush();
- }catch(Exception e){}
- i++;
- }
- }
- }
- else
- {
- Node node=userlist.find(tosombody);
- if(node!=null)
- {
- node.output.writeObject("list");
- node.output.flush();
- int j=0;
- String ss="";
- int count=userlist.getCount();
- while(j<count)
- {
- Node nn=userlist.find(j);
- if(nn==null)
- {
- j++;continue;
- }
- ss+=nn.username;
- ss+="n";
- j++;
- }
- node.output.writeObject(ss);
- node.output.writeObject("message");
- node.output.flush();
- node.output.writeObject(msg);
- }
- }
- }
- }
- }catch(Exception e){}
- }
- }
- public static void main(String args[])
- {
- new ServerFrame();
- }
- }
- //-------------------方法结束--------------------------------
- //====================主框架结束=========================================
- //======================构造客户类,以及编写一些方法===================
- class Node
- {
- String username;
- Socket usersocket;
- ObjectOutputStream output;
- ObjectInputStream input;
- Node next=null;
- }
- class UserList
- {
- Node root;
- Node pointer;
- int count;
- UserList()
- {
- root=new Node();
- root.next=null;
- pointer=null;
- count=0;
- }
- void add(Node n)
- {
- pointer=root;
- while(pointer.next!=null)
- {
- pointer=pointer.next;
- }
- pointer.next=n;
- n.next=null;
- count++;
- }
- void del(Node n)
- {
- pointer=root;
- while(pointer.next!=null)
- {
- if(pointer.next==n)
- {
- pointer.next=n.next;
- count--;
- break;
- }
- pointer=pointer.next;
- }
- }
- int getCount()
- {
- return count;
- }
- Node find(String username)
- {
- if(count==0)return null;
- pointer=root;
- while(pointer.next!=null)
- {
- pointer=pointer.next;
- if(pointer.username.equalsIgnoreCase(username))
- {
- return pointer;
- }
- }
- return null;
- }
- Node find(int index)
- {
- if(count==0)return null;
- if(index<0)return null;
- pointer=root;
- int i=0;
- while(i<index+1)
- {
- if(pointer.next!=null)
- {
- pointer=pointer.next;
- }
- else
- {
- return null;
- }
- i++;
- }
- return pointer;
- }
- }//=====================客户类结束===============================