人生必须的知识就是引人向光明方面的明灯。这篇文章主要讲述android socket 通讯(客户端) 发送数据相关的知识,希望能为你提供帮助。
/** ClientSocket通讯类 **/
【android socket 通讯(客户端) 发送数据】public class ClientSocket
{
/**服务器地址*/
private String serverUrl="192.168.124.214";
/**服务器端口*/
private int serverPort=8888;
/*发送命令线程*/
class sendCommandThread extends Thread{
private String command;
public sendCommandThread(String aCommand){
this.command = aCommand;
}
public void run(){
try{
Socket socket = new Socket(serverUrl,serverPort);
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print(command);
out.flush();
}catch (UnknownHostException e){
}catch (IOException e){
}
}
}
/** 发送文件线程 **/
class sendFileThread extends Thread{
private byte byteBuffer[] = new byte[1024];
private OutputStream outsocket;
private ByteArrayOutputStream myOutputStream;
public sendFileThread(ByteArrayOutputStream myOutputStream) {
this.myOutputStream = myOutputStream;
try {
myOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
try {
Socket socket = new Socket(serverUrl,serverPort);
outsocket = socket.getOutputStream();
//写入头信息
String msg = java.net.URLEncoder.encode("PHONEVIDEO","utf-8");
byte[] buffer = msg.getBytes();
outsocket.write(buffer);
ByteArrayInputStream inputStream = new ByteArrayInputStream(myOutputStream.toByteArray());
int amount;
while ((amount = inputStream.read(byteBuffer))!= -1) {
outsocket.write(byteBuffer,0,amount);
}
myOutputStream.flush();
myOutputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
推荐阅读
- Ubuntu+NDK编译openssl(为了Android上使用libcurl且支持HTTPS协议)
- 浅谈 MVP in Android(转)
- 链接到逍遥安卓模拟器
- SAN LUN Mapping出错导致文件系统共享冲突,数据恢复成功
- Android入门(广播发送者与广播接收者)
- 列表字典append
- virtualenvwrapper
- Appium Mac 命令行安装
- 走进Android系统