I've spent the past few nights trying to figure out whats going on with this AsyncCallback + sockets crap. I've cleaned this code out so its just a basic example of what im trying to do, there is a lot more to it but just to give anyone a general idea ive simplified the code as much as I can. So here's my deal. I have a socket listener set up and im trying to figure out how to pass data back and forth between clients on the server. Im able to do it but its throwing a NullRefrenceException and i cant seem to see it in my head on why its doing that. I have an AcceptConnection method, a SendData method, and a ReceiveData method… I guess the problem is i cant seem to just SendData directly without throwing an exception. Again the message still goes through as I'd expect it to do, but still that damn exception.
I'm including 3 pasties to my whole example program which isn't very big, In the ServerConnectivity class the background thread contains 1 command to broadcast a message to all connected clients, that works fine, no exceptions. Then there are 3 methods to deal with the accepting connection, receiving and the sending of data. The InputHandler method just throws the data out to the InputHandler class to be dealt with. In the InputHandler class that is where my call to BeginSend starts. What happens leading up to that is the data is turned into a string, and if the string starts with "tell" then it is treated as a command and the 2nd word in the string will be the target. the target socket is looked up by reference to a Socket, Client dictionary checking all the Client's in the list to see if they have a their name property equal to the 2nd word in the string. when it does it gets that client's socket and attempts to call SendData on that socket. I've tried
item.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(ServerConnectivity.SendData), null);
item.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(ServerConnectivity.SendData), item.socket);
where item is the client with the name that matched the 2nd word in the string from before. Anyways like I said the messages are being sent, but I'd like to understand the exceptions if anyone can help. Thanks for taking the time to read my book lol.
heres links to my example code
by whitedrow via /r/csharp