Can anyone give me a place to read up on these or possibly an outline of one? I need to register an IP and then collect data from the device.

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

I'd look up the ms commands for windows. Such as ping, nbtstat, etc.

You could call them as process. However, there also happens to be inbuilt IP functions in the dotnet framework, although I've never used them.

Do a Google search on c# Server Socket, and/or get the book "TCP/IP Sockets in C#" by David B Makofske, Michael Donahoo and Kenneth Calvert. This book goes into threading the server and client with lots of examples.

Thanks for the good advice. I think that book will be a big help in trying to figure this crap out.... another question, I think the listener might have to be udp. would that be essentially the same thing except using a different function in .net?

Selecting the UDP protocol is very similar to selecting Tcp, Echo, Ftp, etc etc. You will find plenty of examples of using UDP in the book.
Example:

Socket sock = new Socket(AddressFamily.Internetwork, SocketType.Dgram, ProtocolType.Udp);

J

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SimpleUdpClient
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      string input, stringData;
      IPEndPoint ipep = new IPEndPoint(
                      IPAddress.Parse("127.0.0.1"), 9050);

      Socket server = new Socket(AddressFamily.InterNetwork,
                     SocketType.Dgram, ProtocolType.Udp);


      string welcome = "Hello, are you there?";
      data = Encoding.ASCII.GetBytes(welcome);
      server.SendTo(data, data.Length, SocketFlags.None, ipep);

      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
      EndPoint Remote = (EndPoint)sender;

      data = new byte[1024];
      int recv = server.ReceiveFrom(data, ref Remote);

      Console.WriteLine("Message received from {0}:", Remote.ToString());
      Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

      while(true)
      {
         input = Console.ReadLine();
         if (input == "exit")
            break;
         server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
         data = new byte[1024];
         recv = server.ReceiveFrom(data, ref Remote);
         stringData = Encoding.ASCII.GetString(data, 0, recv);
         Console.WriteLine(stringData);
      }
      Console.WriteLine("Stopping client");
      server.Close();
   }
}

my main question is in line 13-14 does that line tell the device what IP to listen for or is that what IP to listen on?

ok I think i may have answered my own question... My next question is, is there a function I can replace line 13-14 with to make the program listen blindly for anything on the open port?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.