|
|
Line 1: |
Line 1: |
− | =Netværks programmer=
| + | Intet at se her |
− | | |
− | '''Opgaver:'''
| |
− | | |
− | | |
− | Opgave1:
| |
− | | |
− | Lav et program, der kan lytte efter en besked.
| |
− | | |
− | | |
− | | |
− | Opgave2:
| |
− | | |
− | Lav et chat program, hvor en server og klient kan sende og modtage beskeder til og fra hinanden.
| |
− | | |
− | | |
− | ===UDP listener:===
| |
− | | |
− | '''Server:'''
| |
− | <source lang="Csharp">
| |
− | UdpClient client = new UdpClient(11000);
| |
− | IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 11000);
| |
− | | |
− | byte[] recivedBytes = client.Receive(ref endpoint);
| |
− | Console.WriteLine("The message was:");
| |
− | string result = Encoding.ASCII.GetString(recivedBytes);
| |
− | Console.WriteLine(result);
| |
− | | |
− | Console.ReadKey();
| |
− | </source>
| |
− | | |
− | | |
− | ===UDP client:===
| |
− | | |
− | '''Server:'''
| |
− | <source lang="Csharp">
| |
− | UdpClient client = new UdpClient();
| |
− | IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
| |
− | byte[] myBytes = Encoding.ASCII.GetBytes("Hello!");
| |
− | client.Send(myBytes, myBytes.Length, endPoint);
| |
− | | |
− | Console.WriteLine("Message has been sent!");
| |
− | Console.ReadKey();
| |
− | </source>
| |
− | | |
− | | |
− | | |
− | ===Threading===
| |
− | | |
− | | |
− | '''Server:'''
| |
− | <source lang="Csharp">
| |
− | static void Main(string[] args)
| |
− | {
| |
− | Console.WriteLine("Start");
| |
− | | |
− | Thread thread = new Thread(myThread);
| |
− | thread.Start();
| |
− | | |
− | string input = Console.ReadLine();
| |
− | Console.WriteLine(input);
| |
− | Console.ReadKey();
| |
− | }
| |
− | | |
− | static void myThread()
| |
− | {
| |
− | for (int i = 0; i < 10; i++)
| |
− | {
| |
− | Console.WriteLine(i);
| |
− | }
| |
− | }
| |
− | </source>
| |
− | | |
− | | |
− | | |
− | =Onsdag=
| |
− | '''Object to XML and Back'''
| |
− | | |
− | Opgave 1:
| |
− | | |
− | Lav et program der kan konvertere et objekt til XML, og sende det igennem UDP. Når UDP pakkerne når frem, skal de deserialiseres tilbage til et objekt igen.
| |
− | | |
− | | |
− | '''Program:'''
| |
− | <source lang="Csharp">
| |
− | Human joshua = new Human();
| |
− | XmlSerializer serializer = new XmlSerializer(typeof(Human));
| |
− | StringWriter sWriter = new StringWriter();
| |
− | | |
− | serializer.Serialize(sWriter, joshua);
| |
− | | |
− | string myXML = sWriter.ToString();
| |
− | Console.WriteLine(myXML);
| |
− | | |
− | Console.ReadKey();
| |
− | | |
− | StringReader sReader = new StringReader(myXML);
| |
− | | |
− | Human mads = (Human)serializer.Deserialize(sReader);
| |
− | | |
− | Console.WriteLine("This is the player Mads:");
| |
− | Console.WriteLine(mads.money);
| |
− | Console.WriteLine(mads.upgrades);
| |
− | Console.ReadKey();
| |
− | </source>
| |
− | | |
− | | |
− | '''Human:'''
| |
− | <source lang="Csharp">
| |
− | public class Human
| |
− | {
| |
− | private float myWeight;
| |
− | | |
− | public float MyWeight
| |
− | {
| |
− | get {
| |
− | Console.WriteLine("Half-Life 3.. Worth the weight!");
| |
− | return myWeight;
| |
− | }
| |
− | set { myWeight = value; }
| |
− | }
| |
− | | |
− | | |
− | public string name = "Joshua";
| |
− | public float money = 2000.0f;
| |
− | public int upgrades = 0;
| |
− | }
| |
− | </source>
| |
− | | |
− | Link til Simon:
| |
− | http://stackoverflow.com/questions/4996876/generic-deserialization-of-an-xml-string
| |