Если я закодирую программу сервера WHOIS, скажем, используя такие языки, как C# и Java, какие функции я должен предоставить?
Я понимаю, что серверы WHOIS прослушивают порт №. 43. Когда они получают команду «whois», они отвечают различной информацией, относящейся к самому серверу.
Итак, это обычный сервер TCP / IP, который прослушивает специальный порт и служит для определенной c цели.
Прав ли я?
Клиент Whois:
using System;
using System.Net.Sockets;
using System.IO;
public class Whois
{
static void Main(string[] args)
{
try
{
TcpClient client = new TcpClient();
client.Connect("localHost", 43);
if (args.Length < 1)
{
Console.WriteLine("Provide more than one Args");
}
else if (args.Length == 1)
{
StreamWriter sw = new StreamWriter(client.GetStream());
StreamReader sr = new StreamReader(client.GetStream());
sw.WriteLine(args[0]);
sw.Flush();
Console.WriteLine(args[0] + " is "+ sr.ReadToEnd());
return;
}
else if (args.Length > 1)
{
StreamWriter sw = new StreamWriter(client.GetStream());
StreamReader sr = new StreamReader(client.GetStream());
string str = "";
foreach (string arg in args)
{
str += arg + " ";
}
sw.WriteLine(str);
sw.Flush();
Console.WriteLine(args[0] + " location changed to be " + args[1]);
sw.Close();
return;
}
else Console.WriteLine("Invalid args ");
return;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
Сервер Whois:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
namespace locationserver
{
public class respond
{
static Dictionary<string, string> SavedLocationa = new Dictionary<String, String>();
static void Main(string[] args)
{
runServer();
}
static void runServer()
{
TcpListener listener;
Socket connection;
NetworkStream socketStream;
try
{
listener = new TcpListener(IPAddress.Any, 43);
listener.Start();
Console.WriteLine("server started listening");
while (true)
{
connection = listener.AcceptSocket();
socketStream = new NetworkStream(connection);
Console.WriteLine("Connection Received");
doRequest(socketStream);
socketStream.Close();
connection.Close();
}
}
catch (Exception e)
{
Console.WriteLine("Exception:" + e.ToString());
}
}
static void doRequest(NetworkStream socketStream)
{
try
{
StreamWriter sw = new StreamWriter(socketStream);
StreamReader sr = new StreamReader(socketStream);
String line = sr.ReadLine();
Console.WriteLine("Respond Received:" + line);
String[] sections = line.Split(new char[] { ' ' }, 2);
String names, location;
if (line.Contains("-h0"))
{
Console.WriteLine(" Is ok");
}
else if (line.Contains("-h9"))
{
Console.WriteLine("Hey you're progressing");
}
else if (line.Contains("-h1"))
{
Console.WriteLine("We're Done");
}
if (sections.Length < 1)
{
Console.WriteLine("Too little words was inputted");
}
else if (sections.Length == 1)
{
names = sections[0];
if (SavedLocationa.ContainsKey(names))
{
sw.WriteLine(SavedLocationa[names]);
}
else
{
sw.WriteLine("Error no entries found");
}
sw.Flush();
Console.WriteLine("Error no entries found");
}
else if (sections.Length == 2)
{
names = sections[0];
location = sections[1];
if (SavedLocationa.ContainsKey(names))
{
SavedLocationa.Remove(names);
}
else
{
SavedLocationa.Add(names, location);
sw.WriteLine("Update Successful");
}
sw.Flush();
}
}
catch (Exception e)
{
Console.WriteLine("Something went wrong");
Console.WriteLine(e.Message);
}
}
}
}
Какие функции отсутствуют в серверной части кода?