Socket簡單的數(shù)據(jù)包的發(fā)送與接收

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

客戶端:
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];//接收數(shù)據(jù)
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();
}
運(yùn)行結(jié)果:
服務(wù)器:
![V1LEZDBZ07VQ482$02HO%R.png
客戶端:

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

Screenshot_2017-07-14-20-52-54-39.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容