Download source code

Client:

public class MyClient : TcpCommandClient {
    public const int OkResponse = 100;
 
    protected override int GetDefaultPort() {
        return 2110;
    }
 
    protected override int GetResponseCode(string response) {
        if (StringUtils.IsEmpty(response))
            return SOCKET_WAIT_RESPONSE;
 
        if (response.Trim() == ".") {
            return SOCKET_DOT_RESPONSE;
        }
        else {
            return StringUtils.StrToIntDef(response.Trim(), SOCKET_WAIT_RESPONSE);
        }
    }
 
    protected override void CloseSession() {
        SendCommandSync("CLOSE", new int[] { OkResponse });
    }
 
    protected override void OpenSession() {
        WaitResponse(new int[] { OkResponse });
    }
 
    public void Login() {
        SendCommandSync("LOGIN {0}", new int[] { OkResponse }, new object[] { UserName });
    }
 
    public string[] GetLines() {
        SendCommandSync("GETLINES", new int[] { OkResponse });
        WaitMultipleLines(0);
        return Response.ToArray();
    }
 
    public void SendLines(string[] lines) {
        SendCommandSync("SENDLINES", new int[] { OkResponse });
        SendMultipleLines(lines, ".");
        WaitResponse(new int[] { OkResponse });
    }
}

Server:

public class MyServer : TcpCommandServer {
    void HandleLOGIN(CommandConnection connection, TcpCommandParams parameters) {
        SendResponse(connection, parameters.Command, "100");
    }
 
    void HandleCLOSE(CommandConnection connection, TcpCommandParams parameters) {
        try {
            SendResponseAndClose(connection, parameters.Command, "100");
        }
        catch (SocketError) { }
    }
 
    void HandleGETLINES(CommandConnection connection, TcpCommandParams parameters) {
        SendResponse(connection, parameters.Command, "100");
 
        StringCollectionEx lines = new StringCollectionEx();
        lines.Add("This is a first line of data");
        lines.Add("This is a second line of data");
        lines.Add("This is a third line of data");
 
        SendMultipleLines(connection, lines, ".");
    }
 
    void HandleSENDLINES(CommandConnection connection, TcpCommandParams parameters) {
        AcceptMultipleLines(connection, new MyCommandInfo(parameters.Command, new MyCommandHandler(HandleSENDLINESData)));
        SendResponse(connection, parameters.Command, "100");
    }
 
    void HandleSENDLINESData(CommandConnection connection, TcpCommandParams parameters) {
        AcceptCommands(connection);
        OnLinesSent(new MyLinesSentEventArgs(connection, parameters.RawData.ToArray()));
        SendResponse(connection, "SENDLINES", "100");
    }
 
    protected override UserConnection CreateDefaultConnection() {
        return new CommandConnection();
    }
 
    protected override void OnConnectionAccepted(ConnectionAcceptedEventArgs e) {
        base.OnConnectionAccepted(e);
        SendResponse((CommandConnection)e.Connection, String.Empty, "100");
    }
 
    protected override void GetCommands() {
        Commands.Add(new MyCommandInfo("LOGIN", new MyCommandHandler(HandleLOGIN)));
        Commands.Add(new MyCommandInfo("CLOSE", new MyCommandHandler(HandleCLOSE)));
        Commands.Add(new MyCommandInfo("GETLINES", new MyCommandHandler(HandleGETLINES)));
        Commands.Add(new MyCommandInfo("SENDLINES", new MyCommandHandler(HandleSENDLINES)));
    }
}

Article ID: 67, Created On: 7/18/2013, Modified: 7/18/2013