Skip to content

A script pack that can be used to easily create TCP servers and clients.

License

Notifications You must be signed in to change notification settings

scriptcs-contrib/scriptcs-net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scriptcs-net

Net Script Pack

What is it?

A script pack that can be used to create TCP servers. This was originally created for demo purposes, but I will probably continue working on it to make it more usable. Any contributions/feedback about features to implement or API improvements are welcome.

Usage

Install the nuget package by running scriptcs -install ScriptCs.Net.

Creating a TCP Server

var net = Require<Net>();

var server = net.CreateServer(socket =>
{
    Console.WriteLine("New connection");

    socket.On(
        data: bytes => Console.Write(bytes.AsString()),
        close: () => Console.WriteLine("Connection closed"),
        error: e => Console.WriteLine("Error: {0}\r\nStackTrace: {1}", e.Message, e.StackTrace));
});

Console.WriteLine("Listening at 127.0.0.1:8080");

server.Listen(8080, "127.0.0.1").Wait();

Console.WriteLine("Closing server");

server.Close();

Creating a TCP Client

var net = Require<Net>();

var client = net.Connect(8080, "127.0.0.1", onConnect: () => Console.WriteLine("Connected to chat room at 127.0.0.1:8080"));

client.On(
        data: data => Console.WriteLine(data.AsString()),
        close: () => Console.WriteLine("Connection closed"),
        error: e => Console.WriteLine("Error: {0} \r\nStackTrace: {1}", e.Message, e.StackTrace));

var line = Console.ReadLine();

while (line != ":close"){
	client.WriteAsync(line.AsBytes());
	line = Console.ReadLine();
}

// still have to troubleshoot an issue when closing the socket
client.Close();

What's next

  • Add more features, such as error handling.
  • Listen to community feedback.

About

A script pack that can be used to easily create TCP servers and clients.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published