我正在尝试从一个 android xfire 客户端建立一个连接,我相信它是一个 TCP 连接。我到处都用谷歌搜索过,一遍又一遍地说要在端口 25999 上使用 cs.xfire.com 连接到 xfire(一种消息传递服务)。但最后我得到一个异常说它没有连接。所以我想知道为什么我不能建立连接。互联网上几乎没有任何信息可以帮助我弄清楚它为什么无法连接,我通过数据包嗅探器监听了连接,当我从官方 xfire 点击“连接”时,他们也给了我端口 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();
}
}
}