Reputation: 4546
Why am I getting an error of BeginReceive method of Size (3rd parameter)?
But BUFFER_SIZE its never bigger (in lenght) then the constant value (its 2048 bytes).
This is my code:
public void Send(byte[] byteData, M.StateObject _stateObj, Socket client)
{
// Begin sending the data to the remote client(s):
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendData), _stateObj);
}
void SendData(IAsyncResult ar)
{
try
{
M.StateObject stateObj = (M.StateObject)ar.AsyncState;
Socket client = stateObj.server;
byte[] data = stateObj.data;
int bytesSent = client.EndSend(ar);
client.BeginReceive(data, 0, M.StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveData), stateObj);
allDone.Set();
}
catch (Exception ex)
{
OnMessageShowing(ex.Message);
}
}
private void ClientLogging(object _data, M.StateObject _stateObj)
{
M.UserData client = _data as M.UserData;
if (client != null)
{
//setting properties:
client.bConnectred = true;
// 1. add client to list of clients:
ClientAddingRemoving(_stateObj, client, true);
// 2. showing welcome on server
OnMessageShowing(client.ServerNotifying());
// 3. refreshing the list of clients on server`s main form
OnClientsListRefreshing(bll.ClientsListCreating(listOfClients));
// 4. send welcome note to all clients:
byte[] bytesToSend = M.SerializeServer.Login_WelcomeToAllClients(client);
bytesToSend = M.BytesArrayGetSet.BytesSending_Set(bytesToSend);
SendToAll(bytesToSend, _stateObj.server);
// 5. send list of all users to logged client:
bytesToSend = M.SerializeServer.Login_ToLoggedClient(bll.ListOfAllUsers(listOfClients));
bytesToSend = M.BytesArrayGetSet.BytesSending_Set(bytesToSend);
Send(bytesToSend, _stateObj, _stateObj.server);
//6. send new client to list of all other clients:
bytesToSend = M.SerializeServer.Login_ToAllClients(client);
bytesToSend = M.BytesArrayGetSet.BytesSending_Set(bytesToSend);
SendToAll(bytesToSend, _stateObj.server);
}
}
private void SendToAll(byte[] data, Socket _socket)
{
foreach (M.StateObject stObj in listOfClients.Keys) //dictionary<StateObject, Client>, Client is a custom client class
{
Send(data, stObj, _socket);
}
}
Here is the StateObject class:
public class StateObject
{
public Socket Server;
public const int BUFFER_SIZE = 2048;
public byte[] data = new byte[BUFFER_SIZE];
}
Maybe calling Send() method too many times? Do I have to use "ManualResetEvent" delegate to block or to do something else? I really dont know what to do.
Upvotes: 1
Views: 1822
Reputation: 127603
Replace M.StateObject.BUFFER_SIZE
with stateObj.data.Length
, somehow data
is smaller than BUFFER_SIZE
.
Also I see you put out the data in to an array but don't do anything with it. I would reccomend changing this
byte[] data = stateObj.data;
int bytesSent = client.EndSend(ar);
client.BeginReceive(data, 0, M.StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveData), stateObj);
to this
int bytesSent = client.EndSend(ar);
client.BeginReceive(stateObj.data, 0, stateObj.data.Length, SocketFlags.None, new AsyncCallback(ReceiveData), stateObj);
and see if that fixes your error.
Also I noticed that Your BeginSend is passing in the state object blob. Is the array you used to send data stateObj.data
too? And if so, are multiple calls to BeginSend using new stateObj
objects? If they are all sharing the same state object you could have the next send reading the same time as the current receive is writing.
Upvotes: 2