2012-03-07 8 views
0

기본적으로 나는 클라이언트/서버 연결을 통해 이미지, 문자열 및 파일을 보내고받는 중입니다. 내가 문자열 명령에 대한 프로토콜을 쉽게 사용할 수는 있지만 데이터가 이미지인지 또는 무엇인지간에 구분할 수는 없다.패킷 헤더는 어떻게 만듭니 까?

그럼 Dim 버퍼를 byte()로 사용하여 패킷을 만드는 방법은 무엇입니까?

+0

왜 당신이 먼저 제어 문자를 보낼 수 없습니다? 1 = 텍스트, 2 = 이미지 등 ... 그러면 어떤 데이터를 수신하는지 알게되고 그에 따라 행동 할 수 있습니다. –

답변

1

나는 당신이하려는 일에 그냥 짐작할 것입니다. 당신이 들어오는 HTTP 연결을 처리하는 TCPListener를 사용하는 경우는과 같이 응답 할 수있다 :

Private Sub ServePNG() 

     Dim stream As NetworkStream = mytcpclient.GetStream 

     Dim Content As Byte() = System.IO.File.ReadAllBytes("image.png") 

     Dim sb As New System.Text.StringBuilder 
     sb.Append("HTTP/1.0 200 OK" + ControlChars.CrLf) 
     sb.Append("Content-Type: image/png" + ControlChars.CrLf) 
     sb.Append("Content-Length: " + Content.Length.ToString + ControlChars.CrLf) 
     sb.Append(ControlChars.CrLf) 

     Dim Header() As Byte = Encoding.ASCII.GetBytes(sb.ToString) 
     stream.Write(Header, 0, Header.Length) 
     stream.Write(Content, 0, Content.Length) 

     client.Close() 

    End Sub 

을 내가 똑바로 image.png를

0

에서 바이트 배열을 얻고 당신은 새로운 클래스를 만들 수 있습니다 Header Property는 당신이 그 클래스를 직렬화 그것은

예 바이트 배열로 변환 할 수 있습니다 포함 :


Serializable() 
    Public class Packet 
    dim _header as packetHeader 
    dim _data as byte() 
    Public Property Header as packetHeader 
    Get 
    Return _Header 
    End Get 
    Set (byval value as packetHeader) 
    _header = value 
    End Set 
    End Property 
    Public Property Data as Object 
    Get 
    Return _Header 
    End Get 
    Set (byval value as Object) 
    _data = value 
    End Set 
    End Property 
    Public Sub New() 
    End Sub 
    Public Sub New(Byval header as packetHeader, Byval data as Object) 
    _header = header 
    _data = data 
    End Sub 
    Public Shared Function Serialize(myPacket as Packet) as Byte() 
    Dim fs as New MemoryStream 
    Dim formatter as New BinaryFormatter 
    formatter.Serialize(fs, myPacket) 
    Return fs.ToArray 
    End Function 
    Public Shared Function Deserialize(strm() as Byte) as Packet 
    Dim fs as New MemoryStream(strm) 
    Dim formatter as New BinaryFormatter 
    Return DirectCast(formatter.Deserialize(fs),Packet) 
    End Function 
    End Class 
    'Example of use 
    Public Enum packetHeader as Integer 
    Text = 0 
    Image = 1 
    'etc 
    End Enum 
    'Inside button click event (sending the packet) 
    Dim bitmap as new Bitmap("C:/example.png") 
    Dim data as Object = bitmap 
    Dim myPacket as new Packet(packetHeader.Image, data) 
    dim buffer() as byte = Packet.Serialize(myPacket) 
    'to deserialize the packet and check the header 
    Dim buffer() as byte = stream.Read 
    Dim myPacket as Packet = Packet.Deserialize(buffer) 
    Select myPacket.Header 
    Case packetHeader.Image 
    Dim image as Bitmap 
    image = DirectCast(myPacket.Data, Bitmap) 
    Case packetHeader.Text 
    Dim str as String 
    str = DirectCast(myPacket.Data, String) 
    End Select 
    'do your proccess inside the Select statement 

+0

나쁜 formmating에 대해 유감스럽게 생각한다. .. 단지 여기에서 새로운 im>. < – jasperagrante

관련 문제