using System;
using ;
using ;
using ;
using NetCoreServer;
namespace TcpChatServer
{
class ChatSession : TcpSession
{
public ChatSession(TcpServer server) : base(server) {}
protected override void OnConnected()
{
($"Chat TCP session with Id {Id} connected!");
// Send invite message
string message = "Hello from TCP chat! Please send a message or '!' to disconnect the client!";
SendAsync(message);
}
protected override void OnDisconnected()
{
($"Chat TCP session with Id {Id} disconnected!");
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
string message = Encoding.(buffer, (int)offset, (int)size);
("Incoming: " + message);
// Multicast message to all connected sessions
(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Disconnect();
}
protected override void OnError(SocketError error)
{
($"Chat TCP session caught an error with code {error}");
}
}
class ChatServer : TcpServer
{
public ChatServer(IPAddress address, int port) : base(address, port) {}
protected override TcpSession CreateSession() { return new ChatSession(this); }
protected override void OnError(SocketError error)
{
($"Chat TCP server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// TCP server port
int port = 1111;
if ( > 0)
port = (args[0]);
($"TCP server port: {port}");
();
// Create a new TCP chat server
var server = new ChatServer(, port);
// Start the server
("Server starting...");
();
("Done!");
("Press Enter to stop the server or '!' to restart the server...");
// Perform text input
for (;;)
{
string line = ();
if ((line))
break;
// Restart the server
if (line == "!")
{
("Server restarting...");
();
("Done!");
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
(line);
}
// Stop the server
("Server stopping...");
();
("Done!");
}
}
}
using ;
using ;
using ;
using NetCoreServer;
namespace TcpChatServer
{
class ChatSession : TcpSession
{
public ChatSession(TcpServer server) : base(server) {}
protected override void OnConnected()
{
($"Chat TCP session with Id {Id} connected!");
// Send invite message
string message = "Hello from TCP chat! Please send a message or '!' to disconnect the client!";
SendAsync(message);
}
protected override void OnDisconnected()
{
($"Chat TCP session with Id {Id} disconnected!");
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
string message = Encoding.(buffer, (int)offset, (int)size);
("Incoming: " + message);
// Multicast message to all connected sessions
(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Disconnect();
}
protected override void OnError(SocketError error)
{
($"Chat TCP session caught an error with code {error}");
}
}
class ChatServer : TcpServer
{
public ChatServer(IPAddress address, int port) : base(address, port) {}
protected override TcpSession CreateSession() { return new ChatSession(this); }
protected override void OnError(SocketError error)
{
($"Chat TCP server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// TCP server port
int port = 1111;
if ( > 0)
port = (args[0]);
($"TCP server port: {port}");
();
// Create a new TCP chat server
var server = new ChatServer(, port);
// Start the server
("Server starting...");
();
("Done!");
("Press Enter to stop the server or '!' to restart the server...");
// Perform text input
for (;;)
{
string line = ();
if ((line))
break;
// Restart the server
if (line == "!")
{
("Server restarting...");
();
("Done!");
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
(line);
}
// Stop the server
("Server stopping...");
();
("Done!");
}
}
}