Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets java.net.UnknownHostException:cs.xfire.com_Sockets_Tcp_Connection - Fatal编程技术网

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

Sockets java.net.UnknownHostException:cs.xfire.com,sockets,tcp,connection,Sockets,Tcp,Connection,我正在尝试从android xfire客户端建立连接,我相信这是TCP连接。我在谷歌上搜索了所有地方,人们反复说要使用端口25999上的cs.xfire.com连接到xfire(一种消息服务)。但最后我得到一个例外,说它没有连接。所以我想知道为什么我不能建立联系。互联网上几乎没有任何信息可以帮助我找出它无法连接的原因,我通过数据包嗅探器收听了连接,当我在xfire官方windows应用程序中单击“连接”时,他们也给了我25999端口。所以我真的很困惑,对不起,如果这个问题没有多大意义,下面是我的

我正在尝试从android xfire客户端建立连接,我相信这是TCP连接。我在谷歌上搜索了所有地方,人们反复说要使用端口25999上的cs.xfire.com连接到xfire(一种消息服务)。但最后我得到一个例外,说它没有连接。所以我想知道为什么我不能建立联系。互联网上几乎没有任何信息可以帮助我找出它无法连接的原因,我通过数据包嗅探器收听了连接,当我在xfire官方windows应用程序中单击“连接”时,他们也给了我25999端口。所以我真的很困惑,对不起,如果这个问题没有多大意义,下面是我的代码:

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();
    }
 }
 }
公共类连接扩展活动{
私有DataInputStream in=null;
私有DataOutputStream out=null;
专用字节[]缓冲区;
私有字符串用户名、密码、昵称、statustext=“Online”;
私有布尔runThread=true;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.connected);
TextView txtView1=(TextView)findViewById(R.id.tvTest);
Bundle extras=getIntent().getExtras();
字符串用户名=extras.getString(“用户名”);
字符串密码=extras.getString(“密码”);
试一试{
插座s=新插座(“cs.xfire.com”,25999);
setText(“已连接!”);
in=新的DataInputStream(s.getInputStream());
out=新的DataOutputStream(s.getOutputStream());
登录();
}捕获(ioe异常ioe){
//断开连接();
txtView1.setText(ioe.toString());
}   
}
公开募捐{
setTitle(“Xfire阅读器线程”);
while(运行线程){
readBytes();
调试(缓冲);
开关(缓冲区[0]&0xFF){
案例0x80://盐
打破
案例0x81://验证失败
断开连接();
打破
案例0x82://loginreply
打破
案例0x83://friendslist
打破
案例0x84://朋友在线
打破
案例0x85://接收消息
/*ReceiveMessagePacket rmp=新的ReceiveMessagePacket(缓冲区);
if(rmp.getMessageType()==ReceiveMessagePacket.MSGTYPE\u IM){
分路放大器=
新的AckImPacket(rmp.getSid(),rmp.getImIndex());
写入(amp.getBytes());
}*/
打破
案例0x87://游戏中的朋友
打破
案例0x91://由于原因断开连接
断开连接();
打破
案例0x9a://朋友状态文本
打破
案例0xac:
打破
}
}
}
私有void登录(){
//TODO自动生成的方法存根
//初始化与“UA01”数据包的连接
写入(“UA01.getBytes());
//发送版本包
最终字节[]p_版本1=新字节[]{
0x03,0,0x01,0x07,
0x76、0x65、0x72、0x73、0x69、0x6f、0x6e、//版本
0x02
};
最终int版本=118;
字符串vp=null;
写入(vp.getBytes());
//启动读卡器线程
onStart();
}
公共空间断开连接(){
//EventManager.removeObserver(此);
runThread=false;
试一试{
write(新字节[]{0,0,0,0});//破坏流
}捕获(ioe异常ioe){
ioe.printStackTrace();
}最后{
试一试{
out.close();
in.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
//FriendManager.getInstance().cleanup();
//firevent(新的DatalessEvent(xfirevent.XF_脱机));
}
}
私有void readBytes(){
试一试{
字节[]numBytes=新字节[2];
in.read(numBytes,0,2);
int low=numBytes[0]&0xFF,high=numBytes[1]&0xFF;
整数长度=(0x00 |低|高
我收到一个异常,说它无法连接


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

是的,但怎么可能是查找失败?我在路由器上转发了25999端口,cs.xfire.com确实存在。@user1953302-尽管如此,message说这是一个查找失败,很难想象它会在什么情况下撒谎。除非你有一个可靠的基于逻辑的理论来解释它,否则你不应该否认证据。@user1953302-开始尝试对“cs.xfire.com”进行DNS查找在该特定计算机/虚拟机上。如果成功,则在JVM中查找DNS查找失败的原因。端口转发与此无关。这与主机有关,而不是与端口有关。这是DNS主机名查找问题。不是连接问题。请尝试掌握这一基本事实。并阅读异常的实际内容。