Struct Message Transmission via Socket

public struct MyMsg
{
    public int type; // 4 bytes
    public int state; // 4 bytes
    public string userID; // 20 bytes
    public string userPasswd; // 20 bytes
    public string contents; // 100 bytes
}
 
class MyMsgControl
{
    const int MSG_SIZE = 148;
 
    // 패킷 구조체를 바이트 배열로 변환하는 함수. 
    public byte[] GetBytesFromMyMsg(MyMsg pMsg)
    {
        byte[] btBuffer = new byte[MSG_SIZE];
 
        MemoryStream ms = new MemoryStream(btBuffer, true);
        BinaryWriter bw = new BinaryWriter(ms);
 
        // type
        bw.Write(IPAddress.HostToNetworkOrder(pMsg.type));
 
        // state
        bw.Write(IPAddress.HostToNetworkOrder(pMsg.state));
 
        // string.. 
        try
        {
            byte[] tmpBuf = new byte[20];
 
            if (pMsg.userID != null)
            {
                Encoding.Default.GetBytes(pMsg.userID, 0, pMsg.userID.Length, tmpBuf, 0);
                bw.Write(tmpBuf);
            }
 
            if (pMsg.userPasswd != null)
            {
                tmpBuf = new byte[20];
                Encoding.Default.GetBytes(pMsg.userPasswd, 0, pMsg.userPasswd.Length, tmpBuf, 0);
                bw.Write(tmpBuf);
            }
 
            if (pMsg.contents != null)
            {
                tmpBuf = new byte[100];
                Encoding.Default.GetBytes(pMsg.contents, 0, pMsg.contents.Length, tmpBuf, 0);
                bw.Write(tmpBuf);
            }
        }
 
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            // Error Handling 
        }
 
        bw.Close();
        ms.Close();
 
        return btBuffer;
    }
 
    // 바이트 배열을 패킷 구조체로 변환하는 함수
    public MyMsg GetMyMsg(byte[] btBuffer)
    {
        MyMsg retMsg = new MyMsg();
 
        MemoryStream ms = new MemoryStream(btBuffer, false);
        BinaryReader br = new BinaryReader(ms);
 
        // type
        retMsg.type = IPAddress.NetworkToHostOrder(br.ReadInt32());
 
        // state
        retMsg.state = IPAddress.NetworkToHostOrder(br.ReadInt32());
 
        // 20 bytes의 문자열
        retMsg.userID = ExtendedTrim(Encoding.Default.GetString(br.ReadBytes(20)));
 
        // 20 bytes의 문자열
        retMsg.userPasswd = ExtendedTrim(Encoding.Default.GetString(br.ReadBytes(20)));
 
        // 100 bytes의 문자열
        retMsg.contents = ExtendedTrim(Encoding.Default.GetString(br.ReadBytes(100)));
 
        br.Close();
        ms.Close();
 
        return retMsg;
 
 
    }
 
    // 문자열 뒤쪽에 위치한 NULL을 제거한 후에 공백문자를 제거한다.
    public string ExtendedTrim(string source)
    {
        string dest = source;
 
        int index = dest.IndexOf('\0');
        if (index > -1)
        {
            dest = source.Substring(0, index + 1);
        }
        return dest.TrimEnd('\0').Trim();
    }
}

Discussion

Enter your comment
UZLCQ
 
ko/study/dotnet/struct_message_transmission_via_socket.txt · Last modified: 2010/05/21 02:03 (external edit)
CC Attribution-Noncommercial-No Derivative Works 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0