home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / prmjatgt / drawserver.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.1 KB  |  147 lines

  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. /**
  6.  *  Server Application that provide Draw Saving using socket
  7.  *  multi thread application
  8.  *
  9.  * @version     1.0 03/24/96
  10.  * @author         Kang, Dae Woong (namu@star.elim.net)
  11.  
  12.  */
  13. public class DrawServer
  14. {
  15.     static public void main(String argv[])
  16.     {
  17.         int port  = (argv.length == 0) ? 3140 : Integer.parseInt(argv[0]);
  18.         
  19.         if (port == 0)
  20.             port = 3140;
  21.  
  22.         System.out.println("Port is setted to " + port);
  23.  
  24.         try 
  25.         {
  26.             ServerSocket svrSock = new ServerSocket(port, 8);  
  27.             while (true) 
  28.             {
  29.                 Socket sock = svrSock.accept();
  30.                 WriteFileThread thread = new WriteFileThread(sock);
  31.                 thread.start();
  32.             }
  33.         }
  34.         catch (IOException e) 
  35.         {
  36.             System.err.println(e);
  37.         }
  38.     }
  39. }
  40.  
  41. class WriteFileThread extends Thread
  42. {
  43.     Socket sock;
  44.     public WriteFileThread(Socket sock)
  45.     {
  46.         this.sock = sock;
  47.     }
  48.  
  49.     public void run()
  50.     {
  51.         String dirName = null;
  52.         String fileName = null;
  53.         boolean isSuccess = false;
  54.         FileOutputStream fos = null;    
  55.         BufferedOutputStream bos = null;    
  56.         try 
  57.         {
  58.             DataInputStream dis = new DataInputStream(sock.getInputStream());
  59.             byte[] names = new byte[100];
  60.             int i = 0, b;
  61.  
  62.             // get Directory, FileName : seperattor = 0, end mark = -1
  63.             while ((b =  dis.readByte()) != -1)
  64.                 names[i++] = (byte) b;
  65.             String buf = new String(names, 0, 0, i);
  66.             int sep = buf.indexOf(0);
  67.             if (sep != -1)
  68.             {
  69.                 dirName = buf.substring(0, sep);
  70.                 fileName = buf.substring(sep + 1, i);            
  71.  
  72.                 fos = new FileOutputStream(dirName + fileName);
  73.                 bos = new BufferedOutputStream(fos);
  74.                 while (true)
  75.                     bos.write(dis.readByte());
  76.             }
  77.         }
  78.         catch (EOFException eofe) //close session normaly
  79.             isSuccess = true;
  80.         catch (IOException ioe) 
  81.             return;
  82.  
  83.         try 
  84.         {
  85.             if (bos != null)
  86.                 bos.flush();
  87.             if (fos != null)
  88.                 fos.close();
  89.             sock.close();
  90.             addIndex(dirName, fileName);        
  91.         }
  92.         catch (IOException ioe2) 
  93.             ;
  94.         
  95.     }
  96.  
  97.     private void addIndex(String dirName, String fileName)
  98.     {
  99.         String indexName = "index.draw";
  100.         RandomAccessFile raf = null;
  101.         try
  102.         {
  103.             // read index file
  104.             Vector fileNameVector = new Vector();
  105.             FileInputStream fis = null;
  106.             try 
  107.             {
  108.                 fis = new FileInputStream(dirName + indexName);
  109.                 DataInputStream dis = new DataInputStream(fis);
  110.                 String str;
  111.                 while (null != (str = dis.readLine()))
  112.                 {
  113.                     if (str.compareTo(fileName) == 0)
  114.                     {
  115.                         fis.close();
  116.                         return;
  117.                     }
  118.                     fileNameVector.addElement(new String(str));
  119.                 }
  120.             }
  121.             catch (IOException ioeIndex)
  122.             {
  123.                 System.out.println("index read error : " + ioeIndex);
  124.             }
  125.  
  126.             if (fis != null)
  127.                 fis.close();    
  128.             
  129.             // write index file
  130.             raf = new RandomAccessFile(dirName + indexName, "rw");
  131.             raf.seek(raf.length());
  132.             raf.writeBytes(fileName + "\n");
  133.             raf.close();
  134.         }
  135.         catch (IOException ioeIndex)
  136.         {
  137.             if (raf != null)
  138.             {
  139.                 try 
  140.                     raf.close();
  141.                 catch (IOException ioe2)
  142.                     System.out.println("close error" + ioe2);
  143.             }
  144.             System.err.println("add index error : " + ioeIndex);
  145.         }
  146.     }
  147. }