Created
May 21, 2021 12:52
-
-
Save valentinbreiz/480e469b957d1385d99772f0a7e93eab to your computer and use it in GitHub Desktop.
Cosmos TcpClient test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Cosmos.HAL; | |
using Cosmos.System.Network; | |
using Cosmos.System.Network.ARP; | |
using Cosmos.System.Network.Config; | |
using Cosmos.System.Network.IPv4; | |
using Cosmos.System.Network.IPv4.TCP; | |
using Cosmos.System.Network.IPv4.UDP; | |
using Cosmos.System.Network.IPv4.UDP.DHCP; | |
using Cosmos.System.Network.IPv4.UDP.DNS; | |
using Cosmos.TestRunner; | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using Sys = Cosmos.System; | |
namespace NetworkTest | |
{ | |
public class Kernel : Sys.Kernel | |
{ | |
protected override void BeforeRun() | |
{ | |
NetworkDevice nic = NetworkDevice.GetDeviceByName("eth0"); //get network device by name | |
//IPConfig.Enable(nic, Address.Parse("10.33.20.54"), new Address(255, 255, 255, 0), Address.Parse("10.33.20.254")); //enable IPv4 configuration | |
IPConfig.Enable(nic, new Address(10, 33, 20, 54), new Address(255, 255, 255, 0), new Address(10, 33, 20, 254)); //enable IPv4 configuration | |
Console.WriteLine("Network config done! IP=" + NetworkConfig.CurrentConfig.Value.IPAddress.ToString()); | |
} | |
protected override void Run() | |
{ | |
try | |
{ | |
var tcpClient = new TcpClient(4242); | |
tcpClient.Connect(new Address(10, 33, 20, 17), 4242); | |
Console.WriteLine("TCP Connected!"); | |
tcpClient.Send(Encoding.ASCII.GetBytes("Hello from Cosmos!")); | |
var ep = new EndPoint(Address.Zero, 4242); | |
while (true) | |
{ | |
var data = Encoding.ASCII.GetString(tcpClient.Receive(ref ep)); | |
Console.WriteLine("Received TCP packet from " + ep.ToString()); | |
Console.WriteLine("data= " + data); | |
if (data.StartsWith("end")) | |
{ | |
break; | |
} | |
else if (data.StartsWith("send")) | |
{ | |
tcpClient.Send(Encoding.ASCII.GetBytes("Hello from Cosmos!")); | |
} | |
} | |
tcpClient.Close(); | |
Console.ReadKey(); | |
Sys.Power.Shutdown(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Exception: " + ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment