首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.net.UnknownHostException: cs.xfire.com

java.net.UnknownHostException: cs.xfire.com
EN

Stack Overflow用户
提问于 2013-06-29 11:22:10
回答 1查看 170关注 0票数 0

我正在尝试从android xfire客户端建立一个连接,我相信这是一个TCP连接。我到处用谷歌搜索,一次又一次地说要在端口25999上使用cs.xfire.com连接到xfire (一种消息传递服务)。但在最后,我得到了一个异常,说它没有连接。所以我想知道为什么我不能建立连接。互联网上几乎没有任何信息可以帮助我弄清楚为什么它不能连接,我通过数据包嗅探器监听了连接,当我在xfire windows官方应用程序上点击“连接”时,他们给了我25999端口。所以我真的很困惑,如果这个问题没有太多意义,很抱歉,这是我的代码:

代码语言:javascript
复制
public class Connectionn extends Activity{
private DataInputStream in = null;
private DataOutputStream out = null;
private byte[] buffer;
private String username, password, nickname, statustext = "Online";
private boolean runThread = true;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connected);
    TextView txtView1 = (TextView) findViewById(R.id.tvTest);
    Bundle extras = getIntent().getExtras();
    String username = extras.getString("username");
    String password = extras.getString("password");

    try {

        Socket s = new Socket("cs.xfire.com", 25999);
        txtView1.setText("Connected!");
        in = new DataInputStream(s.getInputStream());
        out = new DataOutputStream(s.getOutputStream());
        login();
    } catch (IOException ioe) {
        //disconnect();
        txtView1.setText(ioe.toString());

    }   
}

public void run() {
    setTitle("Xfire Reader Thread");
    while(runThread) {
            readBytes();
            debug(buffer);

            switch(buffer[0] & 0xFF) {
            case 0x80: // salt

                    break;
            case 0x81: // auth failed
                    disconnect();

                    break;
            case 0x82: // loginreply


                    break;
            case 0x83: // friendslist

                    break;
            case 0x84: // friend online

                    break;
            case 0x85: // receive message

                /*ReceiveMessagePacket rmp = new ReceiveMessagePacket(buffer);
                    if (rmp.getMessageType() == ReceiveMessagePacket.MSGTYPE_IM) {
                            AckImPacket amp =
                                    new AckImPacket(rmp.getSid(), rmp.getImIndex());
                            write(amp.getBytes());
                    }*/

                    break;
            case 0x87: // friend in game

                    break;
            case 0x91: // disconnected with reason
                    disconnect();

                    break;
            case 0x9a: // friend status text

                    break;
            case 0xac:

                    break;
            }
    }
}

private void login() {
    // TODO Auto-generated method stub

    // initialize connection with the 'UA01' packet
    write("UA01".getBytes());

    // send the version packet
    final byte[] p_version_1 = new byte[] {
            0x03, 0, 0x01, 0x07,
            0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, // version
            0x02
    };

    final int version = 118;
    String vp = null;
    write(vp.getBytes());

    // start the reader thread
    onStart();

}

public void disconnect() {
    //EventManager.removeObserver(this);
    runThread = false;

    try {
            out.write(new byte[] { 0, 0, 0, 0 }); // sabotage the stream                    
    } catch (IOException ioe) {
            ioe.printStackTrace();
    } finally {
            try {
                    out.close();
                    in.close();
            } catch (IOException ioe) {
                    ioe.printStackTrace();
            }
            //FriendManager.getInstance().cleanup();
           // EventManager.fireEvent(new DatalessEvent(XfireEvent.XF_OFFLINE));
    }
}

private void readBytes() {
     try {
             byte[] numBytes = new byte[2];
             in.read(numBytes, 0, 2);
             int low = numBytes[0] & 0xFF, high = numBytes[1] & 0xFF;
             int len = (0x00 | low | (high << 8)) - 2;

             if (len <= 0) {
                     buffer = new byte[] { 0 };
                     return;
             }

             buffer = new byte[len];
             in.read(buffer, 0, len);
     } catch (IOException ioe) {
             ioe.printStackTrace();
             disconnect();
     }
}

private static void debug(byte[] bs) {
    for (byte b : bs) {
            System.out.print(String.format("%02x", b) + " ");
    }
    System.out.println();
}

public void write(byte[] bs) {
    try {
            out.write(bs);
    } catch (IOException ioe) {
            ioe.printStackTrace();
            disconnect();
    }
 }
 }
EN

回答 1

Stack Overflow用户

发布于 2013-06-29 11:34:52

我收到一个异常,说它不能连接

不,你不知道。再读一遍。这是你的头衔。上面写着“未知主机”。这不是连接失败:而是查找失败。cs.xfire.com不存在,或者您的DNS不知道它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17376353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档