fredimachado / TcpChat

Basic asynchronous TCP/IP chat app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TCP/IP Chat Application

Basic asynchronous TCP/IP chat app, developed live at https://www.youtube.com/playlist?list=PLIebvSMVr_dehKSoq6vuAW0BGEM6QnDlS

There is no such thing as a "simple" TCP/IP application, but this is pretty close. This repository is a minimal correct implementation.

Minimal Correct Implementation

Each socket connection is two unidirectional streams of bytes. A minimal correct implementation consists of:

  • Message framing, to translate the streams of bytes to streams of messages.
  • Error handling:
    • Always Be Reading - this lets you detect errors as quickly as possible.
    • Asynchronous APIs - so we don't need multiple threads per socket.
    • Close both streams for a connection at the same time - so that both sides can detect errors as long as data is still being transferred.
  • Keepalives, to detect the half-open scenario.

Overview of ChatApi Architecture

  • PipelineSocket - kind of like NetworkStream but using System.IO.Pipelines instead of Stream. PipelineSocket provides two unidirectional pipelines of bytes.
  • ChatConnection:
    • Serializes chat protocol messages to/from bytes. ChatConnection provides two unidirectional channels of messages.
    • Handles keepalives:
      • Periodically sends keepalive messages.
      • Ignores incoming keepalive messages.
    • Handles request/response logic:
      • Has a collection of outstanding requests.
      • Handles response messages by completing their matching request.

What's Missing

  • Security
    • Encryption. This protocol is a plain-text protocol.
    • Server/client authentication.
    • Misbehaving clients/servers. This app assumes correctly formed packets.
    • Denial of service. E.g., really large length prefixes, sending data too slowly, etc.
    • Reflection attacks. E.g., broadcast huge message at a high rate.
  • Higher-level state machine. E.g., when connection is lost, retry connection after some timeout.
    • Consider a Desired State pattern: the user indicates the desired state, and your application performs whatever connection and messages are necessary to achieve that state.

Resources

Tools

About

Basic asynchronous TCP/IP chat app

License:MIT License


Languages

Language:C# 75.7%Language:Lua 24.3%