Difference between revisions of "Programmering IV December"
From Teknologisk videncenter
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | ===Simpel UDP server og klient:=== | |
− | |||
+ | '''Server:''' | ||
+ | <source lang="Csharp"> | ||
//Listens for data. | //Listens for data. | ||
Line 18: | Line 19: | ||
} | } | ||
+ | </source> | ||
Line 23: | Line 25: | ||
− | Client: | + | '''Client:''' |
+ | <source lang="Csharp"> | ||
//Sends 100 packages | //Sends 100 packages | ||
Line 36: | Line 39: | ||
} | } | ||
+ | </source> | ||
==Server/Client program== | ==Server/Client program== | ||
+ | |||
+ | '''Application1:''' | ||
+ | <source lang="Csharp"> | ||
static void Main(string[] args) | static void Main(string[] args) | ||
Line 71: | Line 78: | ||
} | } | ||
+ | </source> | ||
− | + | '''Application2:''' | |
− | + | <source lang="Csharp"> | |
− | |||
− | |||
− | |||
− | |||
static void Main(string[] args) | static void Main(string[] args) | ||
Line 110: | Line 114: | ||
} | } | ||
} | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===XML converter:=== | ||
+ | <source lang="Csharp"> | ||
+ | |||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | Human player1 = new Human(); | ||
+ | player1.name = "Mads"; | ||
+ | player1.money = 20; | ||
+ | player1.silo = 0; | ||
+ | |||
+ | //Serializeses player1 to XML: | ||
+ | XmlSerializer serializer = new XmlSerializer(typeof(Human)); | ||
+ | StringWriter sWriter = new StringWriter(); | ||
+ | |||
+ | serializer.Serialize(sWriter, player1); | ||
+ | |||
+ | string myXML = sWriter.ToString(); | ||
+ | |||
+ | //File.WriteAllText(@"C:\Users\MaKN\Desktop\XMLClass.txt", myXML); | ||
+ | |||
+ | |||
+ | string readXML = File.ReadAllText(@"C:\Users\MaKN\Desktop\XMLClass.txt"); | ||
+ | |||
+ | //Serializeses XML to an object: | ||
+ | StringReader sReader = new StringReader(readXML); | ||
+ | Human player2 = (Human)serializer.Deserialize(sReader); | ||
+ | |||
+ | Console.WriteLine("Amount of Money:"); | ||
+ | Console.WriteLine(player2.money); | ||
+ | Console.ReadKey(); | ||
+ | |||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | |||
+ | Link til eksamensopgave: | ||
+ | https://drive.google.com/file/d/0B5oDizcmFQ1YT0E0c3ZIWmNqcDA/view?usp=sharing |
Latest revision as of 07:59, 12 December 2016
Simpel UDP server og klient:
Server:
//Listens for data.
static void Main(string[] args)
{
UdpClient client = new UdpClient(11000);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 11000);
byte[] recivedBytes = client.Receive(ref endpoint);
string result = Encoding.ASCII.GetString(recivedBytes);
Console.WriteLine(result);
Console.ReadLine();
}
Client:
//Sends 100 packages
static void Main(string[] args)
{
UdpClient client = new UdpClient();
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
byte[] messageBytes = Encoding.ASCII.GetBytes("Hello!");
client.Send(messageBytes, messageBytes.Length, endpoint);
}
Server/Client program
Application1:
static void Main(string[] args)
{
Thread thread = new Thread(Listener);
thread.Start();
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11005);
UdpClient client = new UdpClient();
while (true)
{
Console.WriteLine("Write something:");
string input = Console.ReadLine();
byte[] messageBytes = Encoding.ASCII.GetBytes(input);
client.Send(messageBytes, messageBytes.Length, endPoint);
}
}
static void Listener()
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 11010);
UdpClient client = new UdpClient(endpoint);
while (true)
{
byte[] recivedBytes = client.Receive(ref endpoint);
string result = Encoding.ASCII.GetString(recivedBytes);
Console.WriteLine(result);
}
}
Application2:
static void Main(string[] args)
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11010);
UdpClient client = new UdpClient();
Thread thread = new Thread(Listener);
thread.Start();
while (true)
{
string input = Console.ReadLine();
byte[] messageBytes = Encoding.ASCII.GetBytes(input);
client.Send(messageBytes, messageBytes.Length, endpoint);
}
}
static void Listener()
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 11005);
UdpClient client = new UdpClient(endpoint);
while (true)
{
byte[] recivedBytes = client.Receive(ref endpoint);
string result = Encoding.ASCII.GetString(recivedBytes);
Console.WriteLine(result);
}
}
XML converter:
static void Main(string[] args)
{
Human player1 = new Human();
player1.name = "Mads";
player1.money = 20;
player1.silo = 0;
//Serializeses player1 to XML:
XmlSerializer serializer = new XmlSerializer(typeof(Human));
StringWriter sWriter = new StringWriter();
serializer.Serialize(sWriter, player1);
string myXML = sWriter.ToString();
//File.WriteAllText(@"C:\Users\MaKN\Desktop\XMLClass.txt", myXML);
string readXML = File.ReadAllText(@"C:\Users\MaKN\Desktop\XMLClass.txt");
//Serializeses XML to an object:
StringReader sReader = new StringReader(readXML);
Human player2 = (Human)serializer.Deserialize(sReader);
Console.WriteLine("Amount of Money:");
Console.WriteLine(player2.money);
Console.ReadKey();
}
Link til eksamensopgave:
https://drive.google.com/file/d/0B5oDizcmFQ1YT0E0c3ZIWmNqcDA/view?usp=sharing