十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
以太網(wǎng)幀格式,即在以太網(wǎng)幀頭、幀尾中用于實現(xiàn)以太網(wǎng)功能的域。
成都創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、楚雄州網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城網(wǎng)站制作、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為楚雄州等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
在以太網(wǎng)的幀頭和幀尾中有幾個用于實現(xiàn)以太網(wǎng)功能的域,每個域也稱為字段,有其特定的名稱和目的。
IEEE802.3以太幀頭如圖所示:
擴展:以太網(wǎng)幀格式多達5種,這是由歷史原因造成的。事實上,今天的大多數(shù)TCP/IP應(yīng)用都是用Ethernet V2幀格式(IEEE802.3-1997改回了對這一格式的兼容),而交換機之間的BPDU(橋協(xié)議數(shù)據(jù)單元)數(shù)據(jù)包則是IEEE802.3/LLC的幀,VLAN Trunk協(xié)議如802.1Q和Cisco的CDP(思科發(fā)現(xiàn)協(xié)議)等則是采用IEEE802.3SNAP的幀。
前導(dǎo)碼(7字節(jié))、幀起始定界符(1字節(jié))、目的MAC地址(6字節(jié))、源MAC地址(6字節(jié))、類型/長度(2字節(jié))、數(shù)據(jù)(46~1500字節(jié))、幀校驗序列(4字節(jié))[MAC地址可以用2-6字節(jié)來表示,原則上是這樣,實際都是6字節(jié)]
MP3文件有Header,直接合并文件,會導(dǎo)致MP3無法播放,需要把文件2的Header剔除。請參考以下代碼:
try?{
FileInputStream?fistream1?=?new?FileInputStream(_file_name);
File?f?=?new?File(new?File(_file_name).getParent()+"/final.mp3");
if(!f.exists())
{
f.createNewFile();
}
FileOutputStream?sistream?=?new?FileOutputStream((new?File(_file_name)).getParent()+"/final.mp3");
int?temp;
int?size?=?0;
temp?=?fistream1.read();
while(?temp?!=?-1)
{
sistream.write(temp);
temp?=?fistream1.read();
};
fistream1.close();
FileInputStream?fistream2?=?new?FileInputStream(temp_file);
fistream2.read(new?byte[32],0,32);
temp?=?fistream2.read();
while(?temp?!=?-1)
{
sistream.write(temp);
temp?=?fistream2.read();
};
fistream2.close();
sistream.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
1.TCP/IP協(xié)議要求信息必須在塊(chunk)中發(fā)送和接收,而塊的長度必須是8位的倍數(shù),因此,我們可以認為TCP/IP協(xié)議中傳輸?shù)男畔⑹亲止?jié)序列。如何發(fā)送和解析信息需要一定的應(yīng)用程序協(xié)議。
2.信息編碼:
首先是Java里對基本整型的處理,發(fā)送時,要注意:1)每種數(shù)據(jù)類型的字節(jié)個數(shù);2)這些字節(jié)的發(fā)送順序是怎樣的?(little-endian還是
big-endian);3)所傳輸?shù)臄?shù)值是有符號的(signed)還是無符號的(unsigned)。具體編碼時采用位操作(移位和屏蔽)就可以了。
具體在Java里,可以采用DataOutputStream類和ByteArrayOutputStream來實現(xiàn)?;謴?fù)時可以采用
DataInputStream類和ByteArrayInputStream類。
其次,字符串和文本,在一組符號與一組整數(shù)之間的映射稱為編碼字符集(coded character
set)。發(fā)送者與接收者必須在符號與整數(shù)的映射方式上達成共識,才能使用文本信息進行通信,最簡單的方法就是定義一個標(biāo)準(zhǔn)字符集。具體編碼時采用
String的getBytes()方法。
最后,位操作。如果設(shè)置一個特定的設(shè)為1,先設(shè)置好掩碼(mask),之后用或操作;要清空特定一位,用與操作。
3.成幀與解析
成幀(framing)技術(shù)解決了接收端如何定位消息的首位位置的問題。
如果接收者試圖從套接字中讀取比消息本身更多的字節(jié),將可能發(fā)生以下兩種情況之一:如果信道中沒有其他消息,接收者將阻塞等待,同時無法處理接收
到的消息;如果發(fā)送者也在等待接收端的響應(yīng)消息,則會形成死鎖(dealock);另一方面,如果信道中還有其他消息,則接收者會將后面消息的一部分甚至
全部讀到第一條消息中去,這將產(chǎn)生一些協(xié)議錯誤。因此,在使用TCP套接字時,成幀就是一個非常重要的考慮因素。
有兩個技術(shù):
1.基于定界符(Delimiter-based):消息的結(jié)束由一個唯一的標(biāo)記(unique
marker)指出,即發(fā)送者在傳輸完數(shù)據(jù)后顯式添加的一個特殊字節(jié)序列。這個特殊標(biāo)記不能在傳輸?shù)臄?shù)據(jù)中出現(xiàn)。幸運的是,填充(stuffing)技術(shù)
能夠?qū)ο⒅谐霈F(xiàn)的定界符進行修改,從而使接收者不將其識別為定界符。在接收者掃描定界符時,還能識別出修改過的數(shù)據(jù),并在輸出消息中對其進行還原,從而
使其與原始消息一致。
2.顯式長度(Explicit length):在變長字段或消息前附加一個固定大小的字段,用來指示該字段或消息中包含了多少字節(jié)。這種方法要確定消息長度的上限,以確定保存這個長度需要的字節(jié)數(shù)。
接口:
Java代碼 import java.io.IOException; import java.io.OutputStream; public interface Framer { void frameMsg(byte [] message,OutputStream out) throws IOException; byte [] nextMsg() throws IOException; }
定界符的方式:
Java代碼 import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class DelimFramer implements Framer { private InputStream in;//data source; private static final byte DELIMTER=(byte)'\n';//message delimiter public DelimFramer(InputStream in){ this.in=in; } @Override public void frameMsg(byte[] message, OutputStream out) throws IOException { //ensure that the message dose not contain the delimiter for(byte b:message){ if(b==DELIMTER) throw new IOException("Message contains delimiter"); } out.write(message); out.write(DELIMTER); out.flush(); } @Override public byte[] nextMsg() throws IOException { ByteArrayOutputStream messageBuffer=new ByteArrayOutputStream(); int nextByte; while((nextByte=in.read())!=DELIMTER){ if(nextByte==-1){//end of stream? if(messageBuffer.size()==0){ return null; }else{ throw new EOFException("Non-empty message without delimiter"); } } messageBuffer.write(nextByte); } return messageBuffer.toByteArray(); } }
顯式長度方法:
Java代碼 import java.io.DataInputStream; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class LengthFramer implements Framer { public static final int MAXMESSAGELENGTH=65535; public static final int BYTEMASK=0xff; public static final int SHOTMASK=0xffff; public static final int BYTESHIFT=8; private DataInputStream in;// wrapper for data I/O public LengthFramer(InputStream in) throws IOException{ this.in=new DataInputStream(in); } @Override public void frameMsg(byte[] message, OutputStream out) throws IOException { if(message.lengthMAXMESSAGELENGTH){ throw new IOException("message too long"); } //write length prefix out.write((message.lengthBYTEMASK)BYTEMASK); out.write(message.lengthBYTEMASK); //write message out.write(message); out.flush(); } @Override public byte[] nextMsg() throws IOException { int length; try{ length=in.readUnsignedShort(); }catch(EOFException e){ //no (or 1 byte) message; return null; } //0=length=65535; byte [] msg=new byte[length]; in.readFully(msg);//if exception,it's a framing error; return msg; } }