package yoonforh.generic; /* * Copyright (c) 1996 Yoon Kyung Koo. All Rights Reserved. * * GenericClient.java * Author: Yoon Kyung Koo (yoonforh@interpia.net, http://interpia.net/~yoonforh) * Creation Date: 10/24/96 * UPDATE HSITORY * Update: 01/03/97 * Version: 0.99a (for JDK 1.02 API) * Update: 10/25/96 * Version: 0.99 (for JDK 1.02 API) */ import java.io.*; import java.net.*; public class GenericClient { private String ServerHost; // 서버의 인터넷 주소를 지정한다. private int nServerPort; // 서버의 포트를 지정한다. private Socket connection=null; private DataInputStream input = null; // PrintStream output = null; // there's a bug in the method PrintStream()! private DataOutputStream output = null; GenericClient(String szHost, int nPort) { ServerHost=szHost; nServerPort=nPort; } public DataInputStream getInput() { return input; } public DataOutputStream getOutput() { return output; } public int endSession() { try { connection.close(); // 예외가 발생하면 연결된 소켓을 닫는다. } catch(Exception e) { System.err.println(e.toString()); return -1; } return 0; } public int init(String szData) { try { connection = new Socket(ServerHost, nServerPort); } catch(Exception e) { System.err.println(e.toString()); return -1; } try { // 지정된 주소와 포트를 이용하여 소켓을 생성한 후 서버에 연결까지 한다. input = new DataInputStream(new BufferedInputStream(connection.getInputStream())); // 연결된 소켓에서 입력 스트림을 얻는다. // output = new PrintStream(server.getOutputStream()); output = new DataOutputStream(new BufferedOutputStream(connection.getOutputStream())); // 연결된 소켓에서 얻은 출력 스트림으로 PrintStream 인스턴스를 생성한다. if ( !GenericTool.sendMessage( output, "WORKTYPE", szData )) { System.err.println("Send WORKTYPE message error"); return -1; } if ( !( (GenericTool.getMessage( input, "RESPONSE" )).equals("ACCEPT") ) ) { System.err.println("Received wrong message"); return -1; } } catch(Exception e) { System.err.println(e.toString()); try { connection.close(); // 예외가 발생하면 연결된 소켓을 닫는다. } catch(Exception ex) { System.err.println(ex.toString()); return -1; } return -1; } return 0; } // end of init() }