Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Socket programming

Socket programming

Scheduled Pinned Locked Moved C#
sysadminjsonhelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    Hi I have two application which I have managed to get them to exchange data however, I would like to be able to sort the data out when it is recieved by the other end (server). On the client side, the data is taken from the various forms and sent as below:

                 String Pname = LblName.Text;
                String Pdob = LblDOB.Text;
                String PnhsNo = LblNHSno.Text;
                String Paddress = RtxtBxAddre.Text;
                String PmedicalCon = RtxtBxMedCon.Text;
                String pcallOutDetails = RTxtBxCallOut.Text;
    
                byte\[\] forwardMessage = Encoding.ASCII.GetBytes(Pname + Pdob + PnhsNo + Paddress + PmedicalCon + pcallOutDetails);
               sock.Send(forwardMessage);
    
                sock.Shutdown(SocketShutdown.Both);
                sock.Close();
    

    On the other end I would now like to take each item and display it in a form, so "Pname" is taken when received and displayed in a text box and then same with the rest of the data. Unlike displaying it in a message box like I have done below:

    Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    listener.Bind(new IPEndPoint(IPAddress.Any, 2112));
    listener.Listen(10);

           Socket socket = listener.Accept();
           string receivedValue = string.Empty;
    
           
                     byte\[\] receivedBytes = new byte\[1024\];
                      int numBytes = socket.Receive(receivedBytes);
                          
                              receivedValue = Encoding.ASCII.GetString(receivedBytes,0, numBytes);
                             
                                    MessageBox.Show(receivedValue);
                                                                    
                                    listener.Close();
    

    Help....:confused:

    P L T 4 Replies Last reply
    0
    • T Tichaona J

      Hi I have two application which I have managed to get them to exchange data however, I would like to be able to sort the data out when it is recieved by the other end (server). On the client side, the data is taken from the various forms and sent as below:

                   String Pname = LblName.Text;
                  String Pdob = LblDOB.Text;
                  String PnhsNo = LblNHSno.Text;
                  String Paddress = RtxtBxAddre.Text;
                  String PmedicalCon = RtxtBxMedCon.Text;
                  String pcallOutDetails = RTxtBxCallOut.Text;
      
                  byte\[\] forwardMessage = Encoding.ASCII.GetBytes(Pname + Pdob + PnhsNo + Paddress + PmedicalCon + pcallOutDetails);
                 sock.Send(forwardMessage);
      
                  sock.Shutdown(SocketShutdown.Both);
                  sock.Close();
      

      On the other end I would now like to take each item and display it in a form, so "Pname" is taken when received and displayed in a text box and then same with the rest of the data. Unlike displaying it in a message box like I have done below:

      Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
      listener.Bind(new IPEndPoint(IPAddress.Any, 2112));
      listener.Listen(10);

             Socket socket = listener.Accept();
             string receivedValue = string.Empty;
      
             
                       byte\[\] receivedBytes = new byte\[1024\];
                        int numBytes = socket.Receive(receivedBytes);
                            
                                receivedValue = Encoding.ASCII.GetString(receivedBytes,0, numBytes);
                               
                                      MessageBox.Show(receivedValue);
                                                                      
                                      listener.Close();
      

      Help....:confused:

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You need to put in a delimiter to split the elements at the receiving end. One way to do this would be to use an uncommon character when forming the string, which you can then parse out - don't use a comma just in cast it's present in the input data. I like to use \n in this situation because it's highly unlikely that this will ever be in the input data. Then you simply split the data out at the receiving end using this character sequence.

      I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      • T Tichaona J

        Hi I have two application which I have managed to get them to exchange data however, I would like to be able to sort the data out when it is recieved by the other end (server). On the client side, the data is taken from the various forms and sent as below:

                     String Pname = LblName.Text;
                    String Pdob = LblDOB.Text;
                    String PnhsNo = LblNHSno.Text;
                    String Paddress = RtxtBxAddre.Text;
                    String PmedicalCon = RtxtBxMedCon.Text;
                    String pcallOutDetails = RTxtBxCallOut.Text;
        
                    byte\[\] forwardMessage = Encoding.ASCII.GetBytes(Pname + Pdob + PnhsNo + Paddress + PmedicalCon + pcallOutDetails);
                   sock.Send(forwardMessage);
        
                    sock.Shutdown(SocketShutdown.Both);
                    sock.Close();
        

        On the other end I would now like to take each item and display it in a form, so "Pname" is taken when received and displayed in a text box and then same with the rest of the data. Unlike displaying it in a message box like I have done below:

        Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        listener.Bind(new IPEndPoint(IPAddress.Any, 2112));
        listener.Listen(10);

               Socket socket = listener.Accept();
               string receivedValue = string.Empty;
        
               
                         byte\[\] receivedBytes = new byte\[1024\];
                          int numBytes = socket.Receive(receivedBytes);
                              
                                  receivedValue = Encoding.ASCII.GetString(receivedBytes,0, numBytes);
                                 
                                        MessageBox.Show(receivedValue);
                                                                        
                                        listener.Close();
        

        Help....:confused:

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Tichaona J wrote:

        Encoding.ASCII.GetBytes(Pname + Pdob + PnhsNo + Paddress + PmedicalCon + pcallOutDetails);

        you can't just concatenate some strings and get away with it, there is no way to separate them again. What if they represent numbers? is 12345 the result of 1 and 2345? or 12 and 345? or... What you could do is this: - define a little class that holds the variables (either as strings or, better, as their actual type); - add [Serializable] attribute; - make one instance and fill the values from the Form (or whatever your source of information is); - use XmlSerialize class to turn that object into one XML string; - transmit that string; - use XmlSerialize class again to decode the XML string and recreate an instance of your object. I have an example here[^]. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        M 1 Reply Last reply
        0
        • L Luc Pattyn

          Tichaona J wrote:

          Encoding.ASCII.GetBytes(Pname + Pdob + PnhsNo + Paddress + PmedicalCon + pcallOutDetails);

          you can't just concatenate some strings and get away with it, there is no way to separate them again. What if they represent numbers? is 12345 the result of 1 and 2345? or 12 and 345? or... What you could do is this: - define a little class that holds the variables (either as strings or, better, as their actual type); - add [Serializable] attribute; - make one instance and fill the values from the Form (or whatever your source of information is); - use XmlSerialize class to turn that object into one XML string; - transmit that string; - use XmlSerialize class again to decode the XML string and recreate an instance of your object. I have an example here[^]. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          you can't just concatenate some strings and get away with it

          You would be surprised how many people do get away with it thou - running before walking that is, some even just skip both and try to swim - and CP is the bottom of the ocean (help to untie concrete boots - URGENTZ!!!)

          Life goes very fast. Tomorrow, today is already yesterday.

          1 Reply Last reply
          0
          • T Tichaona J

            Hi I have two application which I have managed to get them to exchange data however, I would like to be able to sort the data out when it is recieved by the other end (server). On the client side, the data is taken from the various forms and sent as below:

                         String Pname = LblName.Text;
                        String Pdob = LblDOB.Text;
                        String PnhsNo = LblNHSno.Text;
                        String Paddress = RtxtBxAddre.Text;
                        String PmedicalCon = RtxtBxMedCon.Text;
                        String pcallOutDetails = RTxtBxCallOut.Text;
            
                        byte\[\] forwardMessage = Encoding.ASCII.GetBytes(Pname + Pdob + PnhsNo + Paddress + PmedicalCon + pcallOutDetails);
                       sock.Send(forwardMessage);
            
                        sock.Shutdown(SocketShutdown.Both);
                        sock.Close();
            

            On the other end I would now like to take each item and display it in a form, so "Pname" is taken when received and displayed in a text box and then same with the rest of the data. Unlike displaying it in a message box like I have done below:

            Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            listener.Bind(new IPEndPoint(IPAddress.Any, 2112));
            listener.Listen(10);

                   Socket socket = listener.Accept();
                   string receivedValue = string.Empty;
            
                   
                             byte\[\] receivedBytes = new byte\[1024\];
                              int numBytes = socket.Receive(receivedBytes);
                                  
                                      receivedValue = Encoding.ASCII.GetString(receivedBytes,0, numBytes);
                                     
                                            MessageBox.Show(receivedValue);
                                                                            
                                            listener.Close();
            

            Help....:confused:

            T Offline
            T Offline
            Tichaona J
            wrote on last edited by
            #5

            I have since tried to put the strings into an arraylist but question now is how do you convert the array list into bytes, cause this doesn't work:

            //Add items to the arraylist
            patient.Add(Pname);
            patient.Add(Pdob);
            patient.Add(PnhsNo);
            patient.Add(Paddress);
            patient.Add(PmedicalCon);
            patient.Add(pcallOutDetails);

                        byte\[\] forwardMessage = Encoding.ASCII.GetBytes(patient);
                        
                        sock.Send(forwardMessage);
            
                        
                        sock.Shutdown(SocketShutdown.Both);
                        sock.Close();
            
            M 1 Reply Last reply
            0
            • T Tichaona J

              I have since tried to put the strings into an arraylist but question now is how do you convert the array list into bytes, cause this doesn't work:

              //Add items to the arraylist
              patient.Add(Pname);
              patient.Add(Pdob);
              patient.Add(PnhsNo);
              patient.Add(Paddress);
              patient.Add(PmedicalCon);
              patient.Add(pcallOutDetails);

                          byte\[\] forwardMessage = Encoding.ASCII.GetBytes(patient);
                          
                          sock.Send(forwardMessage);
              
                          
                          sock.Shutdown(SocketShutdown.Both);
                          sock.Close();
              
              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              A byte[] is just a bad as a string in that if you want to split it at the other end you need to do some work on it prior to creating it (like give it something to be split by) Really thou, you should follow Luc's advice - he knows what he is talking about

              Life goes very fast. Tomorrow, today is already yesterday.

              L 1 Reply Last reply
              0
              • M musefan

                A byte[] is just a bad as a string in that if you want to split it at the other end you need to do some work on it prior to creating it (like give it something to be split by) Really thou, you should follow Luc's advice - he knows what he is talking about

                Life goes very fast. Tomorrow, today is already yesterday.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                I can't argue with that. Although Pete's approach is fine too under the circumstances (single-line TextBoxes). :laugh:

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                1 Reply Last reply
                0
                • T Tichaona J

                  Hi I have two application which I have managed to get them to exchange data however, I would like to be able to sort the data out when it is recieved by the other end (server). On the client side, the data is taken from the various forms and sent as below:

                               String Pname = LblName.Text;
                              String Pdob = LblDOB.Text;
                              String PnhsNo = LblNHSno.Text;
                              String Paddress = RtxtBxAddre.Text;
                              String PmedicalCon = RtxtBxMedCon.Text;
                              String pcallOutDetails = RTxtBxCallOut.Text;
                  
                              byte\[\] forwardMessage = Encoding.ASCII.GetBytes(Pname + Pdob + PnhsNo + Paddress + PmedicalCon + pcallOutDetails);
                             sock.Send(forwardMessage);
                  
                              sock.Shutdown(SocketShutdown.Both);
                              sock.Close();
                  

                  On the other end I would now like to take each item and display it in a form, so "Pname" is taken when received and displayed in a text box and then same with the rest of the data. Unlike displaying it in a message box like I have done below:

                  Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                  listener.Bind(new IPEndPoint(IPAddress.Any, 2112));
                  listener.Listen(10);

                         Socket socket = listener.Accept();
                         string receivedValue = string.Empty;
                  
                         
                                   byte\[\] receivedBytes = new byte\[1024\];
                                    int numBytes = socket.Receive(receivedBytes);
                                        
                                            receivedValue = Encoding.ASCII.GetString(receivedBytes,0, numBytes);
                                           
                                                  MessageBox.Show(receivedValue);
                                                                                  
                                                  listener.Close();
                  

                  Help....:confused:

                  T Offline
                  T Offline
                  Tichaona J
                  wrote on last edited by
                  #8

                  Thanks guys...With you hints and tips I have figured it out. Part of the solution (the code any) came from this site: msdn library Once again thanks... :) :) :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups