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

📄 appserver.java

📁 java课程的资料以及实验的代码
💻 JAVA
字号:
import java.awt.event.*;
import java.io.*;
import java.net.*;

class Dealer implements Serializable
{
	String dName;
	String dPwd;	
}

public class AppServer extends Thread
{
	ServerSocket ss;
	
	public AppServer()
	{
		try
		{
			ss = new ServerSocket(5000);
		}
		catch(IOException e)
		{
			fail(e,"无法启动服务器程序...");			
		}
		System.out.println("服务器启动...");	
		this.start();
	}
	
	//fail方法处理所有异常情况
	public static void fail(Exception e,String str)
	{
		System.out.println(str + "." + e.getMessage());
	}
	
	public void run()
	{
		try
		{
			while(true)
			{
				Socket client = ss.accept();
				Connection con = new Connection(client);	
			}			
		}
		catch(IOException e)
		{
			fail(e,"无法监听...");			
		}	
	}
	
	public static void main(String args[])
	{
		new AppServer();
				
	}		
}

class Connection extends Thread
{
	protected Socket netClient;
	protected ObjectInputStream fromClient;
	protected PrintStream toClient;
	
	public Connection(Socket client)
	{
		netClient = client;
		try
		{
			fromClient = new ObjectInputStream(netClient.getInputStream());
			toClient = new PrintStream(netClient.getOutputStream());
		}	
		catch(IOException e)
		{
			try
			{
				netClient.close();	
			}
			catch(IOException e1)
			{
				System.err.println("无法设置流..."+e1);			
			}			
		}
		
		this.start();
	}
	
	public void run()
	{
		Dealer d;

		try
		{
			for(;;)
			{
				//接收来自客户端的对象信息
				d = (Dealer)fromClient.readObject();
				if(d==null)
					break;
				else
					//向客户端返回信息
					toClient.println("接收自 " + d.dName);	
					//屏幕打印登录信息
					System.out.println("接收自 " + d.dName + "密码 " + d.dPwd);	
			}					
		}
		catch(IOException e)
		{
			
		}	
		catch(ClassNotFoundException e1)
		{
			System.out.println("读取对象信息时错误:"+e1);			
		}
		finally
		{
			try
			{
				netClient.close();	
			}	
			catch(IOException e)
			{
			}
		}
	}
}

⌨️ 快捷键说明

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