XML
-
Hi I am sending the ip address and the port no for the socket using the following syntax:
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);Because of the connection problem i want to read both the ip address and the port no from an xml file.I do not exactly know how i should be storing data in an xml...In the xml file named p1.xml i have given the following:Please check whether this is right and give me your suggestions <table width="100%" border="0"> <tr> <td><address>127.0.0.1<address> <td> <tr> <tr><td> <port>10001<port> <td><tr> I am using the following code for reading the data from the xml file:
XmlTextreader reader = new XmlTextReader("p1.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text: //Display the text in each element.
// Console.WriteLine(reader.Value);
String s=reader.Value;
}
}My doubt is that will the string variable "s" take values of the ip and port..and how do i fit it into the socket statement...please help me with this
-
Hi I am sending the ip address and the port no for the socket using the following syntax:
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);Because of the connection problem i want to read both the ip address and the port no from an xml file.I do not exactly know how i should be storing data in an xml...In the xml file named p1.xml i have given the following:Please check whether this is right and give me your suggestions <table width="100%" border="0"> <tr> <td><address>127.0.0.1<address> <td> <tr> <tr><td> <port>10001<port> <td><tr> I am using the following code for reading the data from the xml file:
XmlTextreader reader = new XmlTextReader("p1.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text: //Display the text in each element.
// Console.WriteLine(reader.Value);
String s=reader.Value;
}
}My doubt is that will the string variable "s" take values of the ip and port..and how do i fit it into the socket statement...please help me with this
-
Hi I am sending the ip address and the port no for the socket using the following syntax:
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);Because of the connection problem i want to read both the ip address and the port no from an xml file.I do not exactly know how i should be storing data in an xml...In the xml file named p1.xml i have given the following:Please check whether this is right and give me your suggestions <table width="100%" border="0"> <tr> <td><address>127.0.0.1<address> <td> <tr> <tr><td> <port>10001<port> <td><tr> I am using the following code for reading the data from the xml file:
XmlTextreader reader = new XmlTextReader("p1.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text: //Display the text in each element.
// Console.WriteLine(reader.Value);
String s=reader.Value;
}
}My doubt is that will the string variable "s" take values of the ip and port..and how do i fit it into the socket statement...please help me with this
your using HTML... look for an XML primer
-
That is not XML. How about: <endpoint> <ip>127.0.0.1</ip> <port>43211</port> </endpoint> And please use a better way to read the XML..
Last modified: after originally posted -- darned angle brackets
Hi Both the values of port and ip are integers. I am able to read all the text values from the xml I am using
XmlReader re=new XmlReader("p1.xml");
String s=re.Value;this returns all the text values stored in xml.But i want to retrieve both the port and ip and assign to 2 different integer variables.I am not sure of this..Please help me with this
-
That is not XML. How about: <endpoint> <ip>127.0.0.1</ip> <port>43211</port> </endpoint> And please use a better way to read the XML..
Last modified: after originally posted -- darned angle brackets
Hi I tried with the following code.There is no error at the compilation time but something goes wrong at the run time.I do not know how to retrieve the ip and port from the xml..Please help me with this
public static void start_server()
{
XmlTextReader r1 = new XmlTextReader("p1.xml");
XmlDocument doc1 = new XmlDocument();
doc1.Load(r1);
int ipadd = r1.ReadElementContentAsInt();
int ports = r1.ReadElementContentAsInt();
IPEndPoint ipep = new IPEndPoint(ipadd, ports);Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); Console.WriteLine("Waiting for a client..."); while (true) { try { byte\[\] data = new byte\[1024\]; Console.WriteLine("hai"); int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote); Console.WriteLine("gfgjfk"); Console.WriteLine("Message received from {0}:", tmpRemote.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received data = new byte\[1024\]; string ss = "Welcome to the Server"; data = Encoding.ASCII.GetBytes(ss); newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote); Console.WriteLine("\\nSent Acknowledgement"); } catch (SocketException e) { Console.WriteLine(e.Message); } } }
-
Hi I tried with the following code.There is no error at the compilation time but something goes wrong at the run time.I do not know how to retrieve the ip and port from the xml..Please help me with this
public static void start_server()
{
XmlTextReader r1 = new XmlTextReader("p1.xml");
XmlDocument doc1 = new XmlDocument();
doc1.Load(r1);
int ipadd = r1.ReadElementContentAsInt();
int ports = r1.ReadElementContentAsInt();
IPEndPoint ipep = new IPEndPoint(ipadd, ports);Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); Console.WriteLine("Waiting for a client..."); while (true) { try { byte\[\] data = new byte\[1024\]; Console.WriteLine("hai"); int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote); Console.WriteLine("gfgjfk"); Console.WriteLine("Message received from {0}:", tmpRemote.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received data = new byte\[1024\]; string ss = "Welcome to the Server"; data = Encoding.ASCII.GetBytes(ss); newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote); Console.WriteLine("\\nSent Acknowledgement"); } catch (SocketException e) { Console.WriteLine(e.Message); } } }
-
You aren't correctly specifying which element to read. Try the XPath trick I mentioned earlier, it should always work (provided, of course, that your XPath matches your actual XML)
Hi I retrieved port and ip using
for (int i = 0; i < xmlnode.Count; i++) { st = xmlnode\[i\].FirstChild.InnerText; st1 = xmlnode\[i\].LastChild.InnerText; } ipadd = Convert.ToInt32(st); ports = Convert.ToInt32(st1); IPEndPoint ipep = new IPEndPoint(ipadd, ports);//ipadd=127.0.0.1 port=10001
It gives me an exception"Input String was not in a correct format" but the server(.exe) opens up.In the server code i have used
IPEndPoint ipep = new IPEndPoint(ipadd, ports);
to open the connection with the client.For this opened server how should i change the following statement on the client
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
How should i specify the port and ip on the client?i.e for the server opened on port 10001 and ip=127.0.0.1 (obtained from xml) how should i modify the client?Please give me your suggestions
-
Hi I retrieved port and ip using
for (int i = 0; i < xmlnode.Count; i++) { st = xmlnode\[i\].FirstChild.InnerText; st1 = xmlnode\[i\].LastChild.InnerText; } ipadd = Convert.ToInt32(st); ports = Convert.ToInt32(st1); IPEndPoint ipep = new IPEndPoint(ipadd, ports);//ipadd=127.0.0.1 port=10001
It gives me an exception"Input String was not in a correct format" but the server(.exe) opens up.In the server code i have used
IPEndPoint ipep = new IPEndPoint(ipadd, ports);
to open the connection with the client.For this opened server how should i change the following statement on the client
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
How should i specify the port and ip on the client?i.e for the server opened on port 10001 and ip=127.0.0.1 (obtained from xml) how should i modify the client?Please give me your suggestions
Ok debug your code, put a breakpoint on "ipadd = Convert.ToInt32(st);" and watch the value of st and stl. You may be in for a surprise. To solve this problem.. just start using SelectSingle on an XmlDocument. It's at least 10 times as easy to get right. As to specifying the ip/port in the client - the exact same way you did it in the server will do fine.