⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 黑白棋123 黑白棋456 大学毕业设计 做很好
💻 JAVA
字号:
/* ****************************************************************
 *                                        
 *       Server.java                      
 *                                        
 *            Server 类 ,主机端运行, 负责监听及客户端的管理工作     
 *                                          
 *************************************************************** */

import java.io.*;
import java.net.*;
import ConnectThread;

public class Server
{
	ServerSocket s;
	ConnectThread m_connect[];
	byte m_count;
	int MAXCLIENT;
	
	String msou;
	String mobj;
	int mtype;
	String mtxt;
	
	public Server()
	{
		Socket socket;
		socket = null;
		m_count=0;
		MAXCLIENT = 10;
		
		m_connect = new ConnectThread[MAXCLIENT+1];
		int i;
		for(i=0;i<MAXCLIENT;i++)
		{
			m_connect[i]=null;
		}
		
		try 
		{
			s = new ServerSocket(3000);
		}
		catch(Exception e)
		{
			System.out.println("Error in listener newing:"+e);
			return;
		}
		System.out.println("Server is listening ...... ");
		
		while(true)
		{
			try
			{
				socket = s.accept();
			}
			catch (Exception e)
			{
				System.out.println("Error in accepting  : "+e);
			}
			
			if ( socket!= null )
			{
					InitConnect(socket);
			}
			else System.out.println("Error in initing ");

		}
	}
	
	public String WriteMsg(int type,String obj,String txt)
	{
		String buf = new String("MSG");
		Integer _type = new Integer(type);
		buf +=_type.toString();
		buf += '\\';
		buf += obj;
		buf += '\\';
		
		buf += txt;
		return buf;
	}

