in my code soket is received aync. and in the receive callback there is Send Message.
so callback is looped always.
received-send-received-send .......
in this situation how can i interrupted this loop and quit.
i make quit button but it doesn't work. only i click fast many times.
public class AsyncObject
{
public Byte[] Buffer;
public Socket WorkingSocket;
public AsyncObject(Int32 bufferSize)
{
Buffer = new Byte[bufferSize];
}
}
private Socket _mServerSocket;
private Socket _mSend;
private AsyncCallback _mFnReceiveHandler;
private AsyncCallback _mFnSendHandler;
private AsyncCallback _mFnAcceptHandler;
public void StartServer()
{
if (_mServerSocket != null)
{
if (_mServerSocket.Connected)
{
SetText(richTextBox1, "이미 연결되어있습니다.\n");
}
SetText(richTextBox1, "이미 연결되어있습니다.\n");
return;
}
_mFnReceiveHandler = HandleDataReceive;
_mFnSendHandler = HandleDataSend;
_mFnAcceptHandler = HandleClientConnectionRequest;
_mServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_mServerSocket.Bind(new IPEndPoint(IPAddress.Parse("192.168.0.201"), 8234));
_mServerSocket.Listen(5);
SetText(richTextBox1, "연결중입니다.\n");
_mServerSocket.BeginAccept(_mFnAcceptHandler, _mServerSocket);
}
public void StopServer(object sender, EventArgs e)
{
_isClose = true;
if (_mServerSocket != null)
{
if (_mServerSocket.Connected || _mServerSocket.IsBound)
_mServerSocket.Close();
_mServerSocket = null;
}
if (_mSend != null)
{
if (_mSend.Connected)
{
_mSend.Disconnect(true);
_mSend.Close();
}
_mSend = null;
}
SetText(richTextBox1, "서버가 닫혔습니다.\n");
}
public void SendMessage(String message)
{
if(_mSend == null) return;
if (message.Length==0) return;
string sendmessage = message + "\n";
var ao = new AsyncObject(1) { Buffer = Encoding.UTF8.GetBytes(sendmessage), WorkingSocket = _mSend };
try
{
_mSend.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _mFnSendHandler, ao);
}
catch (SocketException secException)
{
MessageBox.Show(secException.ToString());
}
}
private void HandleClientConnectionRequest(IAsyncResult ar)
{
try
{
Socket sockClient = _mServerSocket.EndAccept(ar);
_mSend = sockClient;
var ao = new AsyncObject(4096);
SetText(richTextBox1, sockClient.RemoteEndPoint + "의 연결 요청 수락\n");
ao.WorkingSocket = sockClient;
_isClose = false;
sockClient.BeginReceive(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _mFnReceiveHandler, ao);
}
catch (ObjectDisposedException)
{
}
catch (SocketException)
{
}
catch (NullReferenceException)
{
}
}
private void HandleDataReceive(IAsyncResult ar)
{
try
{
var ao = (AsyncObject) ar.AsyncState;
Int32 recvBytes = ao.WorkingSocket.EndReceive(ar);
if (!ao.WorkingSocket.Connected) return;
if (recvBytes > 0)
{
SetText(richTextBox1, "메세지 받음: " + Encoding.UTF8.GetString(ao.Buffer));
SetText(richTextBox1, "\n");
}
ar.AsyncWaitHandle.WaitOne();
Array.Clear(ao.Buffer, 0, ao.Buffer.Length);
ao.WorkingSocket.BeginReceive(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _mFnReceiveHandler, ao);
SendMessage("READ?");
}
catch (ObjectDisposedException)
{
}
catch (SocketException)
{
}
}
private void HandleDataSend(IAsyncResult ar)
{
var ao = (AsyncObject)ar.AsyncState;
Int32 sentBytes = ao.WorkingSocket.EndSend(ar);
if (sentBytes > 0)
SetText(richTextBox1, "메세지 보냄: " + Encoding.UTF8.GetString(ao.Buffer));
}
private void Send_Click(object sender, EventArgs e)
{
string message = richTextBox2.Text;
SendMessage(message);
}
What I have tried:
i tried event handler and make thread inside of callback to quit server
and it is not work at all..
i think priority of event and listener and callback is problem.
i know listener is high-priority next callback last event