2015-12-27 11 views
5

मैं, एक पूर्ण द्वैध क्लाइंट-सर्वर संचार योजना प्राप्त करने के लिए कोशिश कर रहा हूँ 2 विभिन्न मशीनों (केवल) है, जहां प्रत्येक अंत बिंदु (क्लाइंट या सर्वर) किसी भी समय सामान भेज सकते हैं पर , असीमित रूप से (गैर-अवरुद्ध पाइप), और दूसरा छोर इसे उठाएगा और इसे पढ़ेगा।C# पूर्ण द्वैध अतुल्यकालिक नाम पाइप्स नेट

मैं जवाब नामित पाइप के अतिरिक्त कोई अन्य प्रौद्योगिकी के लिए मुझे जिक्र नहीं करना चाहता, मैं अन्य प्रौद्योगिकियों के बारे में पता है, लेकिन मैं इस विशेष प्रश्न का उत्तर चाहते हैं। (मैंने यह प्रश्न विभिन्न मंचों पर कई बार पोस्ट किया है, और मैं कुछ अन्य तकनीक का उपयोग करने के लिए सलाह देने वाले प्रतिक्रियाओं को देखता हूं। मुझे लगता है कि यह सीमाएं कठोर पर हैं?)

मैंने पढ़ा है कि नामित पाइप्स को एक होना है केवल डायरेक्शन या वे लॉक हो जाते हैं, लेकिन मुझे लगता है कि यह शायद गलत है। मुझे लगता है कि पाइप सॉकेट आधारित हैं, और मैं कल्पना नहीं कर सकता कि अंतर्निहित सॉकेट केवल एक तरफ होगा।

इस सवाल का कोई जवाब यह वास्तव में उपयोगी होने के लिए इन मुद्दों से निपटने की जरूरत है:

  1. जवाब अतुल्यकालिक पाइप को संबोधित करने की जरूरत है, मैं एक तुल्यकालिक समाधान का उपयोग नहीं कर सकते हैं।
  2. उत्तरों को इस तथ्य को प्रदर्शित करने या अनुमति देने की आवश्यकता है कि पाइप खुले रहें। मैं उदाहरण पढ़ने के थक गया हूं जहां पाइप खोला गया है, एक स्ट्रिंग फैलती है, तो पाइप तुरंत बंद हो जाती है। मुझे एक ऐसा जवाब चाहिए जो मानता है कि पाइप खुले रहते हैं और यादृच्छिक समय पर जंक के बहुत सारे संचारित करते हैं, और दोहराना जारी रखते हैं। बिना लटकता है।
  3. सी # आधारित समाधान

मैं मांग और कुत्सित ध्वनि करने के लिए माफी चाहता हूँ, लेकिन इंटरनेट परिशोध के दिनों के बाद मैं अभी भी एक अच्छा उदाहरण नहीं मिला है, और मैं WFC उपयोग करने के लिए नहीं करना चाहती। यदि आप इस उत्तर के विवरण जानते हैं और इसे अच्छी तरह से उत्तर देते हैं, तो यह विषय आने वाले युग के लिए एक वास्तविक विजेता होगा, मुझे यकीन है। अगर मैं इसे समझता हूं तो मैं खुद जवाब पोस्ट करूंगा।

आप के बारे में लिख सकते हैं और कहते हैं कि "आप दो पाइप का उपयोग करने की जरूरत है" के लिए कर रहे हैं, क्योंकि कुछ भी नहीं मैं पढ़ा है कृपया समझा क्यों, और कैसे आप जानते हैं कि यह सच है यह बताते हैं के बारे में क्यों यह मामला है ।

धन्यवाद!

+0

क्यों आप नामित पाइप बाध्यकारी के साथ डब्ल्यूसीएफ का उपयोग नहीं करना चाहते हैं? क्या आपने इस तरह के समाधान देखा है http://stackoverflow.com/questions/16432813/async-two-way-communication-with-windows-named-pipes-net – admax

+0

अरे, इतना गुस्से में क्यों, मैंने अभी पूछा ** क्यों ** आप डब्ल्यूसीएफ नहीं चाहते हैं। और यह एक टिप्पणी थी, सुझाव नहीं दिया गया समाधान। यदि आप डब्ल्यूसीएफ की कुछ कमियों के बारे में जानते हैं तो कृपया साझा करें, यह समुदाय के लिए उपयोगी हो सकता है। – admax

