2010-07-28 13 views
7

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

फ़ाइलों के लिए ग्राहक के अनुरोध सर्वर उपज त्रुटि कुछ की तरह "क्योंकि लक्ष्य मशीन सक्रिय रूप से इसे मना कर दिया 192.168.1.5:9050 कोई कनेक्शन नहीं बनाया जा सकता है" जब

सर्वर साइड कोड:

private bool SendData(string StrIP) 
    { 

     try 
     { 
      string strmyFile = Directory.GetCurrentDirectory() + "\\" + "XML" + @"\Login.xml"; 
      char[] delimiter = splitter.ToCharArray(); 
      split = strmyFile.Split(delimiter); 
      int limit = split.Length; 
      fName = split[limit - 1].ToString(); 

      byte[] fileName = Encoding.UTF8.GetBytes(fName); //file name 
      byte[] fileData = File.ReadAllBytes(strmyFile); //file 
      using (FileStream stream = File.OpenRead(strmyFile)) 
      { 
       fileData = new byte[stream.Length]; 
       stream.Read(fileData, 0, fileData.Length); 
      } 

      byte[] fileNameLen = BitConverter.GetBytes(fileName.Length); //lenght of file name 
      clientData = new byte[4 + fileName.Length + fileData.Length]; 

      fileNameLen.CopyTo(clientData, 0); 
      fileName.CopyTo(clientData, 4); 
      fileData.CopyTo(clientData, 4 + fileName.Length); 

      System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(StrIP); 

      try 
      { 
       Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
       IPEndPoint ipEnd = new IPEndPoint(ipAdd, 9050); 
       clientSock.Connect(ipEnd); //target machine's ip address and the port number 
       clientSock.Send(clientData); 
       clientSock.Close(); 
       ind = 1; 

       //Socket Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
       //IPEndPoint ipEnd = new IPEndPoint(ipAdd, 9050); 
       //Sock.Bind(ipEnd);//target machine's ip address and the port number 
       //Sock.Listen(100); 

       //Socket clientSock = Sock.Accept(); 

       //clientSock.Send(clientData); 
       //clientSock.Close(); 
       //ind = 1; 
      } 
      catch (Exception ex) 
      { 

       req = false; 
       return false; 

      } 
      req = true; 
     } 
     catch (Exception ex) 
     { 

     } 
     return true; 
    } 


    public class StateObject 
    { 
     // Client socket. 
     public Socket workSocket = null; 

     public const int BufferSize = 1024 * 5000; 
     // Receive buffer. 
     public byte[] buffer = new byte[BufferSize]; 
    } 

    public static ManualResetEvent allDone = new ManualResetEvent(false); 

    public void StartListening() 
    { 


     byte[] bytes = new Byte[1024 * 5000]; 
     IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 9050); 
     Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     try 
     { 
      listener.Bind(ipEnd); 
      listener.Listen(100); 

      while (true) 
      { 
       allDone.Reset(); 
       // string ip = listener.RemoteEndPoint.ToString(); 
       listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); 
       allDone.WaitOne(); 

      } 
     } 
     catch (Exception ex) 
     { 

     } 

    } 


    public void AcceptCallback(IAsyncResult ar) 
    { 

     allDone.Set(); 


     Socket listener = (Socket)ar.AsyncState; 
     Socket handler = listener.EndAccept(ar); 

     StateObject state = new StateObject(); 
     state.workSocket = handler; 
     handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
     new AsyncCallback(ReadCallback), state); 

     flag = 0; 

    } 

    private bool req = false; 
    public void ReadCallback(IAsyncResult ar) 
    { 
     try 
     { 

      int fileNameLen = 1; 
      String content = String.Empty; 
      StateObject state = (StateObject)ar.AsyncState; 
      Socket handler = state.workSocket; 
      string[] str = new string[2]; 
      str = handler.RemoteEndPoint.ToString().Split(':'); 
      string path = Directory.GetCurrentDirectory() + "\\TEST\\" + str[0].ToString(); 
      if (!Directory.Exists(path)) 
      { 
       Directory.CreateDirectory(path); 
      } 
      if (req == false) 
      { 
       Sender snddata = new Sender(); 
       snddata.sendme(str[0].ToString()); 
       SendData(str[0].ToString()); 
      } 

      int bytesRead = handler.EndReceive(ar); 

      if (bytesRead > 0) 
      { 
        if (flag == 0) 
        { 
         fileNameLen = BitConverter.ToInt32(state.buffer, 0); 
         string fileName = Encoding.UTF8.GetString(state.buffer, 4, fileNameLen); 
         receivedPath = Directory.GetCurrentDirectory() + "\\TEST\\" + str[0].ToString() + @"\" + fileName; 
         flag++; 
        } 
        if (flag >= 1) 
        { 
         BinaryWriter writer = new BinaryWriter(File.Open(receivedPath, FileMode.Append)); 
         if (flag == 1) 
         { 
          writer.Write(state.buffer, 4 + fileNameLen, bytesRead - (4 + fileNameLen)); 
          flag++; 
         } 
         else 
          writer.Write(state.buffer, 0, bytesRead); 
         writer.Close(); 
         handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
         new AsyncCallback(ReadCallback), state); 
        } 
      } 
      else 
      { 

       Invoke(new MyDelegate(LabelWriter)); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 

     } 
    } 
public class Sender 
{ 
    public void sendme(string strIP) 
    { 
     try 
     { 
      // MessageBox.Show("That program can transfer small file. I've test up to 850kb file"); 
      IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 9050); 
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 
      sock.Bind(ipEnd); 
      sock.Listen(100); 

      while (true) 
      { 
       //clientSock is the socket object of client, so we can use it now to transfer data to client 
       Socket clientSock = sock.Accept(); 


       string fileName = "Login.xml";// "Your File Name"; 
       string filePath = Directory.GetCurrentDirectory() + "\\" + "XML" + @"\" ;//Your File Path; 
       byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName); 

       byte[] fileData = File.ReadAllBytes(filePath + fileName); 
       byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length]; 
       byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length); 

       fileNameLen.CopyTo(clientData, 0); 
       fileNameByte.CopyTo(clientData, 4); 
       fileData.CopyTo(clientData, 4 + fileNameByte.Length); 


       clientSock.Send(clientData); 
       //MessageBox.Show("File:{0} has been sent." + fileName); 
      } 
      //sock.Shutdown(SocketShutdown.Both); 
      //sock.Close(); 


      // Console.ReadLine(); 
      //sendme(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("File Receiving fail." + ex.Message); 
     } 
    } 
} 

क्लाइंट पक्ष कोड:

public class StateObject 
    { 
     // Client socket. 
     public Socket workSocket = null; 

     public const int BufferSize = 1024; 
     // Receive buffer. 
     public byte[] buffer = new byte[BufferSize]; 
    } 

    public static ManualResetEvent allDone = new ManualResetEvent(false); 

    public void StartListening() 
    { 
     byte[] bytes = new Byte[1024]; 
     IPAddress address = IPAddress.Parse(ServerIP); 
     IPEndPoint ipEnd = new IPEndPoint(address, 9050); 
     Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     try 
     { 
      listener.Bind(ipEnd); 
      listener.Listen(100); 

      while (true) 
      { 
       allDone.Reset(); 
       // string ip = listener.RemoteEndPoint.ToString(); 
       listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); 
       allDone.WaitOne(); 

      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 


    } 


    public void AcceptCallback(IAsyncResult ar) 
    { 

     allDone.Set(); 


     Socket listener = (Socket)ar.AsyncState; 
     Socket handler = listener.EndAccept(ar); 


     StateObject state = new StateObject(); 
     state.workSocket = handler; 
     handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
     new AsyncCallback(ReadCallback), state); 
     flag = 0; 
    } 

    public void ReadCallback(IAsyncResult ar) 
    { 

     try 
     { 

      int fileNameLen = 1; 
      String content = String.Empty; 
      StateObject state = (StateObject)ar.AsyncState; 
      Socket handler = state.workSocket; 
      //string[] str = new string[2]; 
      //str = handler.RemoteEndPoint.ToString().Split(':'); 
      string path = mypath + "@Login.XML"; 
      if (!Directory.Exists(path)) 
      { 
       Directory.CreateDirectory(path); 
      } 

      int bytesRead = handler.EndReceive(ar); 

      if (bytesRead > 0) 
      { 

       if (flag == 0) 
       { 
        fileNameLen = BitConverter.ToInt32(state.buffer, 0); 
        string fileName = Encoding.UTF8.GetString(state.buffer, 4, fileNameLen); 
        receivedPath = mypath + "@Login.XML"; 
        flag++; 
       } 
       if (flag >= 1) 
       { 
        BinaryWriter writer = new BinaryWriter(File.Open(receivedPath, FileMode.Append)); 
        if (flag == 1) 
        { 
         writer.Write(state.buffer, 4 + fileNameLen, bytesRead - (4 + fileNameLen)); 
         flag++; 
        } 
        else 
         writer.Write(state.buffer, 0, bytesRead); 
        writer.Close(); 
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
        new AsyncCallback(ReadCallback), state); 
       } 

      } 
      else 
      { 

       Invoke(new MyDelegate(LabelWriter)); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 

     } 
    } 
    public void LabelWriter() 
    { 
     MessageBox.Show("Data has been received"); 
     Programs p = new Programs(); 
     p.GetFile(serverIP); 
    } 

