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

📄 appserver.java

📁 Appplet自己写的一些小程序,对新手很有帮助可以
💻 JAVA
字号:
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;

//The Message class needs to implement serializable
class Customer implements java.io.Serializable
{
	String custName;
	String custPassword;
}
//Code for the AppServer class 

public class AppServer implements Runnable
{
	ServerSocket server;
	Socket fromClient;
	Thread serverThread;
       
      public AppServer()
	{
		
            try
		{	
			server = new ServerSocket(1001);
			serverThread = new Thread(this);
			serverThread.start();		
		}
		catch(Exception e)
		{
			System.out.println("Cannot start the thread" + e);
		}
	}		
	

	public static void main(String args[])
	{
		new AppServer();

	}

	public void run()
	{   
		try
		{
			while(true)
			{
				//Listening to the clients request
				fromClient = server.accept();
				//Creating the connect object 
				Connect con = new Connect(fromClient); 
			}
		}
		catch(Exception e)
		{
   		   System.out.println("Cannot listen to the client" + e);
		}
	}
}

//Code for the connect class 
class Connect extends Thread
{
      PrintStream streamToClient;
	Customer data;
	ObjectInputStream streamFromClient;
      
      static Vector vector = new Vector(1,1);
      static int messageCount;//To count the total number of messages    
                              //stored
      private int localMsgCount;// To count local messages 



	public Connect(Socket inFromClient)
	{
		//Retrieving the clients stream
		try
		{
			streamFromClient = new 	ObjectInputStream(inFromClient.getInputStream());
                        streamToClient= new PrintStream(inFromClient.getOutputStream());
			
		
	 
		}
	        catch(Exception e)
		{
		System.out.println("Cannot get the client stream" + e);
		}
                
                finally
                {
                  try
                  {
                  inFromClient.close();
                  }
                  catch(IOException e)
                  {}
                }
                this.start();
                 
                

	}
       public void run()
        {

               try	
			{
                               

   data=(Customer)streamFromClient.readObject();
   streamToClient.print(data.custName+" Connected");
   
   for(;;)
      {
                                                data=(Customer)streamFromClient.readObject();
      writeMessage(data);//store message
      String message=readMessage();//read messages stored
      streamToClient.print(message);
      }

  }
  catch(InvalidClassException e)
  {
	System.out.println("Cannot serialize the applicant class" + e);
  }
  catch(NotSerializableException e)
  {
	System.out.println("The object is not serializable" + e);
  }
  catch(IOException e)
  {
	System.out.println("Cannot read from the client stream" + e);
  }

  catch(ClassNotFoundException e)
  {
      System.out.println("Customer class could not be found"+e);
  }
                               
 }
   //Method to store message
   synchronized void writeMessage(Customer cust)
   {
       vector.addElement(cust);
       ++messageCount;
       ++localMsgCount;
       notifyAll();
    }
    //Method to retrieve message
    synchronized String readMessage()
    {
       String str=" ";
       while(localMsgCount>=messageCount)
       {
           try
           {
             wait();
           }
           catch(InterruptedException e)
            {}
        }
        for(int i=localMsgCount;i<=messageCount; i++)
        {
                 str=str+vector.elementAt(i);

           }
           notifyAll();
           return str;
       }

}

⌨️ 快捷键说明

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