public void sendTcp(final String ip,final int port,final byte[] bytes){
final String str=new String(bytes);
Runnable runnable=new Runnable() {
@Override
public void run() {
Socket socket=null;
InputStream in=null;
OutputStream out=null;
try {
//客户端开启 建立连接
socket=new Socket(ip,port);
//从socket中获取输出流
out=socket.getOutputStream();
out.write(bytes);
//发送数据流
socket.shutdownOutput();
//从socket中获取输入流
in=socket.getInputStream();
byte[] bytes=new byte[1024];
int len;
StringBuilder msg=new StringBuilder();
while ((len=in.read(bytes))!=-1){
msg.append(new String(bytes,0,len,CHARSET_NAME));
}
in.close();
out.close();
socket.close();
} catch (Exception e) {
Log.d(TAG, "run: ");
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
new Thread(runnable).start();
}
08-31
5389
05-31
1525