Socket簡單的數據包的發送與接收

具體的發包與拆包的協議可以自己定義:
我定義:包頭第一個字節為1時則為接收文件為0則為接收字符
服務器:
private static void writefile(DataOutputStream dos, FileInputStream fis) {
byte[] bytes=new byte[2];//先發包頭
int length;
bytes[0]=1;
bytes[1]=5;
try {
dos.write(bytes);//發給客戶端
} catch (IOException e) {
e.printStackTrace();
System.out.println("發送包頭失敗");
}
try{
byte[] bytes1=new byte[1024];//發送包體數據
while((length=fis.read(bytes1, 0,bytes1.length))!=-1){
dos.write(bytes1,0,bytes1.length);
dos.flush();
}
}catch(Exception e){
e.printStackTrace();
System.out.println("發送文件包體數據失敗");
}
}
private static void writestring(DataOutputStream dos) {
// TODO Auto-generated method stub
String string="sending";
byte[] bytes=new byte[2];//同理發送字符串的包頭
bytes[0]=0;
bytes[1]=5;
try {
dos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
System.out.println("發送包頭失敗");
}
try {
byte[] bytes1=new byte[1024];//發送字符串的包體數據
bytes1=string.getBytes();
dos.write(bytes1);
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("發送字符包體數據失敗");
}
}
}

客戶端:
try{
s=new Socket("192.168.1.106",45);
Log.d(TAG, "run:2222222222222");
is=s.getInputStream();
dis=new DataInputStream(is);
new Thread(){
@Override
public void run(){
super.run();
try{
dis = new DataInputStream(s.getInputStream());
Log.d(TAG, "33333333333333");
byte[] bytes=new byte[2];
dis.read(bytes);
if(bytes[0]==0){
int a=bytes[1];
byte[] bytes1=new byte[a];
dis.readFully(bytes1);
String ssss=new String(bytes1);
Message msg=new Message();
msg.what=2;
msg.obj=ssss;
revhandler.sendMessage(msg);
}
else {
{
File file=new File("/storage/emulated/0/1/c.jpg");
Log.d(TAG,"run:2.55555555555555555555555");
fos=new FileOutputStream(file);
Log.d(TAG,"run:333333333333333333333");
inputByte = new byte[30];//接收數據
Message msg=new Message();
msg.what=0;
revhandler.sendMessage(msg);
Log.d(TAG,"run:44444444444444");
while((length = dis.read(inputByte, 0, inputByte.length)) != -1) {
Log.d(TAG,"44444444");
fos.write(inputByte,0,inputByte.length);
fos.flush();
}
msg = new Message();
msg.what=1;
revhandler.sendMessage(msg);
if(fos != null)
fos.close();
if(dis != null)
dis.close();
s.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
運行結果:
服務器:
![V1LEZDBZ07VQ482$02HO%R.png
客戶端:

Screenshot_2017-07-14-20-52-15-13.png

Screenshot_2017-07-14-20-52-54-39.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1 IO(三)No20 1.1Properties 屬性集 【 Properties屬性集,主要用于操作配置屬...
    征程_Journey閱讀 912評論 0 1
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,537評論 0 17
  • 一、先看下必備知識: 1,流:系統內部和外部進行數據傳輸的管道。 2,內部:內存;外部:輸入設備,文件,網絡。 3...
    進擊的王餅餅閱讀 1,794評論 1 4
  • 自從逃出了牛欄,青牛精便一路向南。他靈智漸開,速度越來越快,穿過了不知幾條大河,也越過了不知多少座村寨。秋陽漸高,...
    顧厭閱讀 689評論 0 2
  • 你好 你好 你好 你好 你好 你好 你好 你好 你好 11 22 33 11 11 11 1 1 1This ...
    好可怖的西瓜閱讀 133評論 0 0