socket 实现android手机链接服务器,现在服务器直接没数据,请教下是不是发送数据有问题呀?
public static TcpClient instance()
{
if (s_Tcp == null)
{
s_Tcp = new TcpClient(Const.SOCKET_SERVER, Const.SOCKET_PORT);
}
return s_Tcp;
}
public TcpClient(String HostIp, int HostPort)
{
this.hostIp = HostIp;
this.hostPort = HostPort;
try {
socket = RequestSocket(Const.SOCKET_SERVER, Const.SOCKET_PORT);
} catch (IOException e) {
e.printStackTrace();
}
}
private Socket RequestSocket(String host, int port)
throws UnknownHostException, IOException {
Socket ConSocket = new Socket();
//创建套接字地址,其中 IP 地址为通配符地址,端口号为指定值。
//有效端口值介于 0 和 65535 之间。端口号 zero 允许系统在 bind 操作中挑选暂时的端口。
isa = new InetSocketAddress(host, port);
//建立一个远程链接
ConSocket.connect(isa);
return ConSocket;
}
private void SendMsg(byte[]bytes) throws IOException {
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(String.valueOf(bytes));
writer.flush();
}
public boolean canConnectToServer(byte[] bytes)
{
try
{
if (socket != null)
{
Log.e("socketed",socket.toString());
SendMsg(bytes);
}
} catch (IOException e)
{
// TODO Auto-generated catch block
Log.e("sendmsg","Send timeout");
return false;
}
catch (Exception e){
Log.e("socket","socket disconnect");
return false;
}
}