	public void DealInput(String str,ConnectThread sock)
	{
		System.out.print("    Receive Message:");
		System.out.println(str);
		
		String head = new String();
		String obj ;
		int type;
		int i = 0;
		String txt = new String();
		int len = str.length();
		
		
		//数据预处理
		//消息结构
		// MSG /type (byte), /obj (String) , /0 ,/text (String)
		type = 0;
		obj = "";
		txt = "";

		char[] d = new char[str.length()];
		 str.getChars(0,str.length()-1,d,0);
		
		i=0;
		head = new String ();
		
		if (len < 5 )
			return;
		
		head="";
		head+=str.charAt(0);
		head+=str.charAt(1);
		head+=str.charAt(2);
	
		if (!head.equals("MSG")) 
		{
			System.out.print("     Receive Error Message : ");
			System.out.println(str);
			return;
		}
		
		
		i=3;
		String _type = new String();
		while(str.charAt(i) != '\\')
		{
			_type+=str.charAt(i);
			i++;
		}
		i++;
		type = (Integer.decode(_type)).intValue();
		
		while(str.charAt(i) != '\\')
		{
			obj+=str.charAt(i);
			i++;
		}
		i++;
		
		while(i<len)
		{
			txt+=str.charAt(i);
			i++;
		}
	
	//	txt = str.substring(5,str.length());
	
		// msg type
		// 1 要求当前所有登陆用户名称
		// 2 发送当前所有登陆用户名称
		// 3 向主机发送自己的名字(新登陆)
		// 4 向所有客户发送新登陆着的名字
		// 5 客户断开连接
		// 6 通知所有客户端已断开的客户名
		// 7 发送挑战请求 (执黑)
		// 8 发出挑战请求 (执白)
		// 9 接受挑战回应
		// 10 拒绝挑战回应
		// 11 服务器已满
		// 12 新登陆名字冲突
		// 13 不存在的用户(已断开连接)
		
		// 20 chat
		// 21 走棋
		// 22 密谈
		
		String buf;
		Integer _list;
		
		{
			System.out.println(str);
			
			System.out.print(" Recieve msg , type : ");
			System.out.println(type);
			
			System.out.print("obj : ");
			if (obj.equals ( "" )) System.out.println("null");
			else System.out.println(obj);
		
			
			System.out.print("正文 : ");
			if (txt .equals("") ) System.out.println("null");
			else System.out.println(txt);
			
		}
		
		String msg;
		switch(type)
		{
		case 1:
			{
				//要求当前所有登陆用户名称
				buf= new String();
				
				for(i=0;i<MAXCLIENT;i++)
				{
					if (m_connect[i]!=null)
					{
						_list = new Integer(i);
						buf+=_list.toString();
						buf+='\\';
						buf+= m_connect[i].m_name;
						buf+='\\';
					}
				}
				msg = this.WriteMsg(2,"",buf.toString());
				sock.Send(msg);
				break;
			}
		case 2:
			break;
		case 3:
			{
				//接收到客户端发送自己名字
				for(i=0;i<MAXCLIENT;i++)
					if (m_connect[i] != null )
					if (m_connect[i].m_name.equals(txt))
					{
						//名字已被使用,拒绝连接
						System.out.println("Name has been used , refuse connect");
						msg = this.WriteMsg(12,"","");	
						sock.Send(msg);
						sock.quit = true;
						m_connect[sock.m_id] = null;
					//	sock.stop();
						try
						{
							sock.m_input.close();
							sock.m_output.close();
							sock.m_socket.close();
						}
						catch(IOException e)
						{
							System.out.println("Error at close socket : " +e);
						}
						
						m_count--;
						return;
					}
				
				m_connect[sock.m_id].m_name = txt;
			
				//发送所有登陆着的名字			
				
				buf = "";
				for(i=0;i<MAXCLIENT;i++)
				{
					if (m_connect[i]!=null && i != sock.m_id )
					{
						_list = new Integer(i);
						buf+=_list.toString();
						buf+='\\';
						buf+= m_connect[i].m_name;
						buf+='\\';
					}
				}
				msg = this.WriteMsg(2,"",buf.toString());
				sock.Send(msg);
			
				// 将新名字发给所有用户
				buf = "";
				_list = new Integer(sock.m_id);
				buf+=_list.toString();
				buf+='\\';
				buf+=txt;
				msg = WriteMsg(4,"",buf);
				for(i=0;i<MAXCLIENT;i++)
				{
					if ( m_connect[i] != null && i != sock.m_id )
						m_connect[i].Send(msg);
				}
				break;
			}
		case 5:
			{	
				this.Disconnect(sock);
				break;	
			}

		case 10:
			
		case 4:
		
		case 6:
		case 7:
		case 8:
		case 9:
		case 20:
		case 21:
		default :
			{	//转发
				if (obj.equals( "" ))
				{
					//向所有客户端发送
					System.out.println("Begin transport to all user");
					for(i=0;i<MAXCLIENT ; i++)
					{
						if ( m_connect[i] != null && i != sock.m_id ) 
						{
							System.out.print(" Transport Data from ");
							System.out.print(sock.m_name);
							System.out.print (" to ");
							System.out.print (m_connect[i].m_name);
							System.out.print ( "  :  ");
							System.out.println(str);

							m_connect[i].Send(str);
						}
					}
				}
				else
				for (i = 0; i<MAXCLIENT ; i++)	
				if(m_connect[i] != null)
				if( obj.equals( m_connect[i].m_name ) )
				{
					System.out.println("Begin transport to current user");
					System.out.print(" Transport Data from ");
					System.out.print(sock.m_name);
					System.out.print (" to ");
					System.out.print (m_connect[i].m_name);
					System.out.print ( "  :  ");
					System.out.println(str);

					m_connect[i].Send(str);
				}
				else
					System.out.println("Invalid user  : " + obj); 
				break;
			}

		}
				
	}
	
	public void InitConnect(Socket socket)
	{
		ConnectThread c = new ConnectThread(this,m_count,socket);
		if(m_count>= MAXCLIENT)
		{
			//客户数量已达到上限,拒绝连接
			String msg = this.WriteMsg(11,"","");
			c.Send(msg);
			try
			{
				c.m_input.close();
				c.m_output.close();
				c.m_socket.close();
			}
			catch(IOException e)
			{
				System.out.println( " Error at closing socket : " +e);
				return;
			}
			System.out.println(" A client connect to server , but server is full , refused .");
			return;
		}
		
		
		int i;
		for(i=0;i<MAXCLIENT;i++)
		if(m_connect[i] == null )
		{
			m_connect[i] =c;
			c.m_id = i;
			break;
		}
		c.start();
		System.out.print("Receive Connect ,list ");
		System.out.println(i);
		
		m_count++;
	}
	
	
	public static void main(String args[])
	{
		new Server();
	}
	
	
	public void Disconnect(ConnectThread sock)
	{
		int i;
				
		sock.quit = true;
		m_connect[sock.m_id] = null;
		
		System.out.println("Disconnect with "+ sock.m_name);

		// 通知其他客户
		Integer _id = new Integer(sock.m_id);
		String buf =new String();
		buf+= _id.toString();
		String msg = this.WriteMsg(6,"",buf.toString());

		for (i=0;i<MAXCLIENT;i++)
		{
			if (m_connect[i]!= null )
				m_connect[i].Send(msg);
		}
		m_count --;
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -