荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: playboy (冷冷的太阳), 信区: Program
标  题: java server
发信站: BBS 荔园晨风站 (Tue Apr  4 22:08:52 2000), 站内信件

import java .io.*;
import java.net.*;
class sale extends Thread
{

        void set(int k,int j)
         {
                 switch(k)
                 {
                 case 1:
                         if (server.lying<j)
                                 ;
                         else
                                 server.lying -=j;
                         break;
                 case 2:
                         if (server.soft <j)
                                ;
                         else
                                 server.soft -=j;
                         break;
                 case 3:if (server.hard<j)
                                        ;
                                else
                                        server.hard -=j;
                                break;
                 }
        }
}

public class server
{
        static int lying=10,soft=10,hard=10;
        public static void main(String args[]) throws IOException
        {
          sale buy[]=new sale [3];
          for (int i=0;i<3;i++)
          {
                  buy[i]=new sale();
                  buy[i].start ();
          }
          ServerSocket svrsoc=null;
      Socket soc=null;
          DataInputStream in=null;
          PrintStream out=null;
          InetAddress clientIP=null;
          String str=null,str1=null,str2=null;

          try
          {
                  svrsoc=new ServerSocket(8000);
                  soc=svrsoc.accept ();
                  in=new DataInputStream (soc.getInputStream ());

                  clientIP=soc.getInetAddress ();
                  System.out .println ("Client's IP address:"+clientIP);
                  out=new PrintStream (soc.getOutputStream ());
                  out.println ("Welcome...");
                  out.println("Now lying "+lying+";"+" soft "+soft+";"+" hard
"+hard+";");
                  out.println ("Please choose which windows 1,2,3.");
                  str=in.readLine ();
                  while(!str.equals ("quit"))
                        {
                          out.println ("Please choose which tick 1:lying,2:soft;
3:hard");
                          str1=in.readLine ();
                          out.println ("Please input ticket numbers");
                          str2=in.readLine ();

                         switch(Integer.parseInt (str))
                          {
                          case 1:
                                 buy[0].set (Integer.parseInt (str1),Integer
.parseInt (str2));
                                 break;
                          case 2:
                                 buy[1].set (Integer.parseInt (str1),Integer
.parseInt (str2));
                                 break;
                          case 3:
                                 buy[2].set (Integer.parseInt (str1),Integer
.parseInt (str2));
                                 break;
                          }
                         if ((Integer .parseInt (str2)>lying) ||
                                 (Integer .parseInt (str2)>soft)||
                                 (Integer .parseInt (str2)>hard))
                                out.println ("There is not so much tickets.
                                                         Please choose which
windows 1,2,3.");
                         else
                         out.println ("Please choose which windows 1,2,3.");
                     out.println("Now lying "+lying+";"+" "+"soft "
                                                 +soft+";"+" "+"hard "+hard+";")
;
                         str=in.readLine ();

                  }
                  System.out.println ("Client want to leave.");
          }
          catch (Exception e)
          {
                  System.out.println ("Error:"+e);
          }
          finally
          {
                  in.close ();
                  out.close ();
                  soc.close ();
                  svrsoc.close ();
                  System.exit (0);
          }
        }
}

--
☆ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: bbs@192.168.1.90]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店