📄 tcpudpserver.cs
字号:
//to wait forever (note that the calling user's ReceiveFrom call blocks
//until he/she gets the answer).
this.startReceiveNotifyTimer();
dialogResult = MessageBox.Show("An Incoming Request from " +
(Dns.GetHostByAddress(remoteIpEndPoint.Address)).HostName
+ ". Do you want to chat?", "WinChat",
MessageBoxButtons.OKCancel);
if (elapsedDateTime == DateTime.MinValue)
//This means that the timer has not expired. Otherwise, elapsedDateTime
//would be assigned to the time when the timer expired.
{
if (dialogResult == DialogResult.OK)
{
this.stopReceiveNotifyTimer();
Byte[] returningByte = System.Text.Encoding.ASCII.GetBytes(
(ClientServerConstants.notifyAccept).ToCharArray());
udpNotifySocket.SendTo(returningByte, remoteNotifyEP);
//We have to invoke the peerNotify event here to ask the main WinChat application
//to create a TcpClient connection to the remote side so that communication is
//setup both ways.
peerNotify();
alreadyTalking = true;
}
else
{
this.stopReceiveNotifyTimer();
Byte[] returningByte = System.Text.Encoding.ASCII.GetBytes(
(ClientServerConstants.notifyReject).ToCharArray());
udpNotifySocket.SendTo(returningByte, remoteNotifyEP);
}
}
else
{
//The timer has expired!
MessageBox.Show("Timed Out! You have missed a call from " +
(Dns.GetHostByAddress(remoteIpEndPoint.Address)).HostName, "WinChat",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
elapsedDateTime = DateTime.MinValue;
}
}
}
else if (String.Compare(dataNotifyReceivedUdp, ClientServerConstants.notifyHangupRequest) == 0)
{
serverReceivedHangup = true;
Byte[] returningByte = System.Text.Encoding.ASCII.GetBytes(
ClientServerConstants.notifyHangupAccept.ToCharArray());
udpNotifySocket.SendTo(returningByte, remoteNotifyEP);
alreadyTalking = false;
//We have to invoke the peerHangup event here to ask the main WinChat application
//to close the TcpClient connection created during the Notify procedure.
peerHangup();
}
else
{
//If the received byte array is not recognized, the server will return a
//notifyUnknownReject message back to the calling peer.
Byte[] returningByte = System.Text.Encoding.ASCII.GetBytes(
ClientServerConstants.notifyUnknownReject.ToCharArray());
udpNotifySocket.SendTo(returningByte, remoteNotifyEP);
}
}
}
catch (SocketException se)
{
//ErrorCode 10004 corresponds to the SocketException: A blocking operation was interrupted by a call to
//WSACancelBlockingCall.
if (se.ErrorCode != 10004)
{
MessageBox.Show("A Socket Exception has occurred!" + se.ToString(),
"WinChat", MessageBoxButtons.OK);
}
}
}
/* Function : start_servers()
* Purpose : This function will start both the TcpListener thread (sampleTcpThread)
* and the UDP Server thread (udpNotifyThread).
*
*/
public void start_servers()
{
try
{
//Starting the TCP Listener thread.
sampleTcpThread = new Thread(new ThreadStart(this.StartListen));
sampleTcpThread.IsBackground = true;
sampleTcpThread.Start();
}
catch (SocketException se)
{
MessageBox.Show("An TCP Exception has occurred!" + se.ToString(), "WinChat",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
sampleTcpThread.Abort();
}
try
{
//Starting the UDP Notify thread.
udpNotifyThread = new Thread(new ThreadStart(this.receiveNotify));
udpNotifyThread.IsBackground = true;
udpNotifyThread.Start();
}
catch (Exception e)
{
MessageBox.Show("An UDP Exception has occurred!" + e.ToString(), "WinChat",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
udpNotifyThread.Abort();
}
}
/* Function : stop_servers()
* Purpose : This function will stop the TcpClient and close the UDP server. And
* it also aborts the running threads (sampleTcpThread and
* udpNotifyThread).
*
*/
public void stop_servers()
{
try
{
if (this.Active)
{
this.Stop();
}
if (udpNotifySocket != null)
{
udpNotifySocket.Close();
}
sampleTcpThread.Abort();
udpNotifyThread.Abort();
}
catch (Exception e)
{
MessageBox.Show("An Exception has occurred!" + e.ToString(), "WinChat",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/* Function : startReceiveNotifyTimer()
* Purpose : This function will start the 30 seconds Notify timer as soon
* as the UDP server receives a notifyRequest message.
*
*/
public void startReceiveNotifyTimer()
{
// Create a new Timer with Interval set to 30 seconds.
receiveNotifyTimer = new System.Timers.Timer(ClientServerConstants.UDPTIMER);
receiveNotifyTimer.Elapsed += new ElapsedEventHandler(this.OnTimedEvent1);
// Only raise the event the first time Interval elapses.
receiveNotifyTimer.AutoReset = false;
receiveNotifyTimer.Enabled = true;
}
/* Function : stopReceiveNotifyTimer()
* Purpose : This function will stop the Notify timer if the called user
* responds to the notifyRequest message within 30 seconds.
*
*/
public void stopReceiveNotifyTimer()
{
receiveNotifyTimer.Stop();
}
/* Function : OnTimedEvent1(object source, ElapsedEventArgs e)
* Purpose : This is the delegate to be called when the timer expires.
*
*/
public void OnTimedEvent1(object source, ElapsedEventArgs e)
{
//elapsedDateTime is initialized to DateTime.MinValue at the program
//startup. When the timer has expired, elapsedDateTime will be assigned to
//the time that the timer expired (e.SignalTime).
//Therefore, elapsedDateTime can serve as an indicator to tell whether the
//timer has expired or not.
elapsedDateTime = e.SignalTime;
}
/* Function : OnCallInitiation()
* Purpose : This is the delegate to be called when the calling peer is notified
* by the called user that he/she already accepts the call request.
* The TcpUdpServer enters 'alreadyTalking" mode.
*
*/
public void OnCallInitiation()
{
this.alreadyTalking = true;
}
/* Function : OnCallEnding()
* Purpose : This is the delegate to be called when the local user is notified by
* the remote user that he/she already hangs up the call.
* The TcpUdpServer exits 'alreadyTalking" mode.
*
*/
public void OnCallEnding()
{
this.alreadyTalking = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -