Results 1 to 6 of 6

Thread: Simple client & admin -> server -> database

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    1

    Simple client & admin -> server -> database

    Hello

    I want to develop a point of sale solution. My idea is to have a client application (vb.net forms windows application which will be the touch screen point of sale app) and an admin application (forms again, used for adding products, prices, stock taking etc.). Both these communicate on the local network with a third windows forms application which will run on a second machine and be the "server". The server contains all the business logic, data manipulation etc. and communicates with the SQL database.

    I know the admin and POS app could communicate directly with the SQL server, but I want the server app in place to do the business logic, logging, tracking, etc. and be a windows forms app for monitoring current connections etc. and be able to be run on a dedicated machine. Keeping the POS and admin applications separate also keeps things simple to manage for the end user - who would probably have a central machine in an office for admin work but multiple POS machines on the shop floor.

    The server app should allow multiple connections and threaded operation.

    I've Googled the hell out of this and can't find any clear examples. I've found loads of examples of simple "chat" client server applications but nothing that shows me how to implement database communication and, more to the point, how to send that data back to the client. I can also find loads of examples of remote communication with a SQL server. My idea is that the POS client would simply send "commands" to the server application (example: GetAllProducts) and the server queries the database and replies with a list of all products to be displayed on the screen of the POS.

    The admin app would naturally have more control (example: CreateProduct). All the "commands" would be defined and stored on the server app.

    I have reasonable knowledge when it comes to simple applications and can hold my own when coding but this architecture is confusing the hell out of me.

    Any good starting points, tutorials, or examples to download? Anyone got a simple working example? Or would anyone be so kind as to rustle something up? - just showing the basics of course!

    Many thanks for anything you can provide!

    NH

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Simple client & admin -> server -> database

    Quote Originally Posted by nhwebforge View Post
    I've found loads of examples of simple "chat" client server applications but nothing that shows me how to implement database communication and, more to the point, how to send that data back to the client.
    It doesn't matter. Data is data. It's all sent as bytes anyway. If you're sending text then it's exactly the same as a chat app anyway. If you're sending something other than text then you need to serialise it to a Byte array and then send that. That means that the only piece of the puzzle you don't actually have already is serialising and deserialising binary data.

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Simple client & admin -> server -> database

    It sounds like your question is really about architecture and which technologies/techniques to use rather than how to use them. You'll work out the "how" yourself. Is that right? If so, here are my thoughts:-


    1. You're really talking about a service based architecture.

    2. Your Server would expose the various services your client application and your admin applications need. Note, this would not normally be a windows application, it would be a service application and would have no UI at all. There's a few different techs you can use for this. I'd probably suggest a WCF (Windows Communication Foundation) service application because Visual Studio will do a lot of the work for you. You can configure WCF projects to use a variety of protocols but you're probably going to use REST if you want a stripped down, bare bones approach or SOAP if you want stuff like security bundled in - there's pros and cons to either so do so some reading before you make a choice. WEB API seems to be "in" at the moment so maybe take a look at that but I don't know enough about it to comment. You can also step away from WCF completely and use proprietary techs. I've used Thrift in the past and wasn't blown away but I know other developers who swear by it.

    3. Your client and admin applications could then take any form you want. You've suggested Windows which is fine but you'd also have the option of Web or Mobile. They would consume the services your server application exposes. If you're gone with WCF this is simply a matter of adding a web reference to your project. If you've gone with a proprietary tech it might be a little more involved but not too onerous.

    4. As I said in 1. you wouldn't normally build a UI into the service project. Instead, if you want to monitor connections etc. that would be an app in it's own right, similar to your admin and client apps. It would consume services exposed by the service project (which would, of course, need to be extended to provide the necessary services). It's likely (though not necessary) that it would sit on the same machine as the services but that's neither here nor there. It's logically, if not necessarily physically, separated.

    5. You mentioned "send"ing data back to the clients. That implies you want to push data from the server. That's not really what you do. Instead you would pull it from the client. Ie, the client calls a service that returns data. the communication is initiated by the client and the server simply responds. All this is build into the standard service architecture. You can push from the server but that complicates things and you're unlikely to need to for this project.

    So to wrap up, my suggestion would be to google for a Windows Communication Foundation tutorial and run through it. It will probably run you through a straight forward single client - server application but don't worry, multi-connection, multi-client support is built in. I'm not sure you truly need multi-threading for this as IIS will essentially give you a "thread" per client (I'm miss-using the word there for simplicity). If you do want multi threading that's a whole 'nother subject but just google for multi threading in .net and you'll find plenty.

    Good luck and welcome to the forums.
    Last edited by FunkyDexter; Jun 13th, 2018 at 04:25 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  4. #4
    Lively Member
    Join Date
    Jun 2018
    Posts
    80

    Re: Simple client & admin -> server -> database

    Since you really seem to be looking for an example here is the base of a server/client app i had to do for work.

    PS: I don't usually comment my code but i tried to make this as simple as possible.

    Client:

    Code:
    Imports System.Net
    Imports System.Net.Sockets
    
    Public Class Form1
    
        'Variables for connexion port and IP
        Dim TextToSend As String = "I Sent This Text."
        Dim Port As Integer = "8180"
        Dim ServerIp As String = "192.168.1.15"
    
        Sub Main()
    
            'Create socket and Endpoint
            Dim ClientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            Dim ClientIPEndpoint As IPEndPoint = New IPEndPoint(IPAddress.Parse(ServerIp), Port)
    
            Try
                'Attempts to connect to server (Endpoint)
                ClientSocket.Connect(ClientIPEndpoint)
                ConnexionSub(ClientSocket)
            Catch ex As Exception
                'If the connexion can't be made this section of code is executed
                Console.WriteLine("Connexion error: " & ex.ToString)
            End Try
    
        End Sub
    
        Sub ConnexionSub(ByVal SendingSocket As Socket)
    
            Try
                'Transforms text to bytes (same concept for images or any other data) and sends bytes
                Dim BufferBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextToSend)
                SendingSocket.Send(BufferBytes)
            Catch ex As Exception
                'If the message can't be sent but Server and Client are connected this section of code is executed
                Console.WriteLine("Error when sending message: " & ex.ToString)
            End Try
    
        End Sub
    
    End Class
    Simply add a "Send" button to your form and use it to call "Main()"

    Server:

    Code:
    Imports System.Net.Sockets
    Imports System.Net
    Imports System.Threading
    
    Public Class Form1
    
        Sub Main()
    
            'Create socket and Endpoint (IP address is set to IPAddress.Any to take any pc as client, you can parse an IP list instead)
            Dim ServerSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            Dim ServerIPEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 8180) 'Must be on the same port 
    
            'Links socket with IP (in this case any) then listen for X amount of devices
            ServerSocket.Bind(ServerIPEndPoint)
            ServerSocket.Listen(1) 'Change this value to allow more than 1 devices
    
            While True
                'Loop waiting for connexions and calls ConnexionAccepted Sub
                Dim ConnectionSocket As Socket = ServerSocket.Accept()
                ConnexionAccepted(ConnectionSocket)
    
            End While
    
        End Sub
    
        Sub ConnexionAccepted(ByVal ReceptionSocket As Socket)
    
            'Create buffer to store data (watch out for empty bytes after your data, an EndOfFile tag or character might be a good thing)
            Dim BufferBytes(255) As Byte
    
            Try
                'Receives bytes and converts back to ASCII (or other data)
                ReceptionSocket.Receive(BufferBytes)
                Dim ReceivedInfos As String = (System.Text.Encoding.ASCII.GetString(BufferBytes))
                'ReceivedInfos could be a public variable to be used in other subs if you don't use data instantly
                MsgBox(ReceivedInfos.Trim)
            Catch ex As Exception
                'In case of reception error this section of code is executed
                Console.WriteLine("Reception failed: " & ex.ToString)
            End Try
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Creates and starts a different thread so the server can accept connexion and still execute other subs
            Dim ListenerThread As New Thread(New ThreadStart(AddressOf Main))
            ListenerThread.IsBackground = True
            ListenerThread.Start()
        End Sub
    
    End Class
    In this example the Client sends data and the Server receives it. Doing it both way from there is rather simple you should figure it out.

    I also used basic sockets while you could use "TCPClient" and "TCPListener" which are much easier (basically read/write to a stream).
    TCP Client and Listener both being simplified sockets I rather use basic sockets since they offer much more in the long run.

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    I create a new thread on the Server program to handle connexion requests and still execute code from the main thread (Instead of "freezing" until connection is made).
    You might want to check out "syncronous vs asyncronous" and "threading" before rushing into the coding.

    https://www.bisque.com/products/orch..._Execution.htm
    https://docs.microsoft.com/en-us/dot...pts/threading/

    I hope this answer's you and I didn't confuse you too much.

    Good luck to you,

    KBConsole
    Last edited by KBConsole; Jun 13th, 2018 at 06:04 PM.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Simple client & admin -> server -> database

    Quote Originally Posted by KBConsole View Post
    Since you really seem to be looking for an example here is the base of a server/client app i had to do for work.

    PS: I don't usually comment my code but i tried to make this as simple as possible.Client:

    I hope this answer's you and I didn't confuse you too much.

    Good luck to you,

    KBConsole
    You should be programming with strict on. You declare port as a string when it's an integer value. You also leave variables/methods with default instances. Dim would have a different modifier value depending on where it is declared. In a structure, for example, would be Public.

    John has a nice example written a while ago you might find interesting

    http://www.vbforums.com/showthread.p...ent&highlight=

  6. #6
    Lively Member
    Join Date
    Jun 2018
    Posts
    80

    Re: Simple client & admin -> server -> database

    You should be programming with strict on. You declare port as a string when it's an integer value.
    Thanks, quite right about that, will edit.

    You also leave variables/methods with default instances.
    I really don't see the problem here ?

    Dim would have a different modifier value depending on where it is declared. In a structure, for example, would be Public.
    I knew that i guess my comment was confusing, edited as well.

    Thanks mate

    KBConsole

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width