package project.UserClient;

import project.common.*;
import java.io.*;
import java.net.*;

public class ClientConnection {

	Socket sock;
	BufferedIO io;

	public ClientConnection()
	{

	}

	public BufferedIO newConnection(String server, int port)
	{
		BufferedInputStream sockIn;
		PrintStream sockOut;

		try
		{
			sock = new Socket(server, port);
			sockIn = new BufferedInputStream(sock.getInputStream());
			sockOut  = new PrintStream(sock.getOutputStream());
			
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
			return null;
		}
		io = new BufferedIO(sockIn, sockOut);
		String s = io.readln();
		if(s == null)
			return null;
		System.out.println(s);
		return io;
	}

	public void terminateConnection()
	{
		try {
			io.println("QUIT");
			io.readln();
			sock.close();
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
		}			
	}
}
