I want to create an async socket. The server side should to handle client request on time and not to set in a queue. I read more and wrote my code similar below link. In the server side is correct and i have not any error or problems but in client side when i want to receive data, for first received data, everything is good but when called ReceiveCallback for second time, i get below error:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
The link is:
https://stackoverflow.com/questions/14974404/socket-programming-multiple-client-one-server
My code in ReceiveCallback method is:
private void ReceiveCallback(IAsyncResult asyncResult)
{
try
{
StateObject state = (StateObject)asyncResult.AsyncState;
Socket client = state.workSocket;
int bytesRead = 0;
if (client != null)
bytesRead = client.EndReceive(asyncResult);
if (bytesRead > 0)
{
//string content = state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)).ToString();
Buffer.BlockCopy(state.buffer, 0, _imageBuff, _totBytesRead, bytesRead);
_totBytesRead += bytesRead;
//if (content.IndexOf(Cache.EndOfMessage, StringComparison.Ordinal) > -1)
if (_totBytesRead < state.ImageSize)
{
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
//if (state.sb.Length > 1)
//{
// _response = state.sb.ToString();
//}
}
else
{
newMsgReceived(_imageBuff);
receiveDone.Set();
}
}
}
catch (Exception ex)
{
Log.CreateLog(Kit.Journal.Logger.Enums.LogType.Error, ex.Message, "ReceiveCallback in MonitoringSocket has error!");
}
}
The error was for Blockcopy part of my method..
I want for test my application for handle some client in my computer before publish that, i wrote below code. I used Threads.
private void BtnConfirm_Click(object sender, RoutedEventArgs e)
{
//ImgDataGrid.Items.Clear();
var pc = new PersianCalendar();
var fromDate = pc.ToDateTime(_fromYear, _fromMonth, _fromDay, 0, 0, 0, 1);
var toDate = pc.ToDateTime(_toYear, _toMonth, _toDay, 0, 0, 0, 1);
byte[] fileNameObj = null;
var thread = new Thread(() =>
{
fileNameObj = ServiceUtility.GetImageFileNames(fromDate, toDate);
var fileNameListStrTemp = "";
var fileNameListStr = new List<string>();
if (fileNameObj != null && fileNameObj.Length > 0)
{
fileNameListStrTemp = Kit.Utility.ZipTask.Unzip(fileNameObj);
}
if (fileNameListStrTemp != null && !string.IsNullOrEmpty(fileNameListStrTemp))
{
fileNameListStr = fileNameListStrTemp.Split('|').ToList();
FillDataGridView(fileNameListStr);
}
else
{
MessageBox.Show("");
}
});
thread.Start();
byte[] fileName = null;
var thread1 = new Thread(() =>
{
fileName = ServiceUtility.GetImageFileNames(fromDate, toDate);
var fileNameListStrTemp = "";
var fileNameListStr = new List<string>();
if (fileName != null && fileName.Length > 0)
{
fileNameListStrTemp = Kit.Utility.ZipTask.Unzip(fileName);
}
if (fileNameListStrTemp != null && !string.IsNullOrEmpty(fileNameListStrTemp))
{
fileNameListStr = fileNameListStrTemp.Split('|').ToList();
FillDataGridView(fileNameListStr);
}
else
{
MessageBox.Show("");
}
});
thread1.Start();
}
I am confused now. For handle some request from client in server side, i should thread in server side or i can write like above code? My code in client side is incorrect or test my code in BtnConfirm_click is incorrect?