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

📄 client.java.bak

📁 java聊天室服务器
💻 BAK
📖 第 1 页 / 共 2 页
字号:
		removeAllFriends();

		/*关闭SOCKET*/
		try
		{
			if( skt != null )
				skt.close();	
			skt = null;
			if( read != null )
				read.close();
			read = null;
		}
		catch ( IOException e )
		{
			JOptionPane.showMessageDialog( this, "关闭socket发生IO异常" );
		}
//		dSkt.close();		//dSkt可以一直使用
		
		write.close();
		ip.setEditable( true );
		port.setEditable( true );
		connect.setEnabled( true );
		tf.setEditable( false );
		send.setEnabled( false );
		quit.setEnabled( false );
		nickName = "xx聊天室";
		setTitle( nickName );

		cThread = null;
		file.setEnabled( false );
		cancel.setEnabled( false );

		try
		{
			doc.remove( 0, doc.getLength() );
			pdoc.remove( 0, pdoc.getLength() );
		}
		catch ( BadLocationException e )
		{
			e.printStackTrace();
		}
	}

	protected void sendMessage( String msg )
	{
	//	JOptionPane.showMessageDialog( this, "要发送的消息是" + msg + "\n状态" + dSkt.isClosed() + 
	//		"\n" + skt.getInetAddress() + " " + serverUDPPort );
		DatagramPacket dp = new DatagramPacket( msg.getBytes(), msg.getBytes().length,
			skt.getInetAddress(), serverUDPPort );
		try
		{
			dSkt.send( dp );
		}
		catch ( Exception e )
		{
			JOptionPane.showMessageDialog( this, "发送消息发生异常" );
			dp = null;
		}

	}

	public void run()
	{
		while( cThread != null )
		{
			try
			{
				
				if( read == null )
					return;

				String s = read.readLine();
				if( s == null )
					continue;
				/*处理消息*/				
				parseMsg( s );				

			}
			catch ( IOException e )
			{
				if( ! skt.isClosed() )
					JOptionPane.showMessageDialog( this, "网络IO异常" );
				leave();				
				return;
			}
		}
	}
	
	private void getNickName()
	{
		/*发送昵称给服务器,服务器判断是否重名*/
		nickNameDlg.showDlg();
	}
	
	private void parseMsg( String s )
	{
		boolean b = nickNameDlg.isVisible();

		if( s == null || s.length() == 0 ) {				
			return;
		}
		
		//检测消息内容
	/*	if( !b )
			JOptionPane.showMessageDialog( this, s );
		else
			JOptionPane.showMessageDialog( nickNameDlg, s );*/

		

		if( s.equals( "NickNameRepeated" )) {			
			JOptionPane.showMessageDialog( nickNameDlg, "已有同名用户,请选择其他名字" );
			nickNameDlg.nickName.setEditable( true );
			nickNameDlg.apply.setEnabled( true );
			return;
		}
		
		//获得焦点
		if( ! isFocused() )
			show();

		StringTokenizer st = new StringTokenizer( s );
		try
		{
			String s1 = st.nextToken();
			String s2 = st.nextToken();
			//如果是服务器发来的欢迎消息
			if( s1.equals( "WELCOME" ) ){
				try
				{
					serverUDPPort = Integer.parseInt( s2 );
					//获得UDP端口号
				//	JOptionPane.showMessageDialog( nickNameDlg, "服务器UDP端口号" + serverUDPPort );
					//将对话框隐藏
					nickNameDlg.setVisible( false );

					ip.setEditable( false );
					port.setEditable( false );
					connect.setEnabled( false );
					quit.setEnabled( true );
					send.setEnabled( true );
					tf.setEditable( true );
					setTitle( nickName );

					appendPublicSystemMsg( "*******************\n欢迎进入XX聊天室\n*******************\n" );
					
					return;
				}
				catch ( NumberFormatException er )
				{
					JOptionPane.showMessageDialog( nickNameDlg, "服务器UDP端口号格式错误");
				}
			}			
			//如果是服务器发来的退出消息
			if( s1.equals( "QUIT" ) ) {
				if( s2.equals( "SERVER" ) ) {
					leave();
					appendPublicSystemMsg( "服务器已关闭\n" );
				}
				else {				
					appendPublicSystemMsg( s2 + "已经离开聊天室\n" );
					removeFriend( s2 );//从好友列表删除用户
					file.setEnabled( false );
					cancel.setEnabled( false );
				}			
				
				return;
			}
			//如果被服务器踢出
			if( s1.equals( "URFUCKED" ) ) {
				//如果是自己被踢
				if( s2.equals( nickName ) ) {
					//发送QUIT消息,调用leave				
					appendPublicSystemMsg( "*******************\n你已经被踢出聊天室\n*******************\n" );
					sendMessage( "QUIT: " + nickName );					
					leave();
				}
				return;
			}
			//如果是好友列表信息
			if( s1.equals( "LIST" ) ) {
				if( s2.equals( nickName ) )
					return;
				else {
					//向table里添加好友名字
					addFriend( s2 );				
					appendPublicSystemMsg( s2 + "加入聊天室\n" );
					return;
				}
			}
			//如果是文件传送信息
			if( s1.equals( "FILE" ) ) {
				if( !s2.equals( nickName ) )
					return;
				try
				{
					String source = st.nextToken();//source					
					String ip = st.nextToken();//ip
					String sPort = st.nextToken();//port					
					String sSize = st.nextToken();
					String filename = s.substring( s1.length() + s2.length() + source.length() + ip.length() +
						sPort.length() + sSize.length() + 6, s.length() );					
					
					
					int port = Integer.parseInt( sPort );
					int size = Integer.parseInt( sSize );
					
					//弹出选择对话框
					int i = JOptionPane.showConfirmDialog( this, source + "请求发送" + filename + 
						"(" + size + "字节)\n是否接收?", "文件传送", JOptionPane.YES_NO_OPTION );
					//拒绝接收
					if( i == JOptionPane.NO_OPTION || i == -1 ) {
						sendMessage( "REJECT " + source + " " + nickName );
						return;
					}
					//另存为
					JFileChooser jfc = new JFileChooser( "." );
					jfc.setMultiSelectionEnabled( false );
					jfc.setSelectedFile( new File( filename ) );
					i = jfc.showSaveDialog( this );

					if( i != JFileChooser.APPROVE_OPTION )
						return;
					
					String filePath = jfc.getSelectedFile().getPath();

					ft = new FileReceiveThread( this, ip, port, filePath ,source );
					((FileReceiveThread)ft).start();

					return;
				}
				catch ( IndexOutOfBoundsException e )
				{
					JOptionPane.showMessageDialog( this, "文件传送消息格式错误IndexOutOfBounds" );
					return;
				}
				catch ( NoSuchElementException e )
				{
					JOptionPane.showMessageDialog( this, "文件传送消息格式错误" );
					return;
				}
				catch ( NumberFormatException e )
				{
					JOptionPane.showMessageDialog( this, "文件传送消息数字格式错误" );
					return;
				}
				catch ( UnknownHostException e )
				{
					JOptionPane.showMessageDialog( this, "无法解析主机地址" );					
					return;
				}
				catch( IOException e )
				{
					JOptionPane.showMessageDialog( this, "无法与主机建立TCP连接" );					
					return;
				}
			}
			//如果是拒绝接收信息
			if( s1.equals( "REJECT" ) ) {
				//不是拒绝你
				if( ! s2.equals( nickName ) )
					return;
				
				try
				{
					String source = st.nextToken();
					appendPersonalSystemMsg( source + "拒绝了传送文件的要求\n" );

					if( ft.getClass() == FileSendThread.class )	{
						((FileSendThread)ft).stop();
						ft.dispose();
					}					
				}
				catch ( NoSuchElementException e )
				{
					JOptionPane.showMessageDialog( this, "拒绝接收文件消息格式错误" );
					return;
				}				
			}
			//如果是中断文件传送信息
			if( s1.equals( "INTERRUPT" ) ) {
				if( ! s2.equals( nickName ) )
					return;

				try
				{
					String source = st.nextToken();
					appendPersonalSystemMsg( source + "中断了文件传送\n" );
					if( ft.getClass() == FileSendThread.class )	{
						((FileSendThread)ft).stop();
						ft.dispose();
					}
					if( ft.getClass() == FileReceiveThread.class ) {
						((FileReceiveThread)ft).stop();
						ft.dispose();
					}					
				}
				catch ( NoSuchElementException e )
				{
					JOptionPane.showMessageDialog( this, "中断接收文件消息格式错误" );
					return;
				}
				return;
			}
			//如果是文件成功发送信息
			if( s1.equals( "SUCCESS" ) ) {
				if( ! s2.equals( nickName ) )
					return;

				try
				{
					String source = st.nextToken();
					appendPersonalSystemMsg( "文件传输完毕\n" );
					if( ft.getClass() == FileSendThread.class )	{
						((FileSendThread)ft).stop();
						ft.dispose();						
					}								
				}
				catch ( NoSuchElementException e )
				{
					JOptionPane.showMessageDialog( this, "中断接收文件消息格式错误" );
					return;
				}
				return;
			}
			//如果是私聊消息
			if( s1.equals( "TALK" ) ) {
				try
				{
					String source = st.nextToken();
					String s3 = s.substring( s1.length() + s2.length() + source.length() + 3, s.length() );
					
					if( s2.equals( nickName ) )
						appendPersonalChatMsg ( source , "你" , s3 + "\n" );
					if( source.equals( nickName ) )
						appendPersonalChatMsg ( "你" , s2, s3 + "\n" );
					return;
				}
				catch ( IndexOutOfBoundsException e )
				{
					JOptionPane.showMessageDialog( this, "悄悄话parse越界" );
				}
				catch ( NoSuchElementException e )
				{
					JOptionPane.showMessageDialog( this, "悄悄话格式错误" );
				}
			}
			//如果是服务器发来的聊天消息
			String s3 = s.substring( s1.length(), s.length() );
			appendPublicChatMsg ( s1 + "\n" ,  s3 + "\n" );
		}
		catch ( NoSuchElementException e )
		{
			if( !b )
				JOptionPane.showMessageDialog( this, "从服务器TCP发来的消息格式错误" );
			else
				JOptionPane.showMessageDialog( nickNameDlg, "从服务器TCP发来的消息格式错误" );
		}

	}
	
	public void addFriend( String nickName ) 
	{
		FriendsTableModel ftm = (FriendsTableModel)friends.getModel();
		if( ! ftm.isFriendExsit( nickName ) ) {
			ftm.addFriend( nickName );
			friends.updateUI();
		}
	}

	public void removeFriend( String nickName ) 
	{
		FriendsTableModel ftm = (FriendsTableModel)friends.getModel();
		ftm.removeFriend( nickName );
		friends.updateUI();
	}
	
	public void removeAllFriends( ) 
	{
		FriendsTableModel ftm = (FriendsTableModel)friends.getModel();
		ftm.clear();
		friends.updateUI();
	}

	public void appendPublicChatMsg( String s1, String s2 ) 
	{
		try
		{
			doc.insertString( doc.getLength(), s1, sacBlue );
			doc.insertString( doc.getLength(), s2, sacBlack );
		}
		catch ( BadLocationException e )
		{
			e.printStackTrace();
		}
		jsp.validate();
		JScrollBar jsb = jsp.getVerticalScrollBar();
		int max = jsb.getMaximum();
		jsb.setValue( max );
	}
	
	public void appendPublicSystemMsg( String s ) 
	{
		try
		{
			doc.insertString( doc.getLength(), s, sacRed );
		}
		catch ( BadLocationException e )
		{
			e.printStackTrace();
		}
		jsp.validate();
		JScrollBar jsb = jsp.getVerticalScrollBar();
		int max = jsb.getMaximum();
		jsb.setValue( max );
	}
	
	public void appendPersonalChatMsg ( String s1, String s2, String s3 ) 
	{
		try
		{
			pdoc.insertString( pdoc.getLength(), s1, sacBlue );
			pdoc.insertString( pdoc.getLength(), " 悄悄地对 ", sacBlack );
			pdoc.insertString( pdoc.getLength(), s2 , sacBlue );
			pdoc.insertString( pdoc.getLength(), " 说:\n", sacBlack );
			pdoc.insertString( pdoc.getLength(), s3 , sacBlack );
		}
		catch ( BadLocationException e )
		{
			e.printStackTrace();
		}
		jsp.validate();
		JScrollBar jsb = jsp.getVerticalScrollBar();
		int max = jsb.getMaximum();
		jsb.setValue( max );
	}

	public void appendPersonalSystemMsg ( String s ) 
	{
		try
		{
			pdoc.insertString( pdoc.getLength(), s, sacRed );
		}
		catch ( BadLocationException e )
		{
			e.printStackTrace();
		}
		pjsp.validate();
		JScrollBar jsb = pjsp.getVerticalScrollBar();
		int max = jsb.getMaximum();
		jsb.setValue( max );
	}
	
	public static void main(String[] args) 
	{	
		Client client = new Client();
		client.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );		

		client.setVisible( true );				
	}
	
}

⌨️ 快捷键说明

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