实现tcp侦听器 [英] Implementing a tcp listener
问题描述
我在msdn的帮助下提出了以下代码,这是我目前要实现聊天应用程序的代码
public class Communicate
  {
      private static ManualResetEvent connectDone {get; set;}
      private static ManualResetEvent sendDone {get;组; }¥b $ b      私有静态字符串响应{get;组; }¥b $ b       private static ManualResetEvent receiveDone {get;组; }
      public static void Connect(EndPoint remoteEP,Socket客户端)
      {
          client.BeginConnect(remoteEP,new AsyncCallback(ConnectCallback),client);
          connectDone.WaitOne();
      }
      private static void ConnectCallback(IAsyncResult ar)
      {
         试试
          {
             套接字客户端=(套接字)ar.AsyncState;
              client.EndConnect(ar);
              connectDone.Set();              
          }¥b $ b           catch(例外e)
          {
              Console.WriteLine(e.ToString());
          }¥b $ b        
      }
      private static void Send(套接字客户端,字符串数据)
      {
          byte [] byteData = Encoding.ASCII.GetBytes(data);
          client.BeginSend(byteData,0,byteData.Length,SocketFlags.None,new AsyncCallback(SendCallBack),client);
      }
      private static void SendCallBack(IAsyncResult ar)
      {
         试试
          {
             套接字客户端=(套接字)ar.AsyncState;
              int bytesSent = client.EndSend(ar);
              sendDone.Set();
          }¥b $ b           catch(例外e)
          {
              Console.WriteLine(e.ToString());
          }¥b $ b       }
      private static void Receive(套接字客户端)
      {
         试试
          {
              StateObject state = new StateObject();
              state.workSocket = client;
              client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback),state);
          }¥b $ b           catch(例外e)
          {
              Console.WriteLine(e.ToString());
          }
      }
      private static void ReceiveCallback(IAsyncResult ar)
      {
         试试
          {
              StateObject state =(StateObject)ar.AsyncState;
             套接字客户端= state.workSocket;
              int bytesRead = client.EndReceive(ar);
              if(bytesRead> 0)
              {
                  state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
                  client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback),state);
$
              }¥b $ b              否则
              {
                  if(state.sb.Length> 1)
                  {
                      response = state.sb.ToString();
                  }¥b $ b                   receiveDone.Set();
                  client.Shutdown(SocketShutdown.Both);
                  client.Close();
               
              }¥b $ b           }¥b $ b           catch(例外e)
          {
              Console.WriteLine(e.ToString());
          }¥b $ b       }¥b $ b   }
我遇到的问题是知道我是否可以实现一个tcp监听器来监听传入的消息,考虑到这段代码。同样,当发送消息时,发送方是否总是需要连接到接收方?
i have come up with the following code with the help of msdn and this is the code that i currently have to implement a chat application
public class Communicate
{
private static ManualResetEvent connectDone{get;set;}
private static ManualResetEvent sendDone { get; set; }
private static string response { get; set; }
private static ManualResetEvent receiveDone { get; set; }
public static void Connect(EndPoint remoteEP, Socket client)
{
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Send(Socket client, String data)
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallBack), client);
}
private static void SendCallBack(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
int bytesSent = client.EndSend(ar);
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
else
{
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
receiveDone.Set();
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
The problem i'm having is knowing whether i can implement a tcp listener to listen for incoming messages, considering this code. Also when a message is sent, will the sender always need to connect to the receiver?
推荐答案
TCP需要连接。 您可以保持连接始终打开或关闭,并为每个发送和接收的消息打开连接。通常你有一个主(客户端)和从属Iserver)接口。 客户端向服务器发送命令,
服务器处理命令,然后服务器发回响应。 因此,在完成此过程之前,请不要关闭连接。
TCP requires a connection. You can either keep the connection always open or close and open the connection for each message sent and receive. normally you have a master (client) and slave Iserver) interface. The client sends commands to the server, the server process the command, and then the server sends back a response. So you don't close the connection until you complete the process.
必须先以聆听模式启动服务器。 大多数应用程序将在服务器上提供一项服务,该服务将在计算机启动时自动启动并侦听连接,但某些应用程序只需让用户手动启动
服务器即可。 只有在服务器启动后启动应用程序时,客户端才会连接到服务器。
The server must be started first in listening mode. Most application will have at the server a service that will automatically start when the computer is started and listens for a connection, but some application just have the user start the server manually. The client will connect to the server only when an application is started after the server is started.
这篇关于实现tcp侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!