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)
{
}
}
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 ...
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? 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.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With