+1

नाराज नहीं, बस विशिष्ट। इसका कारण यह है कि मैं उन प्रौद्योगिकियों से दूर और प्रयास करता हूं जिन्हें "बहिष्कृत" माना जाता है, भले ही वे अभी भी उपयोग में हैं, खासकर यदि वे हिरन के लिए बहुत अधिक मूल्य-जोड़ नहीं जोड़ते हैं। डब्ल्यूसीएफ नामित पाइप्स + जेएसओएन के शीर्ष पर बहुत कुछ नहीं जोड़ता है, इस प्रकार मैं इसका उपयोग नहीं करना चाहूंगा। मुझे "न्यूनतम" रहना पसंद है। एक वितरित सिस्टम चलने के लिए, मैं केवल कुछ बाइट्स को आगे और पीछे स्थानांतरित कर रहा हूं, मुझे बस एक नामित पाइप चाहिए! –

उत्तर

0

मुझे लगता है कि आप async-संचार, आप दो पाइप का उपयोग करना है।

एक recv पाइप एक और पाइप भेज

वजह से, जब आप डेटा recv नहीं जानता है।

आप एक पाइप के साथ कुछ डेटा जब भेजने के लिए, recv डेटा पाइप पर नहीं लिख सकते हैं।

इसके विपरीत, आप पाइप पर डेटा भेजने नहीं लिख सकते हैं।

इसलिए आप async संचार के लिए दो पाइप किया है।

+1

पाइप सॉकेट समर्थित हैं। कोई सामान्य यूनिक्स सॉकेट और दोनों सिरों पर भेज और प्राप्त करें, अगर यह इस तरह से खोला गया है। मुझे लगता है कि बहुत समय पहले, यूनिक्स पाइप केवल एक तरफ थे, लेकिन यह बहुत पुराना है। विंडोज़ पर पूर्ण डुप्लेक्स पाइप का उपयोग करने की चाल यह सुनिश्चित करने के लिए है कि चीजें कैसे की जाती हैं, और फिर पाइप उलझन में नहीं आती है। यदि आप दो पाइप का उपयोग करते हैं, तो मुझे यकीन है कि यह आसान बनाता है, लेकिन आप संसाधनों और पोर्ट आवंटन को बर्बाद कर रहे हैं। –

+0

ओह..मैं देखता हूँ। मुझे यह नहीं पता था। आपके ज्ञान के लिए धन्यवाद .. मुझे और अध्ययन करना है। – lucidmaj7

6

आपको दो पाइप का उपयोग करने की आवश्यकता नहीं है। मुझे नेट पर बहुत सारे उत्तर मिलते हैं जो बताते हैं कि आपको दो पाइप का उपयोग करने की आवश्यकता है। मैंने चारों ओर खोला, सारी रात रुक गई, कोशिश की और फिर से कोशिश की, और यह पता लगाया कि यह कैसे करना है, यह बहुत आसान है, लेकिन आपको सबकुछ ठीक से प्राप्त करना होगा (विशेष रूप से सही कॉलिंग ऑर्डर में चीजें प्राप्त करें), या यह काम नहीं करेगा । एक और चाल हमेशा यह सुनिश्चित करना है कि आपके पास बकाया कॉल बकाया है, या यह भी लॉक हो जाएगा। इससे पहले कि आप किसी को पढ़ रहे हों, लिखने से पहले मत लिखो। जब तक आपके पास ईवेंट पहले सेट न हो जाए तब तक एक रीड कॉल प्रारंभ न करें। उस तरह की चीस।

यहां पाइप क्लास का उपयोग कर रहा हूं। यह शायद पाइप त्रुटियों, बंद, और अतिप्रवाह से निपटने के लिए पर्याप्त मजबूत नहीं है।

ठीक है मुझे नहीं पता कि यहां क्या गलत है, लेकिन स्वरूपण थोड़ा दूर है! vvvv

namespace Squall 
{ 
    public interface PipeSender 
    { 
     Task SendCommandAsync(PipeCommandPlusString pCmd); 
    } 

