Newer
Older
nonet / src / ch / epfl / lca / sc250 / TCPClient.java
@Andreas Jaggi Andreas Jaggi on 4 May 2006 1 KB Added stub (notworking->commented)
package ch.epfl.lca.sc250;

import java.net.*;
import java.io.*;

import ch.epfl.lca.sc250.gui.*;

public class TCPClient {

	private Socket sock;

	private String targethost;

	private int targetport;

	private String name;

	private	BufferedReader inr;
	private	DataOutputStream outs;

	private CnFrameTCP gui;


	public static void main ( String[] args ) {
		CnFrameTCP gui = new CnFrameTCP("TP 4: TCP Part");
		//Boolean finished = false;

		try {
			TCPClient tc = new TCPClient(gui, "in3sun23.epfl.ch", 13370, "x-way");

			BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

			tc.join();

			/*while ( !finished ) {

				String outBuff = stdin.readLine();
				outs.writeBytes(outBuff);
			}*/

			tc.leave();

		} catch ( IOException ioe ) {
			System.out.println("IO-Error");
			gui.appendText("IO-Error");
		}
	}

	TCPClient ( CnFrameTCP gui, String targethost, int targetport, String name ) throws IOException {
		this.gui = gui;
		this.targethost = targethost;
		this.targetport = targetport;
		this.name = name;

		try {
			sock = new Socket(targethost, targetport);
		} catch ( UnknownHostException uhe ) {
			System.out.println("Unknown host");
			gui.appendText("Unknown host");
		}

		outs = new DataOutputStream(sock.getOutputStream());

		inr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
	}

	public void join ( ) throws IOException {
		outs.writeBytes("Hello: " + name + "\n");
		System.out.println(inr.readLine());
		gui.appendText(inr.readLine());
	}

	public void leave ( ) throws IOException {
		sock.close();
		System.out.println("Connection closed.");
		gui.appendText("Connection closed.");
	}
}