Socket listening
-
I want to listen ip host with a specified port number. But not local ip address. I've tried to do it using three classes IPEndPoint, Socket, NetworkStream, but I couldn't see any data (in a MessageBox for example). I use: private static string connectSocket(string address, int port) { //address = "10.0.105.4"; //port = 4000; String data = null; try { Byte[] bytes = new Byte[256]; IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(address), port); Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Connect(endPoint); NetworkStream ns = new NetworkStream(socket); Int32 i; while((i = ns.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); } return data } Does anyone know how to solve this?
-
I want to listen ip host with a specified port number. But not local ip address. I've tried to do it using three classes IPEndPoint, Socket, NetworkStream, but I couldn't see any data (in a MessageBox for example). I use: private static string connectSocket(string address, int port) { //address = "10.0.105.4"; //port = 4000; String data = null; try { Byte[] bytes = new Byte[256]; IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(address), port); Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Connect(endPoint); NetworkStream ns = new NetworkStream(socket); Int32 i; while((i = ns.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); } return data } Does anyone know how to solve this?
Err... The line that goes:
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Should you appending? i.e.
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
It is that simple, just adding the
data +=
instead ofdata =
?- Eitsop What we do not understand we do not possess. - Goethe.
-
Err... The line that goes:
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Should you appending? i.e.
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
It is that simple, just adding the
data +=
instead ofdata =
?- Eitsop What we do not understand we do not possess. - Goethe.