    /****************************************************************************** 
    * 
    * 
    * 
    * 
    ******************************************************************************/ 
    public class ClientPipe : BasicPipe 
    { 
     NamedPipeClientStream m_pPipe; 

     public ClientPipe(string szServerName, string szPipeName) 
      : base("Client") 
     { 
      m_szPipeName = szPipeName; // debugging 
      m_pPipe = new NamedPipeClientStream(szServerName, szPipeName, PipeDirection.InOut, PipeOptions.Asynchronous); 
      base.SetPipeStream(m_pPipe); // inform base class what to read/write from 
     } 

     public void Connect() 
     { 
      Debug.WriteLine("Pipe " + FullPipeNameDebug() + " connecting to server"); 
      m_pPipe.Connect(); // doesn't seem to be an async method for this routine. just a timeout. 
      StartReadingAsync(); 
     } 

     // the client's pipe index is always 0 
     internal override int PipeId() { return 0; } 
    } 

    /****************************************************************************** 
    * 
    * 
    * 
    * 
    ******************************************************************************/ 
    public class ServerPipe : BasicPipe 
    { 
     public event EventHandler<EventArgs> GotConnectionEvent; 

     NamedPipeServerStream m_pPipe; 
     int m_nPipeId; 

     public ServerPipe(string szPipeName, int nPipeId) 
      : base("Server") 
     { 
      m_szPipeName = szPipeName; 
      m_nPipeId = nPipeId; 
      m_pPipe = new NamedPipeServerStream(
       szPipeName, 
       PipeDirection.InOut, 
       NamedPipeServerStream.MaxAllowedServerInstances, 
       PipeTransmissionMode.Message, 
       PipeOptions.Asynchronous); 
      base.SetPipeStream(m_pPipe); 
      m_pPipe.BeginWaitForConnection(new AsyncCallback(StaticGotPipeConnection), this); 
     } 

     static void StaticGotPipeConnection(IAsyncResult pAsyncResult) 
     { 
      ServerPipe pThis = pAsyncResult.AsyncState as ServerPipe; 
      pThis.GotPipeConnection(pAsyncResult); 
     } 

     void GotPipeConnection(IAsyncResult pAsyncResult) 
     { 
      m_pPipe.EndWaitForConnection(pAsyncResult); 

      Debug.WriteLine("Server Pipe " + m_szPipeName + " got a connection"); 

      if (GotConnectionEvent != null) 
      { 
       GotConnectionEvent(this, new EventArgs()); 
      } 

      // lodge the first read request to get us going 
      // 
      StartReadingAsync(); 
     } 

     internal override int PipeId() { return m_nPipeId; } 
    } 

    /****************************************************************************** 
    * 
    * 
    * 
    * 
    ******************************************************************************/ 

    public abstract class BasicPipe : PipeSender 
    { 
     public static int MaxLen = 1024 * 1024; // why not 
     protected string m_szPipeName; 
     protected string m_szDebugPipeName; 

     public event EventHandler<PipeEventArgs> ReadDataEvent; 
     public event EventHandler<EventArgs> PipeClosedEvent; 

     protected byte[] m_pPipeBuffer = new byte[BasicPipe.MaxLen]; 

     PipeStream m_pPipeStream; 

     public BasicPipe(string szDebugPipeName) 
     { 
      m_szDebugPipeName = szDebugPipeName; 
     } 

     protected void SetPipeStream(PipeStream p) 
     { 
      m_pPipeStream = p; 
     } 

     protected string FullPipeNameDebug() 
     { 
      return m_szDebugPipeName + "-" + m_szPipeName; 
     } 

     internal abstract int PipeId(); 

     public void Close() 
     { 
      m_pPipeStream.WaitForPipeDrain(); 
      m_pPipeStream.Close(); 
      m_pPipeStream.Dispose(); 
      m_pPipeStream = null; 
     } 

     // called when Server pipe gets a connection, or when Client pipe is created 
     public void StartReadingAsync() 
     { 
      Debug.WriteLine("Pipe " + FullPipeNameDebug() + " calling ReadAsync"); 

      // okay we're connected, now immediately listen for incoming buffers 
      // 
      byte[] pBuffer = new byte[MaxLen]; 
      m_pPipeStream.ReadAsync(pBuffer, 0, MaxLen).ContinueWith(t => 
      { 
       Debug.WriteLine("Pipe " + FullPipeNameDebug() + " finished a read request"); 

       int ReadLen = t.Result; 
       if (ReadLen == 0) 
       { 
        Debug.WriteLine("Got a null read length, remote pipe was closed"); 
        if (PipeClosedEvent != null) 
        { 
         PipeClosedEvent(this, new EventArgs()); 
        } 
        return; 
       } 

       if (ReadDataEvent != null) 
       { 
        ReadDataEvent(this, new PipeEventArgs(pBuffer, ReadLen)); 
       } 
       else 
       { 
        Debug.Assert(false, "something happened"); 
       } 

       // lodge ANOTHER read request 
       // 
       StartReadingAsync(); 

      }); 
     } 

     protected Task WriteByteArray(byte[] pBytes) 
     { 
      // this will start writing, but does it copy the memory before returning? 
      return m_pPipeStream.WriteAsync(pBytes, 0, pBytes.Length); 
     } 

     public Task SendCommandAsync(PipeCommandPlusString pCmd) 
     { 
      Debug.WriteLine("Pipe " + FullPipeNameDebug() + ", writing " + pCmd.GetCommand() + "-" + pCmd.GetTransmittedString()); 
      string szSerializedCmd = JsonConvert.SerializeObject(pCmd); 
      byte[] pSerializedCmd = Misc.StringToBytes(szSerializedCmd); 
      Task t = WriteByteArray(pSerializedCmd); 
      return t; 
     } 
    } 

    /****************************************************************************** 
    * 
    * 
    * 
    * 
    ******************************************************************************/ 

    public class PipeEventArgs 
    { 
     public byte[] m_pData; 
     public int m_nDataLen; 

     public PipeEventArgs(byte[] pData, int nDataLen) 
     { 
      // is this a copy, or an alias copy? I can't remember right now. 
      m_pData = pData; 
      m_nDataLen = nDataLen; 
     } 
    } 

    /****************************************************************************** 
    * if we're just going to send a string back and forth, then we can use this 
    * class. It it allows us to get the bytes as a string. sort of silly. 
    ******************************************************************************/ 

    [Serializable] 
    public class PipeCommandPlusString 
    { 
     public string m_szCommand; // must be public to be serialized 
     public string m_szString; // ditto 

     public PipeCommandPlusString(string sz, string szString) 
     { 
      m_szCommand = sz; 
      m_szString = szString; 
     } 

     public string GetCommand() 
     { 
      return m_szCommand; 
     } 

     public string GetTransmittedString() 
     { 
      return m_szString; 
     } 
    } 
} 

और यहाँ एक प्रक्रिया पर चल रहा है मेरी पाइप परीक्षण है। यह दो प्रक्रियाओं पर भी चलाता है, मैं

namespace NamedPipeTest 
{ 
    public partial class Form1 : Form 
    { 
     SynchronizationContext _context; 
     Thread m_pThread = null; 
     volatile bool m_bDieThreadDie; 
     ServerPipe m_pServerPipe; 
     ClientPipe m_pClientPipe; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      _context = SynchronizationContext.Current; 

      m_pServerPipe = new ServerPipe("SQUALL_PIPE", 0); 
      m_pServerPipe.ReadDataEvent += M_pServerPipe_ReadDataEvent; 
      m_pServerPipe.PipeClosedEvent += M_pServerPipe_PipeClosedEvent; 

      // m_pThread = new Thread(StaticThreadProc); 
      // m_pThread.Start(this); 
     } 

     private void M_pServerPipe_PipeClosedEvent(object sender, EventArgs e) 
     { 
      Debug.WriteLine("Server: Pipe was closed, shutting down"); 

      // have to post this on the main thread 
      _context.Post(delegate 
      { 
       Close(); 
      }, null); 
     } 

