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

📄 connect.java

📁 用java开发的聊天程序源代码 包含客户端文件和服务器文件
💻 JAVA
字号:
import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.io.*;

/**
 * 网络连接
 *
 * @author 戚荣波
 *
 * @version 1.0
 * @since J2SE 1.6
 */
public class Connect
{
	/**
	 *等待连接
	 *
	 *@param server 等待连接的ServerSocket
	 *@return Socket连接
	 */
	public static Socket mb_server(ServerSocket server)
	{
		Socket s = null;
		try
		{
			s = server.accept();
		}
		catch(SocketException e)
		{
		}
		catch(Exception e)
		{
			System.out.println("Error");
			e.printStackTrace();
			s = null;
		}
		
		return s;
	}
	
	/**
	 *连接服务器
	 *
	 *@param host 服务器地址
	 *@param port 端口
	 */
	public static Socket mb_client(InetAddress host, int port)
	{
		Socket s;
		try
		{
			s = new Socket(host,port);
		}
		catch(ConnectException e)
		{
			JOptionPane.showMessageDialog(ShowWindow.app, "Can't connect!!!", "alert", JOptionPane.ERROR_MESSAGE);
			SChattingFrame.showMessage("...ready");
			return null;
		}
		catch(Exception e)
		{
			System.out.println("Error");
			e.printStackTrace();
			s = null;
		}
		return s;
		
	}
	
	/**
	 *断开连接
	 *
	 */
	public static void mb_disconnect()
	{
		try
		{
			SChattingFrame.showMessage("Disconnected!");
			if(SomethingStored.s == null)SomethingStored.server.close();
			else
			{
				SChattingFrame.writeSMessage(SMessage.MT_DISCONNECT,null,null,null);
			
				SomethingStored.m_output.flush();
				SomethingStored.m_output.close();
				SomethingStored.m_input.close();
				SomethingStored.s.close();
				SomethingStored.server.close();
				SomethingStored.server = null;
			}
		}
		catch(NullPointerException npe)
		{
		}
		catch(IOException e)
		{
		}
		
		SomethingStored.s = null;
		SChattingMenu.mb_disconnect();
	}
}

⌨️ 快捷键说明

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