Home - lishu/Jock.Net.TcpJson GitHub Wiki
Welcome to the Jock.Net.TcpJson wiki!
Your need known about TcpListener
and TcpClient
, Two Main class TcpJsonServer
and TcpJsonClient
is all about it.
- Planing what object your need transmission by TcpJson, This means the Server and Client Need know the Object Type in any loaded assembly. So if your Server and Client is not in one project, your need create a project just for shard all Type in transmission.
-
TcpJsonServer
andTcpJsonClient
all use inner thread, MUST canStart()
after config done. IfTcpJsonClient
is created byTcpJsonServer
, will auto start after Connected event.
// Create a new TcpJsonClient ready connect to a remote port.
var client = new TcpJsonClient(new System.Net.IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 8013));
// Use all `OnReceive` methods receive remote data
// In This start, we no need receive anything from server
// Start client
client.Start();
// Send a string object
client.SendObject("Hello world", ()=>{
// Send Done, just Stop client
client.Stop();
});
Start Server
// Create a new TcpJsonServer ready Listener a local Tcp port
var server = new TcpJsonServer(new System.Net.IPEndPoint(IPAddress.Any, 8013));
// Add Connected event handler so you can get a TcpJsonClient for new connecting client
server.Connected += Server_Connected;
// Start server, this will start listener and core loop
server.Start();
Server_Connected
// In Connected Event Handler, get TcpJsonClient by `e.ServerClient`
// Use all `OnReceive` methods receive remote data
// This demo is use simple string type object data, but your can try more
e.ServerClient
.OnReceive<string>((str,c)=>{
// str is the string remote sended
// c is the TcpJsonClient instance you can call and `Send` method send data back
Console.WriteLine($"Client say:{str}");
});