इस कोड को सर्वर से फ़ाइल प्राप्त करने के लिए प्रयोग किया जाता है।

private void GetLoginTemp() 
    { 
     Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     try 
     { 
      char[] delimiter = splitter.ToCharArray(); 
      byte[] fileName = Encoding.UTF8.GetBytes("empty"); //file name 
      byte[] fileData; 
      fileData = Encoding.UTF8.GetBytes("empty"); 
      //byte[] fileData = reads.ReadToEnd().to; //file 
      byte[] fileNameLen = BitConverter.GetBytes(fileName.Length); //lenght of file name 
      clientData = new byte[4 + fileName.Length + fileData.Length]; 

      fileNameLen.CopyTo(clientData, 0); 
      fileName.CopyTo(clientData, 4); 
      fileData.CopyTo(clientData, 4 + fileName.Length); 
      System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(serverIP); 

      IPEndPoint ipEnd = new IPEndPoint(ipAdd, 9050); 
      clientSock.Connect(ipEnd); //target machine's ip address and the port number 
      clientSock.Send(clientData); 

      byte[] clientData1 = new byte[1024 * 5000]; 
      string receivedPath = mypath + @"\XML" + @"\Login.xml"; 

      int receivedBytesLen = clientSock.Receive(clientData1); 

      int fileNameLen1 = BitConverter.ToInt32(clientData1, 0); 
      string fileName1 = Encoding.ASCII.GetString(clientData1, 4, fileNameLen1); 

      BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ; 
      bWrite.Write(clientData, 4 + fileNameLen1, receivedBytesLen - 4 - fileNameLen1); 
      //clientSock.Shutdown(SocketShutdown.Send); 

      clientSock.Close(); 
     } 
     catch (Exception ex) 
     { 
      clientSock.Close(); 
      MessageBox.Show(ex.Message); 
     } 
    } 

उत्तर

8

आपको लगता है कि आपको उस दिशा में दूसरे टीसीपी कनेक्शन की आवश्यकता है जहां फ़ाइल चल रही है। यह आवश्यक नहीं है क्योंकि टीसीपी कनेक्शन पूर्ण-डुप्लेक्स स्ट्रीम है। सर्वर से वापस क्लाइंट से कनेक्ट करने के लिए यह कभी भी एक अच्छा डिज़ाइन नहीं है, जो कि एनएटी-आईएनजी गेटवे/फ़ायरवॉल के पीछे हो सकता है। यह एफ़टीपी के साथ समस्या है (देखें active vs passive ftp)।

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

0
बस त्रुटि आप (मैं एक ही त्रुटि कई कई बार मिल गया है) की जाँच करें कि अंतर्निहित धारा आप अपने संचार के लिए प्रयोग कर रहे हैं बंद नहीं है, या आप फिर से करने का प्रयास नहीं कर रहे हैं कि हो रही है से

एक बंद धारा का उपयोग करें।

क्या आपके पास एक विशिष्ट रेखा है जहां त्रुटि हो रही है?

+0

ब्रैंडन मैं उस त्रुटि "क्योंकि लक्ष्य मशीन सक्रिय रूप से इसे मना कर दिया 192.168.1.5:9050 कोई कनेक्शन नहीं बनाया जा सकता है" का सामना भेजा गया हो तोयह त्रुटि मुझे senddata फ़ंक्शन में सामना करती है। –

+0

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

0

कनेक्शन सुनिश्चित करने के लिए आप सिस्टम फ़ायरवॉल जांचना चाहेंगे। पिंग और टेलनेट का उपयोग डीबगिंग के लिए बहुत अच्छा है।

0

यदि यह फ़ायरवॉल नहीं है, मुझे लगता है कि के रूप में सुनने के लिए सर्वर डाल करने के लिए (सर्वर शुरू करने के लिए)

मैं कोड के इस टुकड़े का सुझाव आपकी समस्या का समाधान के रूप में सरल है:

var prg = StateObject(); 
prg.StartListening; 

के बाद आप इस का प्रबंधन करेंगे, कृपया मुझे बताओ 8KB से अधिक फ़ाइल पूर्ण

संबंधित मुद्दे