tags:

views:

463

answers:

2

In c# when simulating a lan-messenger I am using the loopback address just for testing the current code and am being able to receive the first message I am sending.However after that there aren't any messages reaching the socket.Is there anything to do with clearing a socket buffer?.please help This is the callback function when a connection is made:

private void accepted(IAsyncResult iar)
    {
        Socket server = (Socket)iar.AsyncState;
        Socket client = server.EndAccept(iar);
        if (client.Connected)
        {
            try
            {
                client.BeginReceive(receive, 0, receive.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(rec), client);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("arguments incorrect in begin-receive call", "Error", MessageBoxButtons.OK);
            }
            catch (SocketException)
            {
                MessageBox.Show("error in accessing socket while receiving", "Error", MessageBoxButtons.OK);
            }
            catch (ObjectDisposedException)
            {
                MessageBox.Show("socket closed while receiving", "Error", MessageBoxButtons.OK);
            }
            catch (Exception)
            {
                MessageBox.Show("error while receiving", "Error", MessageBoxButtons.OK);
            }

        }

This is the callback function when the begin-receive method is executed:

void rec(IAsyncResult ar)
    {

        StringBuilder receivedData;
        //String oldvalue;
        Socket remote = (Socket)ar.AsyncState;
        int recv = remote.EndReceive(ar);
        receivedData = new StringBuilder(Encoding.ASCII.GetString(receive, 0, recv));
        //MessageBox.Show(receivedData.ToString(), "received", MessageBoxButtons.OK);
        StringBuilder sb = new StringBuilder(this.textBox1.Text);
        sb.AppendLine(receivedData.ToString());
        if (textBox1.InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate { this.textBox1.Text = sb.ToString(); });

        }
        remote.Close(5);
        return;
    }

Does it have anything to do with a stringbuilder or string datatype for assigning the data received to a variable.

+1  A: 

Here is a 2-part tutorial on C# socket programming, with source code...

http://www.devarticles.com/c/a/C-Sharp/Socket-Programming-in-C-Part-I/1/

http://www.devarticles.com/c/a/C-Sharp/Socket-Programming-in-C-sharp-Part-II/

What is wrong depends entirely on your design. Are you polling? Or, are you waiting for events?

The source is the download.zip file attached to this page: http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

Rodddgers
I do not think that is needed.I am sure its a simple code that must be added. I am receiving the first message sent.However after that, no message is coming in.
Avik
A: 

Take a look at this. Notice how WaitForData calls itself. This allows the second, and subsequent messages to be received.

You never wait for more data.

    private void OnClientConnect(IAsyncResult asyn)
    {
        m_Client = m_Listener.EndAccept(asyn);
        WaitForData();
    }
    private void WaitForData()
    {
        buffer = new byte[1024];
        m_Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
    }
    private void OnDataReceived(IAsyncResult asyn)
    {
        int iRx = 0;
        iRx = m_Client.EndReceive(asyn);

        if (iRx > 0)
        {
            PacketReceiver.BytesReceived(buffer);
        }

        if (m_Client.Connected == true)
            WaitForData();
    }
Mike Christiansen
I tried to make my code as similar to this as possible..Nothing seems to be working.I think its some logical error.Is there anyway I can get some help.Messages are coming in the 1st time.Nothing after that.
Avik
- I mean your entire network code.
Mike Christiansen
I think I understand what is going on.. The begin-accept method is being called only once. Apart from using threads, is there anyway tat I can make that asynchronous call to begin-accept continuously?
Avik
AFAIK, you only call BeginAccept once for a one-to-one communication. That would have nothing to do with receiving more than one message.Are you sure you're BeginReceive more than once?
Mike Christiansen