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

📄 clientconnectionmanager.java

📁 Howdy Communication System的目的是提供一个简单、易用的基于文本的 TCP/IP 通信方法
💻 JAVA
字号:
package Howdy;/*    This file is part of Howdy.    Howdy is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    Howdy is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with Foobar; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/import java.io.*;import java.net.*;import java.util.*;public class ClientConnectionManager implements Runnable // Deals with a connection to the server.{	Socket socket;	BufferedReader in;	PrintWriter out;	int port;	Client client;	String alias;	boolean run;	String input;	Vector chatters;	ClientChatter currentWorkingChatter;	ClientChatter currentChatter2;	ClientChatter currentChatter3;	ClientPinger clientPinger;	Thread cpt;	Date date;	public ClientConnectionManager(Client client)	{		this.client = client;		clientPinger = new ClientPinger(this);		chatters = new Vector();	}	public void run() // Deals with input and closing the socket.	{		try		{			run = true;			while(run == true)			{				input = in.readLine();				if(input == null)					run = false;				else if( input.equals("PONG") )					client.processPong(clientPinger);				else if( input.equals("PING") )					out.println("PONG");				else if( input.startsWith("CHATTERS: ") )					updateChattersVector( input.substring(10) );				else if( input.startsWith("INFO: ") )					updateInfo( input.substring(6) );				else if( input.startsWith("MSG: ") )					client.appendOutput( input.substring(5) );				else if( input.startsWith("VERSION: ") )					client.checkVersion( input.substring(9) );			}			client.warningPopup("The socket has been closed, and thus you have been disconnected.", "Disconnected");			client.setDefaultWindow();		}		catch(Exception e)		{			if(run == true)			{				client.errorPopup("An exception has occured:\n\n" + e, "Exception");				e.printStackTrace();			}		}	}	boolean connection(boolean connect) // Connects and disconnects.	{		try		{			if(connect == true) // Connect.			{				port = client.getPort();				if(port == -1)					return false;				else				{					socket = new Socket(client.serverTF.getText(), port);					if(socket != null)					{						in = new BufferedReader( new InputStreamReader(socket.getInputStream() ) );						out = new PrintWriter(socket.getOutputStream(), true);						setAlias(true);												cpt = new Thread(clientPinger);						cpt.start();												return true;					}					else					{						client.warningPopup("A connection could not be established.", "Connection Error");						return false;					}				}			}			else // Disconnect.			{				run = false;				client.setDefaultWindow();				return true;			}		}		catch(Exception e)		{			client.warningPopup("A connection could not be established:\n\n" + e, "Connection Error");			e.printStackTrace();			return false;		}	}	void sendMessage(String message)	{		out.println("msg: " + message);	}	void sendWhisper(String recipient, String message)	{		out.println("whisper: recipient: " + recipient);		out.println("whisper: msg: " + message);	}	void setAlias(boolean connectnow)	{		String newAlias = client.aliasTF.getText();		if( newAlias.equals(alias) && connectnow == false) { }		else		{			alias = newAlias;			out.println( "alias: " + newAlias );		}	}		void getInfo(String infoAlias)	{		out.println("getinfo: " + infoAlias);	}	void updateInfo(String input)	{		if( input.equals("BEGIN") )			currentChatter2 = new ClientChatter();		else if( input.startsWith("ALIAS: ") )			currentChatter2.alias = input.substring(7);		else if( input.startsWith("IP: ") )			currentChatter2.IP = input.substring(4);		else if( input.startsWith("CONNECTED: ") )			currentChatter2.timeConnected = input.substring(11);		else if( input.startsWith("LAG: ") )			currentChatter2.lag = input.substring(5);		else if( input.equals("END") )		{			for(int i = 0; i < chatters.size(); i++)			{				currentChatter3 = (ClientChatter)chatters.elementAt(i);								if( currentChatter3.alias.equals(currentChatter2.alias) )				{					currentChatter3.IP = currentChatter2.IP;					currentChatter3.timeConnected = currentChatter2.timeConnected;					currentChatter3.lag = currentChatter2.lag;					client.popupInfoBox();				}			}		}	}		void updateChattersVector(String input)	{		if( input.equals("BEGIN LIST") )			chatters.clear();		else if( input.startsWith("BEGIN CHATTER") )			currentWorkingChatter = new ClientChatter();		else if( input.startsWith("ALIAS: ") )			currentWorkingChatter.alias = input.substring(7);		else if( input.equals("END CHATTER") )		{			chatters.addElement(currentWorkingChatter);			currentWorkingChatter = null;		}		else if( input.equals("END LIST") )			client.updateChattersList(chatters);	}}// Copyright (C) 2003-2004 Serban Giuroiu

⌨️ 快捷键说明

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