Hello again,
I've looked at this, I still cannot get it to work. Here's the Sending part below;
public static void getClientList(int i)
{
BinaryFormatter bformatter = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
foreach (String client in userList)
{
PimpDU pdu = new PimpDU();
pdu.setType("name");
pdu.setFrom(client);
pdu.setPayload(client);
bformatter.Serialize(mem, pdu);
workerSocket[i].BeginSend(mem.GetBuffer(), 0, mem.GetBuffer().Length, SocketFlags.None, new AsyncCallback(getClientListCallback), workerSocket[i]);
getlistDone.WaitOne();
mem.Position = 0;
mem.SetLength(0);
}
}
public static void getClientListCallback(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
client.EndSend(ar);
getlistDone.Set();
}
And here's the receiving;
public void receive(IAsyncResult asyn)
{
BinaryFormatter bformatter = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
int readBytes = 0;
StateObject state = (StateObject)asyn.AsyncState;
readBytes = state.getWorkSocket().EndReceive(asyn);
mem.Write(state.getBuffer(), 0, readBytes);
mem.Position = 0;
PimpDU pdu = (PimpDU)bformatter.Deserialize(mem);
if (readBytes > 0)
{
if (pdu.getType()=="message")
{
this.textbox_display.Text = this.textbox_display.Text + pdu.getFrom() + ": " + pdu.getPayload() + "\r\n";
this.textbox_display.SelectionStart = this.textbox_display.TextLength;
this.textbox_display.ScrollToCaret();
state.getWorkSocket().BeginReceive(state.getBuffer(), 0, state.getBufferSize(), 0, new AsyncCallback(receive), state);
}
if (pdu.getType()=="name")
{
this.listbox_users.Items.Add(pdu.getFrom());
state.getWorkSocket().BeginReceive(state.getBuffer(), 0, state.getBufferSize(), 0, new AsyncCallback(receive), state);
}
}
else {
this.textbox_display.Text = this.textbox_display.Text + "*** Received 0 bytes?!";
}
}
I know it's alot of code sorry :( I can't get it to work though. When I send my list of strings using the getClientList function, some simply dissapear/aren't read.
1. When my receiving buffer gets more than one object(PDU), I take it only one object is retrieved from the buffer, and the other is thrown away? Is there any way to read the first object, see if there's more stuff in the buffer, and if so read more?
2. Did I do anything wrong with ManualResetEvent?