home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 3.1 KB | 147 lines |
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
- /**
- * Server Application that provide Draw Saving using socket
- * multi thread application
- *
- * @version 1.0 03/24/96
- * @author Kang, Dae Woong (namu@star.elim.net)
-
- */
- public class DrawServer
- {
- static public void main(String argv[])
- {
- int port = (argv.length == 0) ? 3140 : Integer.parseInt(argv[0]);
-
- if (port == 0)
- port = 3140;
-
- System.out.println("Port is setted to " + port);
-
- try
- {
- ServerSocket svrSock = new ServerSocket(port, 8);
- while (true)
- {
- Socket sock = svrSock.accept();
- WriteFileThread thread = new WriteFileThread(sock);
- thread.start();
- }
- }
- catch (IOException e)
- {
- System.err.println(e);
- }
- }
- }
-
- class WriteFileThread extends Thread
- {
- Socket sock;
- public WriteFileThread(Socket sock)
- {
- this.sock = sock;
- }
-
- public void run()
- {
- String dirName = null;
- String fileName = null;
- boolean isSuccess = false;
- FileOutputStream fos = null;
- BufferedOutputStream bos = null;
- try
- {
- DataInputStream dis = new DataInputStream(sock.getInputStream());
- byte[] names = new byte[100];
- int i = 0, b;
-
- // get Directory, FileName : seperattor = 0, end mark = -1
- while ((b = dis.readByte()) != -1)
- names[i++] = (byte) b;
- String buf = new String(names, 0, 0, i);
- int sep = buf.indexOf(0);
- if (sep != -1)
- {
- dirName = buf.substring(0, sep);
- fileName = buf.substring(sep + 1, i);
-
- fos = new FileOutputStream(dirName + fileName);
- bos = new BufferedOutputStream(fos);
- while (true)
- bos.write(dis.readByte());
- }
- }
- catch (EOFException eofe) //close session normaly
- isSuccess = true;
- catch (IOException ioe)
- return;
-
- try
- {
- if (bos != null)
- bos.flush();
- if (fos != null)
- fos.close();
- sock.close();
- addIndex(dirName, fileName);
- }
- catch (IOException ioe2)
- ;
-
- }
-
- private void addIndex(String dirName, String fileName)
- {
- String indexName = "index.draw";
- RandomAccessFile raf = null;
- try
- {
- // read index file
- Vector fileNameVector = new Vector();
- FileInputStream fis = null;
- try
- {
- fis = new FileInputStream(dirName + indexName);
- DataInputStream dis = new DataInputStream(fis);
- String str;
- while (null != (str = dis.readLine()))
- {
- if (str.compareTo(fileName) == 0)
- {
- fis.close();
- return;
- }
- fileNameVector.addElement(new String(str));
- }
- }
- catch (IOException ioeIndex)
- {
- System.out.println("index read error : " + ioeIndex);
- }
-
- if (fis != null)
- fis.close();
-
- // write index file
- raf = new RandomAccessFile(dirName + indexName, "rw");
- raf.seek(raf.length());
- raf.writeBytes(fileName + "\n");
- raf.close();
- }
- catch (IOException ioeIndex)
- {
- if (raf != null)
- {
- try
- raf.close();
- catch (IOException ioe2)
- System.out.println("close error" + ioe2);
- }
- System.err.println("add index error : " + ioeIndex);
- }
- }
- }