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

📄 server.java

📁 一个简单的socket聊天工具,实现在发送文件和语音聊天等功能,还能聊天的同时听音乐,分服务器端和客户端两部分
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	public void run(){
		try{
			server=new ServerSocket(5555,10);
			while(true){
				waitForConnection();
				getStreams();
				processConnection();
				closeConnection();
				++counter;
			}
		}
		catch(EOFException e){
			System.out.println("终止连接");
		}
		catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
	
	private void waitForConnection() throws IOException{
		displayArea.setText("正在连接……");
		connect = server.accept();
		client = connect.getInetAddress().getHostAddress();
		displayArea.append("连接数:"+counter);
		enterArea.setEnabled(true);
		sendButton.setEnabled(true);
	}
	
	private void getStreams() throws IOException{
		output=new ObjectOutputStream(connect.getOutputStream());
		output.flush();
		input=new ObjectInputStream(connect.getInputStream());
	}
	
	private void processConnection() throws IOException{
		String message="对方:>>>连接成功!";
		output.writeObject(message);
		output.flush();
	
		do{
			try{
				message=(String)input.readObject();
				if(message.equals("send")){
					receiveButton.setVisible(true);
				}
				else if(message.equals("talk")){
					retalkButton.setVisible(true);
				}
				else if(message.equals("stop")){
					displayArea.append("\n对方断开连接");
				}
				else if(message.equals("cancel")){
					openFile.stop();
				}
				else{
				displayArea.append("\n"+message);
				displayArea.setCaretPosition(displayArea.getText().length());
				}
				if(state){
					sendData("对方暂时离开",1);
				}
			}
			catch(ClassNotFoundException e){
				displayArea.append("\n连接出错");
			}
		}
		while(!message.equals("stop"));
	}
	
	private void closeConnection() throws IOException{
		displayArea.append("\n用户关闭连接");
		enterArea.setEnabled(false);
		sendButton.setEnabled(false);
		output.close();
		input.close();
		connect.close();
	}
	
	private void sendData(String message,int choose){
		if(choose==0){   //0为正常聊天信息
			try{
				Date today = new Date();
				output.writeObject(nickName+"   "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+":\n   "+message);
				output.flush();
				displayArea.append("\n"+nickName+"   "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+":\n   "+message);
			}
			catch(IOException e){
				displayArea.append("\n传送出错");
			}
		}
		if(choose==1){
			try{
				output.writeObject(message);
				output.flush();
			}	
			catch(IOException e){
				displayArea.append("\n传送出错");
			}
		}
	}
	/****************************************/
	






	
	/**************************重写接口*******************/
	public void actionPerformed (ActionEvent e){
		String str = e.getActionCommand();
		
		//处理事件
		if (str.equals("退出")){
			sendData("stop",1);
		}
		if (str.equals("红色")){
			displayArea.setForeground(Color.red);
		}
		if (str.equals("绿色")){
			displayArea.setForeground(Color.green);
		}
		if (str.equals("蓝色")){
			displayArea.setForeground(Color.blue);
		}
		if(str.equals("保存聊天记录")){
			File file = null;
			JFileChooser filechooser = new JFileChooser();;
			if(file!=null) filechooser.setSelectedFile(file);
	        int returnVal=filechooser.showSaveDialog(myframe);
	        if(returnVal==JFileChooser.APPROVE_OPTION){
	        file=filechooser.getSelectedFile();
	        
	        try{
	                    FileWriter fw=new FileWriter(file);
	                    fw.write(displayArea.getText());
	                    fw.close();
	                     }
	                catch(Exception ex){ex.printStackTrace();}
			}
		}
		if(str.equals("发送文件")){
			openFile = new OpenFile(myframe);
			if(openFile.sendBack()!=null){
				sendData("send",1);
				sendData("即将向你发送文件:"+openFile.sendBack(),0);
			}
			openFile.start();
		}

		if(str.equals("发送聊天")){
			Vox vox = new Vox(new String(""));
			vox.start();
			sendData("talk",1);
			sendData("正在呼叫中……",0);
		}
		if(str.equals("接受聊天")){
			Vox vox = new Vox(client);
			vox.start();
			retalkButton.setVisible(false);
		}
		
		if(str.equals("接收文件")){
			Save save = new Save(myframe,1,client);
			save.start();
			receiveButton.setVisible(false);
		}
		
		if(str.equals("聊天模式")){
			displayArea.setVisible(true);
			chat.setVisible(false);
			mess.setVisible(true);
		}
		if(str.equals("消息模式")){
			displayArea.setVisible(false);
			chat.setVisible(true);
			mess.setVisible(false);
		}
	}
	

	public void itemStateChanged (ItemEvent e){
		int st = e.getStateChange();
		JCheckBoxMenuItem source = (JCheckBoxMenuItem) e.getSource();
		int style = displayArea.getFont().getStyle();
		if (source == boldItem){
			if (st == e.SELECTED){
				displayArea.setFont(new Font("SansSerif",style+Font.BOLD,14));
			}
			else {
				displayArea.setFont(new Font("SansSerif",style-Font.BOLD,14));
			}
		}
		else if(source== italicItem){
			if (st == e.SELECTED){
				displayArea.setFont(new Font("SansSerif",style+Font.ITALIC,14));
			}
			else {
				displayArea.setFont(new Font("SansSerif",style-Font.ITALIC,14));
			}
		}
		else if(source == mouseItem1){
			state =!state;
		}
	}
	
	public void mouseReleased(MouseEvent e){
		if(e.isPopupTrigger())
			popupMenu.show(displayArea,e.getX(),e.getY());
	}
	
	public void mouseClicked(MouseEvent e){
		
	}
	
	public void mouseEntered(MouseEvent e){
		
	}
	
	public void mouseExited(MouseEvent e){
		
	}
	
	public void mousePressed(MouseEvent e){
		
	}
		/**public boolean keyDown (Event evt, int key) {
			 if (evt.controlDown()){
			   ……… //显示大写字母
			   else
			   ……… //显示小写字母
			   }
		}*/
	

	/****************************************/
		
	/**	private void prepareSplash() 
		{
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		windowSplash = new Window(myframe); 
		Image image = toolkit.getImage( "images" + File.separator + "splash.gif" ); 
		//ImageCanvas canvas = (ImageCanvas)image; 
		//windowSplash.add( canvas, "Center" ); 
		Dimension scmSize = toolkit.getScreenSize(); 
		int imgWidth = image.getWidth(myframe); 
		int imgHeight = image.getHeight(myframe); 
		windowSplash.setLocation( scmSize.width/2 - (imgWidth/2), scmSize.height/2 - (imgHeight/2)); 
		windowSplash.setSize( imgWidth, imgHeight );
		}
		private void startSplash()
		{
		windowSplash.setVisible( true ); 
		windowSplash.toFront();
		}
		private void stopSplash() 
		{ 
		windowSplash.dispose(); 
		}
*/

	public static void main(String args[]){
		Server server = new Server();
		server.run();
	}
}
			
		
				
	
		
	

⌨️ 快捷键说明

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