Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ReceiveAsync Error

Tags:

c#

Im currently developing an appserver. I would like to use AcceptAsync method. I got error "Object reference not set to instance of object." when calling ReceiveAsync method. If there any come up with this problem and got the solution to it?

public class AppServer
{
    public void Start()
    {
        Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        serverSocket.Bind(new IPEndPoint(IPAddress.Any, 12345));
        serverSocket.Listen(100);

        SocketAsyncEventArgs e = new SocketAsyncEventArgs();
        e.Completed += new EventHandler<SocketAsyncEventArgs>(e_Completed);

        bool raiseEvent = serverSocket.AcceptAsync(e);
        if (!raiseEvent)
            AcceptCallback(e);
    }

    void e_Completed(object sender, SocketAsyncEventArgs e)
    {
        AcceptCallback(e);
    }

    private void AcceptCallback(SocketAsyncEventArgs e)
    {
        SocketAsyncEventArgs readEventArgs = new SocketAsyncEventArgs();
        readEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(readEventArgs_Completed);

        Socket clientSocket = e.AcceptSocket;
        bool raiseEvent = clientSocket.ReceiveAsync(readEventArgs); // <-- Error goes here
        if (!raiseEvent)
            ReceiveCallback(readEventArgs);
    }

    void readEventArgs_Completed(object sender, SocketAsyncEventArgs e)
    {
        ReceiveCallback(e);
    }

    private void ReceiveCallback(SocketAsyncEventArgs e)
    {

    }
}
like image 960
Andi Wijaya Avatar asked Jun 11 '11 09:06

Andi Wijaya


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

I had the same problem and I managed to figure it out. You need to provide to your SocketAsyncEventArgs object a data buffer (byte array) to store the data received before calling ReceiveAsync(e).

void e_Completed(object sender, SocketAsyncEventArgs e)
{
    byte[] buffer = new byte[1024];
    e.SetBuffer(buffer, 0, buffer.Length);
    AcceptCallback(e);
}
like image 93
user627283 Avatar answered Oct 06 '22 01:10

user627283