All pastes #719848 Raw Edit

Jemand

public text v1 · immutable
#719848 ·published 2007-09-29 18:13 UTC
rendered paste body
import java.io.*;
import java.lang.*;
import java.net.*;
public class Client {
	private Socket _socket;
	private static Client _instance;
	private PrintWriter _out;
	private Listener _listener;
	class Listener extends Thread{
		private BufferedReader _reader;
		public Listener(Socket s)throws Exception{
			BufferedReader r=new BufferedReader(new InputStreamReader(_socket.getInputStream()));
			_reader=r;
		}
		public void run(){
			try{	
				System.out.println("read");
				while(!_reader.ready()){}
				String msg=_reader.readLine();
				if(msg.substring(0,4).equalsIgnoreCase("ping")){
					Client.getInstance().send("pong "+msg.substring(5));
				}
				System.out.println(msg);
			}catch(Exception e){System.out.println(e);}
		}
	}
	public static Client getInstance()throws Exception{
		return _instance;
	}
	public Client(String ip,int port)throws Exception{
		_socket=new Socket(ip,port);	
		_out = new PrintWriter(_socket.getOutputStream(), true);
		_listener=new Listener(_socket);
		_listener.start();
	}
	public void send(String msg)throws Exception{
		
		_out.println(msg);
		
	}
	public void close()throws Exception{
		_out.close();
		_socket.close();
	}
	public static void main(String[] args)throws Exception{
		Client x=new Client(args[0],Integer.parseInt(args[1]));
		String input="";
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		while(!input.equals("quit")){
			input=in.readLine();
			x.send(input);
		}
		x.close();
	}
}