     private void M_pServerPipe_ReadDataEvent(object sender, PipeEventArgs e) 
     { 
      // this gets called on an anonymous thread 

      byte[] pBytes = e.m_pData; 
      string szBytes = Misc.BytesToString(pBytes, e.m_pData.Length); 
      PipeCommandPlusString pCmd = JsonConvert.DeserializeObject<PipeCommandPlusString>(szBytes); 
      string szValue = pCmd.GetTransmittedString(); 

      if (szValue == "CONNECT") 
      { 
       Debug.WriteLine("Got command from client: " + pCmd.GetCommand() + "-" + pCmd.GetTransmittedString() + ", writing command back to client"); 
       PipeCommandPlusString pCmdToSend = new PipeCommandPlusString("SERVER", "CONNECTED"); 
       // fire off an async write 
       Task t = m_pServerPipe.SendCommandAsync(pCmdToSend); 
      } 
     } 

     static void StaticThreadProc(Object o) 
     { 
      Form1 pThis = o as Form1; 
      pThis.ThreadProc(); 
     } 

     void ThreadProc() 
     { 
      m_pClientPipe = new ClientPipe(".", "SQUALL_PIPE"); 
      m_pClientPipe.ReadDataEvent += PClientPipe_ReadDataEvent; 
      m_pClientPipe.PipeClosedEvent += M_pClientPipe_PipeClosedEvent; 
      m_pClientPipe.Connect(); 

      PipeCommandPlusString pCmd = new PipeCommandPlusString("CLIENT", "CONNECT"); 
      int Counter = 1; 
      while (Counter++ < 10) 
      { 
       Debug.WriteLine("Counter = " + Counter); 
       m_pClientPipe.SendCommandAsync(pCmd); 
       Thread.Sleep(3000); 
      } 

      while (!m_bDieThreadDie) 
      { 
       Thread.Sleep(1000); 
      } 

      m_pClientPipe.ReadDataEvent -= PClientPipe_ReadDataEvent; 
      m_pClientPipe.PipeClosedEvent -= M_pClientPipe_PipeClosedEvent; 
      m_pClientPipe.Close(); 
      m_pClientPipe = null; 
     } 

     private void M_pClientPipe_PipeClosedEvent(object sender, EventArgs e) 
     { 
      // wait around for server to shut us down 
     } 

     private void PClientPipe_ReadDataEvent(object sender, PipeEventArgs e) 
     { 
      byte[] pBytes = e.m_pData; 
      string szBytes = Misc.BytesToString(pBytes, e.m_nDataLen); 
      PipeCommandPlusString pCmd = JsonConvert.DeserializeObject<PipeCommandPlusString>(szBytes); 
      string szValue = pCmd.GetTransmittedString(); 

      Debug.WriteLine("Got command from server: " + pCmd.GetCommand() + "-" + pCmd.GetTransmittedString()); 

      if (szValue == "CONNECTED") 
      { 
       PipeCommandPlusString pCmdToSend = new PipeCommandPlusString("CLIENT", "DATA"); 
       m_pClientPipe.SendCommandAsync(pCmdToSend); 
      } 
     } 

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      if (m_pThread != null) 
      { 
       m_bDieThreadDie = true; 
       m_pThread.Join(); 
       m_bDieThreadDie = false; 
      } 

      m_pServerPipe.ReadDataEvent -= M_pServerPipe_ReadDataEvent; 
      m_pServerPipe.PipeClosedEvent -= M_pServerPipe_PipeClosedEvent; 
      m_pServerPipe.Close(); 
      m_pServerPipe = null; 

     } 
    } 
} 
0

जाँच के रूप में ओवरलैप बस पाइप बनाते हैं और अपने कोड एक सूत्र में पढ़ने पर ब्लॉक कर सकते हैं, जबकि एक अन्य से पाइप के लिए लिख।

void StartServer() 
    { 
     Task.Factory.StartNew(() => 
     { 
      var server = new NamedPipeServerStream("PipesOfPiece", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); 
      server.WaitForConnection(); 
      reader = new StreamReader(server); 
      writer = new StreamWriter(server); 
     }); 
    } 

    private async void timer1_Tick(object sender, EventArgs e) 
    { 
     timer1.Stop(); 
     if (null != reader) 
     { 
      char[] buf = new char[50]; 

      int count = await reader.ReadAsync(buf, 0, 50); 

      if (0 < count) 
      { 
       m_textBox_from.Text = new string(buf, 0, count); 
      } 
     } 
     timer1.Start(); 
    } 
संबंधित मुद्दे