Solving this packet fragmentation problem I'm having

Using System.Net.Socket's Socket asynchronously and having problem

Page 1 of 1

0 Replies - 1104 Views - Last Post: 14 February 2010 - 02:35 PM Rate Topic: -----

#1 Gluebag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-November 08

Solving this packet fragmentation problem I'm having

Posted 14 February 2010 - 02:35 PM

Hey, was wondering if you guys could help me with this. I'm fairly new to the whole socket connections. I finally got down (atleast i think) how they work. However, I'm having this problem trying to figure out a solution to the problem im having with packet fragmentation.

Basically, with the example above, say I'm connected to a server, and we process and send message to each other. Well I'm using async for my project because I thought it would best suite my needs. However, with sockets I notice you have to set a buffer with a specified "length". Well my problem comes when, each "command" or message that we send each other ends with \r\n, so we know when that specific one ends, much like HTTP protocol. However, I cannot figure out... Say I got a byte array that's 512 bytes big, so "byte[] buffer = new byte[512]", and I use that with my asynchronous requests. Then on the receiving, say the server sends me something that's 724 bytes, and it splits one "command" or message in half. Now how could I connect the NEXT incoming transmission send from them right after to complete the command with the original 1st half of it?

I hope I'm not just babblin and someone understands this. Thanks!

EXAMPLE:

    public class gSocket
    {
        private Socket mSock;
        private IPEndPoint epServer;
        private byte[] m_byBuff = new byte[512]; <-- Only 512 buffer
        private string m_Data;
        public event On_DatarecievedEventHandler On_Datarecieved;
        public delegate void On_DatarecievedEventHandler(string data);
        public event On_ConnectEventHandler On_Connect;
        public delegate void On_ConnectEventHandler();

        public gSocket(string server, int port)
        {
            string domain = GetIpFromDomain(server);
            if (string.IsNullOrEmpty(domain)) { throw new Exception("Was unable to resolve domain!!!"); }
            this.epServer = new IPEndPoint(IPAddress.Parse(domain), port);
            mSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //mSock.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);
            mSock.Blocking = false;
        }

        public void StartConnection()
        {
            if (epServer != null)
            {
                AsyncCallback oc = new AsyncCallback(OnConnect);
                mSock.BeginConnect(epServer, oc, mSock);
            }
            else
            {
                throw new Exception("No server set!!");
            }
        }

        private string GetIpFromDomain(string domain)
        {
            string hostname = domain;
            IPAddress[] addresslist = Dns.GetHostAddresses(hostname);
            if (addresslist.Length >= 1)
            {
                return addresslist[0].ToString();
            }
            return "";
        }

        public void Write(string sData)
        {
            byte[] msg = null;
            msg = Encoding.ASCII.GetBytes(sData);
            mSock.Send(msg);
        }

        private void SetupRecieveCallback(Socket sock)
        {
            AsyncCallback receiveData = new AsyncCallback(OnReceivedData);
            sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, receiveData, sock);
        }

        private void OnConnect(IAsyncResult ar)
        {
            Socket sock = default(Socket);
            sock = (Socket)ar.AsyncState;
            if (sock.Connected == true)
            {
                SetupRecieveCallback(sock);
                if (On_Connect != null)
                {
                    On_Connect();
                }
            }
        }

        private void OnReceivedData(IAsyncResult ar)
        {
            int nBytesRec = 0;
            Socket sock = (Socket)ar.AsyncState;
            string sRecieved = null;
            nBytesRec = sock.EndReceive(ar);
            if (nBytesRec > 0)
            {
                sRecieved = Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec);
                //What im talking about is right here, say they sent 40 "commands" or messages at once, the async socket trys to
                //combine them together, until it hits end of buffer, well if it fills up, it just picks up on the next one
                if (this.On_Datarecieved != null)
                {
                    this.On_Datarecieved(sRecieved);
                }
                m_Data = sRecieved;
                SetupRecieveCallback(sock);
            }
            else
            {
                sock.Shutdown(SocketShutdown.Both);
                sock.Close();
            }
        }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1