/* Decode */ public static request_t Decode_request(request_t request) { /* ntohs */ byte[] bytes = BitConverter.GetBytes(request.port); Array.Reverse(bytes); request.port = BitConverter.ToUInt16(bytes, 0); return(request); }
/* Decode */ public request_t Decode_request(request_t request) { /* ntohs */ long converted_port = request.port; converted_port = IPAddress.NetworkToHostOrder(converted_port); request.port = (uint)converted_port; return(request); }
/* Encode */ public request_t Encode_request(request_t request) { /* htons */ long converted_port = request.port; converted_port = IPAddress.HostToNetworkOrder(converted_port); request.port = (uint)converted_port; return(request); }
public static Int32 GetPort() { const int NS_PORT = 50050; const string NS_IP = "unix.cset.oit.edu"; const string SERVICE_NAME = "ftp_cal"; UdpClient ns_client = new UdpClient(NS_IP, NS_PORT); request_t port_request = new request_t(0, LOOKUP_PORT, SUCCESS, SERVICE_NAME.ToCharArray()); int request_size = 54; port_request = Encode_request(port_request); byte[] converted_request = port_request.ToArray(); ns_client.Send(converted_request, request_size); IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); /* Blocks until a message returns on this socket from a remote host. */ Byte[] receiveBytes = ns_client.Receive(ref RemoteIpEndPoint); request_t ns_response = request_t.FromArray(receiveBytes); ns_response = Decode_request(ns_response); Console.WriteLine("Contacting nameserver..."); Console.WriteLine("looking up service name: " + SERVICE_NAME); if (ns_response.port != 0) { Console.WriteLine("on port: " + ns_response.port + "\n"); } ns_client.Close(); return(ns_response.port); }