Socket Connection
-
Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String:
XXX
XXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip side -
Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String:
XXX
XXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip sidemay be the server will not send data back to the client?!? this piece of code is not very helpfull for a good answer. first your XML you want to send is corrupt! take a look at the xml-declaration >. sencond one is that you will read data from the stream without knowing whether data is available or not ^^ use the search function to take a look at articles that work as server/client app..
-
Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String:
XXX
XXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip sideYou are asking for results immediately but results would not be available immediately. You need a read loop to get the number of bytes back you expect from the server.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String:
XXX
XXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip sideYou need to block with networkStream.Read() Otherwise the connection closes before you have the oportunity to read data. ;) See ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system/html/b46663bf-ae16-3bd8-73c3-2d057d0868e3.htm
-
You need to block with networkStream.Read() Otherwise the connection closes before you have the oportunity to read data. ;) See ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system/html/b46663bf-ae16-3bd8-73c3-2d057d0868e3.htm