Code Monkey home page Code Monkey logo

simpletcp's Introduction

SimpleTCP

Straightforward and incredibly useful .NET library to handle the repetitive tasks of spinning up and working with TCP sockets (client and server).

NuGet Package: https://www.nuget.org/packages/SimpleTCP/

Build Status

Want a TCP server that listens on port 8910 on all the IP addresses on the machine?

var server = new SimpleTcpServer().Start(8910);

Want a TCP client that connects to 127.0.0.1 on port 8910?

var client = new SimpleTcpClient().Connect("127.0.0.1", 8910);

Want to send "Hello world!" to the server and get the reply that it sends within 3 seconds?

var replyMsg = client.WriteLineAndGetReply("Hello world!", TimeSpan.FromSeconds(3));

Want to receive a message event on the server each time you see a newline \n (char 13), and echo back any messages that come in?

server.Delimiter = 0x13;
server.DelimiterDataReceived += (sender, msg) => {
                msg.ReplyLine("You said: " + msg.MessageString);
            };

Want to know how many clients are connected to the server?

int clientsConnected = server.ConnectedClientsCount;

Want to change the text encoding that the client and server uses when sending and receiving strings? (The default is ASCII/UTF8.)

server.StringEncoder = System.Text.ASCIIEncoding.ASCII;
client.StringEncoder = System.Text.ASCIIEncoding.ASCII;

Want to get the IP addresses that the server is listening on?

var listeningIps = server.GetListeningIPs();

Want to get only the IPv4 addresses the server is listening on?

var listeningV4Ips = server.GetListeningIPs().Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);

Want to make your node.js friends stop saying things like "with node I can spin up a web server in only 4 lines of code"?

var server = new SimpleTcpServer().Start(80);
server.DataReceived += (sender, msg) => {
                msg.Reply("Content-Type: text/plain\n\nHello from my web server!"); 
                };

(But really, this library isn't ideal for web server-ing, so don't do that in prod.)

simpletcp's People

Contributors

aquamoth avatar brandonpotter avatar brudo avatar manuel-s avatar oceanairdrop avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.