Client
-
Hi, I have used the following code for the Tcp client server communication... When i run the Client.cs, it gives me an error stating: Error....at System.Net.Sockets.TcpClient.Connect..What changes should i do for the ipaddress and port..please help me with this...
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Runtime;
using System.Text;/// /// Summary description for Class1
///
public class Server
{
public Server()
{
//
// TODO: Add constructor logic here
//
}
public static void Main()
{try { IPAddress ipAd = IPAddress.Parse("192.168.1.32"); //use local m/c IP address, and use the same in the client TcpListener myList = new TcpListener(ipAd, 8006); myList.Start(); Console.WriteLine("The server is running at port 8006..."); Console.WriteLine("The local End point is :" + myList.LocalEndpoint); Console.WriteLine("Waiting for a connection....."); Socket s = myList.AcceptSocket(); Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); byte\[\] b = new byte\[100\]; int k = s.Receive(b); Console.WriteLine("Recieved..."); for (int i = 0; i < k; i++) Console.Write(Convert.ToChar(b\[i\])); ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("The string was recieved by the server.")); Console.WriteLine("\\\\nSent Acknowledgement"); s.Close(); myList.Stop(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
}
-
Hi, I have used the following code for the Tcp client server communication... When i run the Client.cs, it gives me an error stating: Error....at System.Net.Sockets.TcpClient.Connect..What changes should i do for the ipaddress and port..please help me with this...
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Runtime;
using System.Text;/// /// Summary description for Class1
///
public class Server
{
public Server()
{
//
// TODO: Add constructor logic here
//
}
public static void Main()
{try { IPAddress ipAd = IPAddress.Parse("192.168.1.32"); //use local m/c IP address, and use the same in the client TcpListener myList = new TcpListener(ipAd, 8006); myList.Start(); Console.WriteLine("The server is running at port 8006..."); Console.WriteLine("The local End point is :" + myList.LocalEndpoint); Console.WriteLine("Waiting for a connection....."); Socket s = myList.AcceptSocket(); Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); byte\[\] b = new byte\[100\]; int k = s.Receive(b); Console.WriteLine("Recieved..."); for (int i = 0; i < k; i++) Console.Write(Convert.ToChar(b\[i\])); ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("The string was recieved by the server.")); Console.WriteLine("\\\\nSent Acknowledgement"); s.Close(); myList.Stop(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
}
Which line throws the exception - and what is the exception?
Regards, Rob Philpott.
-
Which line throws the exception - and what is the exception?
Regards, Rob Philpott.
Hi i am sorry i pasted the server code instead of the client in the previous post.. This is the code for the client the line
tcpclnt.Connect("192.168.0.2",8001);
gives me the error
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;public class clnt {
public static void Main() { try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program Console.WriteLine("Connected"); Console.Write("Enter the string to be transmitted : "); String str=Console.ReadLine(); Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen= new ASCIIEncoding(); byte\[\] ba=asen.GetBytes(str); Console.WriteLine("Transmitting....."); stm.Write(ba,0,ba.Length); byte\[\] bb=new byte\[100\]; int k=stm.Read(bb,0,100); for (int i=0;i<k;i++)> Console.Write(Convert.ToChar(bb\[i\])); tcpclnt.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
}
-
Hi i am sorry i pasted the server code instead of the client in the previous post.. This is the code for the client the line
tcpclnt.Connect("192.168.0.2",8001);
gives me the error
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;public class clnt {
public static void Main() { try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program Console.WriteLine("Connected"); Console.Write("Enter the string to be transmitted : "); String str=Console.ReadLine(); Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen= new ASCIIEncoding(); byte\[\] ba=asen.GetBytes(str); Console.WriteLine("Transmitting....."); stm.Write(ba,0,ba.Length); byte\[\] bb=new byte\[100\]; int k=stm.Read(bb,0,100); for (int i=0;i<k;i++)> Console.Write(Convert.ToChar(bb\[i\])); tcpclnt.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
}
I think the Connect method of TcpClient, or rather the overload you're using requires the Server's hostname, as opposed to its IP as a string. So try
tcpclnt.Connect("YourServerName",8006);
Worked for me ;> Or use a different overload of the Connect method. I need to investigate a bit further into that, cos this stuff is great!
var question = (_2b || !(_2b));
-
I think the Connect method of TcpClient, or rather the overload you're using requires the Server's hostname, as opposed to its IP as a string. So try
tcpclnt.Connect("YourServerName",8006);
Worked for me ;> Or use a different overload of the Connect method. I need to investigate a bit further into that, cos this stuff is great!
var question = (_2b || !(_2b));
-
OK. If you don't know the computer's name, try this:
byte[] zb = { 192, 168, 0, 5 }; //where 192.168.0.5 is the server's IP
System.Net.IPAddress ipa = new System.Net.IPAddress(zb);
System.Net.IPEndPoint Ipep = new System.Net.IPEndPoint(ipa, 8006);
TcpClient tcpCl = new TcpClient();
tcpCl.Connect(Ipep);I just figured you would have known your server's name, that's all. But this works just as well (if not better). ;)
var question = (_2b || !(_2b));
-
Hi i am sorry i pasted the server code instead of the client in the previous post.. This is the code for the client the line
tcpclnt.Connect("192.168.0.2",8001);
gives me the error
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;public class clnt {
public static void Main() { try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program Console.WriteLine("Connected"); Console.Write("Enter the string to be transmitted : "); String str=Console.ReadLine(); Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen= new ASCIIEncoding(); byte\[\] ba=asen.GetBytes(str); Console.WriteLine("Transmitting....."); stm.Write(ba,0,ba.Length); byte\[\] bb=new byte\[100\]; int k=stm.Read(bb,0,100); for (int i=0;i<k;i++)> Console.Write(Convert.ToChar(bb\[i\])); tcpclnt.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
}
Perhaps you could post the actual exception. Often e.Source and e.Message are useful as well as the e.StackTrace. I have no problem using: myClient.Connect("xxx.xxx.xxx.xxx", port); in my current project. I'm using UdpClients but the overloads for .Connect are the same.
--------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler