Socket programming
-
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:
-
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:
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
-
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:
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.
-
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.
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.
-
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:
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();
-
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();
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.
-
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.
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.
-
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:
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... :) :) :)