Chinaunix首页 | 论坛 | 博客
  • 博客访问: 659595
  • 博文数量: 53
  • 博客积分: 830
  • 博客等级: 准尉
  • 技术积分: 1089
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-31 18:23
文章存档

2019年(1)

2018年(1)

2016年(1)

2015年(1)

2014年(12)

2013年(4)

2012年(33)

分类: 系统运维

2012-06-18 12:48:48


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

//import org.apache.mina.transport.socket.nio.NioSocketConnector;

//import ClientHandler.Callback;

public class Client {
   
//    private NioSocketConnector connector;
    
    public List receiveMsg;

    private String host;
    private int port;
    
//    private IoSession session;

    private boolean connected = true;
    
    Socket socket = null;
     BufferedReader in;  
        PrintWriter out;
//        BufferedReader line;
    public void longin() {
        
//        DataOutputStream out = null;

            try {
                socket = new Socket(host, port);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(socket.isConnected()){
                connected = true;
                receiveMsg = new ArrayList();
                try {
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  
                 try {
                    out = new PrintWriter(socket.getOutputStream(), true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{
                connected = false;
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
//             line = new BufferedReader(new InputStreamReader(in));  
//            while(true){
//                
////                    out.println(line.readLine());  
//                    String msg = line.readLine();
//                    
//                    receiveMsg.add(msg);
//                    try {
//                        Thread.sleep(10);
//                    } catch (InterruptedException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//            }

        
//        clientSupport = new ClientSupport("sdf", handler);
//        receiveMsg = new ArrayList();
//        if (!clientSupport.connect(connector, address, false)) {
//            connected = false;
////            try {
////            Thread.sleep(1000 * 3);
////            longin();
////        } catch (InterruptedException e) {
////            e.printStackTrace();
////        }
//
//        } else {
//            connected = true;
//            // ADDROBOT
//            // if(AIClientPara.isGiveSeat==1){
//            // sendMessage("ADDROBOT \n");
//            // }
//        }
    }
    
    public void getMessage(){
        String msg = null;
        try {
            msg = in.readLine();
            System.out.println(msg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(msg != null && !"".equals(msg))
        receiveMsg.add(msg);
    }

    public Client(String host, int port) {
        this.host = host;
        this.port = port;
        longin();
    }

    public void logout() {
//        receiveMsg.clear();
//        receiveMsg = null;
//        clientSupport.quit();
//        session.close();
        if(socket!=null){
            try {
                if(socket!=null && socket.isClosed()==false)
                    socket.close();
                if(in!=null)
                    in.close();
                    if(out!=null)
                    out.close();
                    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    if(socket!=null && socket.isClosed()==false)
                        socket.close();
                    if(in!=null)
                    in.close();
                    if(out!=null)
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
         Client tc = new Client("localhost",40887);
         tc.longin();
        // tc.logout();
    }

    public boolean connected() {
        return connected;
    }

    /**
     * 断线重连
     */
    public void disconnected() {
//        try {
//            Thread.sleep(1000 * 15);
//            longin();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    public void error(String message) {
//        disconnected();
    }

    public void loggedIn() {
    }

    public void loggedOut() {
    }

//    public void messageReceived(String message) {
//        receiveMsg.add(message);
//    }

    public void sendMessage(String message) {
        out.write(message);
        out.flush();
    }

}

阅读(938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~