import java.net.*;
import java.applet.*;
import java.awt.*;
import java.io.*;

class Client
{
	InputStream input = null;
	PrintStream output = null;
        Mud mud = null;
        Socket server = null;
        int y=10;

	public Client(String serverName, int port, Mud thismud)
	{
		Socket server = null;
		int c;
                this.mud = thismud;
		try
		{
			server = new Socket(serverName, port);
			input = server.getInputStream();
			output = new PrintStream(server.getOutputStream());

		}
		catch(Exception exception)
		{
			mud.log.setText("Exception:\n" + exception);
			try
			{
				server.close();
			}
			catch(Exception e)
			{
				mud.log.setText("Exception:\n" + e);
				/* System.exit(1); */
			}
			/* System.exit(1); */
		}
        }


        public String ReadString()
        {
		int nbytes;
		byte b[] = new byte[4096];
		try
		{
                        while((nbytes = input.read(b,0,4096)) > 0)
                        {
                           String str = new String(b,0,0,nbytes);
                           //str.replace('\r',' ');
                           //return("Received from server: " + str);
                        mud.log.setText("packet size=" + nbytes + "\n");
                           //mud.log.appendText(".");
                           return(str);
                        } 
                        mud.log.setText("packet size=" + nbytes + "\n");
                        if ( (nbytes==0) || (nbytes<0) ){
                          //mud.log.appendText("^"); 
                          return(null);
                        }
		}
		catch(Exception exception)
		{
			mud.log.setText("Exception:\n" + exception);
/*
			try
			{
				server.close();
			}
			catch(Exception e)
			{
				mud.log.setText("Exception:\n" + e);
				return(null);
			}
*/
			return(null);
		}
                return("Error");
	}

     
        public void WriteString(String s)
        {
                try{
                   output.println(s);
                   //mud.out.appendText(mud.connection.ReadString());
                }
                catch(Exception exception)
                {
                        mud.log.setText("Exception:\n" + exception);
                        try
                        {
                                server.close();
                        }
                        catch(Exception e)
                        {
                                mud.log.setText("Exception:\n" + e);
                                System.exit(1);
                        }
                        System.exit(1);
                }

        }
}