2016-11-30 2 views
0

저는이 모든 TCP에 대해 매우 익숙하며, 여기서 잘못된 점을 발견 할 수 없습니다. 여러분 중 일부는 꽤 분명 할 것입니다. 코드를 실행하면 서버 측에서 시작 프로세스를 실행하고 '연결 대기 중'을 표시 한 다음 클라이언트를 시작하고 나중에 '연결된 서버'를 표시하고 서버 측에서 시작 프로세스를 다시 시작하여 표시합니다. 'Connected to the client'그리고 양쪽면에서 SendThroughTCPSocket ("what strings") 메서드를 호출하면 전송되는 것을 볼 수 있지만 상대방에서 아무 것도 감지되지 않고 있습니까?
내가 뭘 잘못하고 있니? 둘 다 서로 지속적으로 듣고 싶습니다.TCP 서버와 클라이언트가 서로의 정보를받지 못합니다.

서버 측 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace Server 
{ 
// State object for reading client data asynchronously 
public class StateObject 
{ 

    #region instance variables 

    // Client socket. 
    public Socket workSocket = null; 
    // Size of receive buffer. 
    public const int BufferSize = 1024; 
    // Receive buffer. 
    public byte[] buffer = new byte[BufferSize]; 
    // Received data string. 
    public StringBuilder sb = new StringBuilder(); 

    #endregion 

} 

public class AsynchronousSocketListener 
{ 

    public enum TransmissionSate 
    { 
     Waiting, 
     Receiving, 
     Received, 
     Sending, 
     Sent, 
     Off 
    } 


    #region class variables 

    // Thread signal. 
    public static ManualResetEvent allDone = new ManualResetEvent(false); 

    public static IPHostEntry ipHostInfo; 
    public static IPAddress ipAddress; 
    public static IPEndPoint localEndPoint; 

    public static Socket listener; 
    public static Socket handler; 


    //flags 
    public static TransmissionSate currentState = TransmissionSate.Off; 

    #endregion 

    #region constructor 

    public AsynchronousSocketListener() 
    { 
    } 

    #endregion 

    #region Start Listening 

    //Start Listening for Client 
    public static void StartListening() 
    { 
     // Data buffer for incoming data. 
     byte[] bytes = new Byte[1024]; 

     // Establish the local endpoint for the socket. 
     // The DNS name of the computer 
     ipHostInfo = Dns.Resolve(Dns.GetHostName()); 
     ipAddress = ipHostInfo.AddressList[0]; 
     localEndPoint = new IPEndPoint(ipAddress, 11000); 

     // Create a TCP/IP socket. 
     listener = new Socket(AddressFamily.InterNetwork, 
      SocketType.Stream, ProtocolType.Tcp); 

     // Bind the socket to the local endpoint and listen for incoming connections. 
     try 
     { 
      listener.Bind(localEndPoint); 
      listener.Listen(100); 

      while (true) 
      { 
       // Set the event to nonsignaled state. 
       allDone.Reset(); 

       // Start an asynchronous socket to listen for connections. 
       Console.WriteLine("Waiting for a connection..."); 
       listener.BeginAccept(
        new AsyncCallback(AcceptCallback), 
        listener); 

       //flag 
       currentState = TransmissionSate.Waiting; 

       // Wait until a connection is made before continuing. 
       allDone.WaitOne(); 
      } 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 


     Console.WriteLine("\nConnected to the client."); 
     //Console.Read(); 

    } 

    #endregion 

    #region Receive Methods 

    //Accept communication and Start reading 
    public static void AcceptCallback(IAsyncResult ar) 
    { 
     // Signal the main thread to continue. 
     allDone.Set(); 

     // Get the socket that handles the client request. 
     listener = (Socket)ar.AsyncState; 
     handler = listener.EndAccept(ar); 

     //flag 
     currentState = TransmissionSate.Receiving; 

     // Create the state object. 
     StateObject state = new StateObject(); 
     state.workSocket = handler; 
     handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
      new AsyncCallback(ReadCallback), state); 
    } 

    //Read and handle the information 
    public static void ReadCallback(IAsyncResult ar) 
    { 
     String content = String.Empty; 

     // Retrieve the state object and the handler socket 
     // from the asynchronous state object. 
     StateObject state = (StateObject)ar.AsyncState; 
     handler = state.workSocket; 

     // Read data from the client socket. 
     int bytesRead = handler.EndReceive(ar); 

     if (bytesRead > 0) 
     { 
      // There might be more data, so store the data received so far. 
      state.sb.Append(Encoding.ASCII.GetString(
       state.buffer, 0, bytesRead)); 

      // Check for end-of-file tag. If it is not there, read 
      // more data. 
      content = state.sb.ToString(); 
      if (content.IndexOf("<EOF>") > -1) 
      { 

       //flag 
       currentState = TransmissionSate.Received; 

       // All the data has been read from the 
       // client. Display it on the console. 
       Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", 
        content.Length, content); 

       //reference 
       Data.lastResponse = content; 

       //react 
       Console.WriteLine("Server just received a request"); 
       switch (content) 
       { 
        case "DataRequest": 
         Console.WriteLine("Client is trying to get the preset data"); 
         SendThroughTCPSocket(Data.xmlToSend + Data.endOfFile); 
         break; 

        default: 
         Console.WriteLine("Unknown request from Client"); 
         break; 
       } 

      } 
      else 
      { 
       // Not all data received. Get more. 
       handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
       new AsyncCallback(ReadCallback), state); 
      } 
     } 
    } 

    #endregion 

    #region Send Methods 

    //Send data to a socket 
    private static void Send(Socket handler, String data) 
    { 
     // Convert the string data to byte data using ASCII encoding. 
     byte[] byteData = Encoding.ASCII.GetBytes(data); 

     //flag 
     currentState = TransmissionSate.Sending; 

     // Begin sending the data to the remote device. 
     handler.BeginSend(byteData, 0, byteData.Length, 0, 
      new AsyncCallback(SendCallback), handler); 
    } 

    //Sending data and closing handler 
    private static void SendCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 
      Socket handler = (Socket)ar.AsyncState; 

      // Complete sending the data to the remote device. 
      int bytesSent = handler.EndSend(ar); 
      Console.WriteLine("Sent {0} bytes to client.", bytesSent); 

      //flag 
      currentState = TransmissionSate.Sent; 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    #endregion 

    #region SendThroughTCPSocket 

    public static void SendThroughTCPSocket(String data) 
    { 
     try 
     { 
      // Send data to the client 
      Send(handler, data); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 

    } 

    #endregion 

    #region Stop Server 

    //Close Sever 
    public static void StopServer() 
    { 
     try 
     { 
      // Release the socket. 
      handler.Shutdown(SocketShutdown.Both); 
      handler.Close(); 

      //flag 
      currentState = TransmissionSate.Off; 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    #endregion 

} 
} 

클라이언트 측 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace Client 
{ 
// State object for receiving data from remote device. 
public class StateObject 
{ 


    #region instance variables 

    // Client socket. 
    public Socket workSocket = null; 
    // Size of receive buffer. 
    public const int BufferSize = 256; 
    // Receive buffer. 
    public byte[] buffer = new byte[BufferSize]; 
    // Received data string. 
    public StringBuilder sb = new StringBuilder(); 

    #endregion 

} 

public class AsynchronousClient 
{ 

    public enum TransmissionSate 
    { 
     Waiting, 
     Receiving, 
     Received, 
     Sending, 
     Sent, 
     Off 
    } 


    #region instance variables 

    // The port number for the remote device. 
    private const int port = 11000; 

    #endregion 

    #region class variables 

    // ManualResetEvent instances signal completion. 
    private static ManualResetEvent connectDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent sendDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent receiveDone = 
     new ManualResetEvent(false); 

    // The response from the remote device. 
    private static String response = String.Empty; 

    public static IPHostEntry ipHostInfo; 
    public static IPAddress ipAddress; 
    public static IPEndPoint remoteEP; 

    public static Socket client; 

    //flags 
    public static TransmissionSate currentState = TransmissionSate.Off; 
    public static bool isConnected = false; 

    #endregion 

    #region Start Client 

    //Start Client and use it 
    public static void StartClient() 
    { 
     // Connect to a remote device. 
     try 
     { 
      //flag 
      isConnected = false; 

      // Establish the remote endpoint for the socket. 
      // The name of the host 
      // remote device is the current device 
      ipHostInfo = Dns.Resolve(Dns.GetHostName()); 
      ipAddress = ipHostInfo.AddressList[0]; 
      remoteEP = new IPEndPoint(ipAddress, port); 

      // Create a TCP/IP socket. 
      client = new Socket(AddressFamily.InterNetwork, 
       SocketType.Stream, ProtocolType.Tcp); 

      // Connect to the remote endpoint. 
      client.BeginConnect(remoteEP, 
       new AsyncCallback(ConnectCallback), client); 
      connectDone.WaitOne(); 

      //flag 
      currentState = TransmissionSate.Waiting; 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    #endregion 

    #region Stop Client 

    //Close Client 
    public static void StopClient() 
    { 
     try 
     { 
      // Release the socket. 
      client.Shutdown(SocketShutdown.Both); 
      client.Close(); 

      //flag 
      currentState = TransmissionSate.Off; 
      isConnected = false; 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    #endregion 

    #region Connect 

    //callback for connection 
    private static void ConnectCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 
      Socket client = (Socket)ar.AsyncState; 

      // Complete the connection. 
      client.EndConnect(ar); 

      Console.WriteLine("Socket connected to {0}", 
       client.RemoteEndPoint.ToString()); 

      // Signal that the connection has been made. 
      connectDone.Set(); 

      //flag 
      isConnected = true; 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    #endregion 

    #region Receive 

    //Receive data 
    private static void Receive(Socket client) 
    { 
     try 
     { 
      // Create the state object. 
      StateObject state = new StateObject(); 
      state.workSocket = client; 

      //flag 
      currentState = TransmissionSate.Receiving; 

      // Begin receiving the data from the remote device. 
      client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
       new AsyncCallback(ReceiveCallback), state); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    //callback Receive 
    private static void ReceiveCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the state object and the client socket 
      // from the asynchronous state object. 
      StateObject state = (StateObject)ar.AsyncState; 
      Socket client = state.workSocket; 

      // Read data from the remote device. 
      int bytesRead = client.EndReceive(ar); 

      if (bytesRead > 0) 
      { 
       // There might be more data, so store the data received so far. 
       state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); 

       // Get the rest of the data. 
       client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
        new AsyncCallback(ReceiveCallback), state); 
      } 
      else 
      { 
       // All the data has arrived; put it in response. 
       if (state.sb.Length > 1) 
       { 
        response = state.sb.ToString(); 
       } 
       // Signal that all bytes have been received. 
       receiveDone.Set(); 

       //flag 
       currentState = TransmissionSate.Received; 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    #endregion 

    #region Send 

    //Send data 
    private static void Send(Socket client, String data) 
    { 
     // Convert the string data to byte data using ASCII encoding. 
     byte[] byteData = Encoding.ASCII.GetBytes(data); 

     //flag 
     currentState = TransmissionSate.Sending; 

     // Begin sending the data to the remote device. 
     client.BeginSend(byteData, 0, byteData.Length, 0, 
      new AsyncCallback(SendCallback), client); 
    } 

    //Callback send 
    private static void SendCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 
      Socket client = (Socket)ar.AsyncState; 

      // Complete sending the data to the remote device. 
      int bytesSent = client.EndSend(ar); 
      Console.WriteLine("Sent {0} bytes to server.", bytesSent); 

      // Signal that all bytes have been sent. 
      sendDone.Set(); 

      //flag 
      currentState = TransmissionSate.Sent; 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    #endregion 

    #region SendThroughTCPSocket 

    public static string SendThroughTCPSocket(String data) 
    { 
     try 
     { 
      // Send data to the remote device. 
      Send(client, data); 
      sendDone.WaitOne(); 

      // Receive the response from the remote device. 
      Receive(client); 
      receiveDone.WaitOne(); 

      // Write the response to the console. 
      Console.WriteLine("Response received : {0}", response); 

      return response; 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
      return "<No response>"; 
     } 

    } 

    #endregion 

} 
} 

답변

1

당신은 소켓의 상단에 프로토콜을 구현해야합니다. 서버에서 ""을 사용하여 메시지가 끝났음을 확인하고이를 얻은 후에 만 ​​인쇄합니다. 그러나, 나는 당신이 이것을하는 클라이언트에서 아무데도 볼 수 있습니다. 이상적으로는 메시지 토큰의 시작과 메시지 토큰의 끝을 갖고 싶기 때문에 소켓을 통해 여러 메시지를 보낼 수 있습니다.

private static void Send(Socket client, String data) 
    { 
     // Convert the string data to byte data using ASCII encoding. 
     byte[] byteData = Encoding.ASCII.GetBytes(data + "<EOF>"); 

     //flag 
     currentState = TransmissionSate.Sending; 

     // Begin sending the data to the remote device. 
     client.BeginSend(byteData, 0, byteData.Length, 0, 
      new AsyncCallback(SendCallback), client); 
    } 

에 클라이언트 전송 방법 변경 이 작동해야하지만, 나는 시작과 끝 토큰 프로토콜을 구현하는 것이 좋습니다 않습니다. 클라이언트의 receivecallback에서

당신은 프로토콜의 종류를 확인하지 코드가 단순히 데이터 수신에 계속 시도합니다 있는지 확인하기 위해 프로그램이 데이터를 확인해야합니다 몇 가지 단계에서

if (bytesRead > 0) 
      { 
       // There might be more data, so store the data received so far. 
       state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); 

       // Get the rest of the data. 
       client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
        new AsyncCallback(ReceiveCallback), state); 
      } 

이 어떤 종류의 마지막 토큰입니다. 다음이 변경 시도 :

   if (bytesRead > 0) 
      { 
       // There might be more data, so store the data received so far. 
       state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); 
       var endPos = state.sb.ToString().IndexOf("<EOF>"); 
       if (endPos > -1) //we have a complete message. huzzah! 
       { 
        response = state.sb.ToString().Substring(0, endPos); 
        // Signal that all bytes have been received. 
        receiveDone.Set(); 
        //flag 
        currentState = TransmissionSate.Received; 
       } 
       else 
       // Get the rest of the data. 
        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
        new AsyncCallback(ReceiveCallback), state); 
      } 

나는 반복해야합니다 시작과 은밀한 메시지의 끝을 확인하는 방법 : 당신은 원시 소켓을 사용하려는 경우 프로토콜을 설계 할 필요가있다.

+0

EOF 태그를 생략 했으므로 이제 추가했습니다. 서버 측에서는 요청을 이해했음을 알려주고 적절한 문자열 + EOF 태그를 보내 응답했습니다. ,하지만 클라이언트 측에서 아무 것도 응답으로 잡히지 않습니다. 그러나 SendThroughTCPSocket()을 사용하면 먼저 데이터를 보내고 클라이언트 측에서 "상태 수신 대기"로 전환합니다. 어떤 생각? – BUZZE

+0

서버가 연결을 닫을 때만 수신됩니다. – BUZZE

+0

이 질문에 유감이지만 디버깅을 시도 했습니까? 다시 말하지만 클라이언트에서는 프로토콜이없는 것처럼 보이므로 수신 바이트의 크기가 0보다 큰지 확인한 다음 수신을 계속합니다. 메시지가 끝나면 아무 곳이나 확인하지 마십시오. 편집 참조 – Kell

관련 문제