C# Socket Programming - Multiple Clients

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it what's up in this video I'm going to write a server that waits for requests and basically the client is going to send a request to the server and the server is going to respond by sending information back to the client and basically we're going to do one function and that's the get time function and so if the client says sends get time to the server the server is going to send the time the current time back to the to the client it's basically all we're going to do serving wise it's the simplest solution I could think of okay and another thing is that the server is going to use a synchronicity well the clients are going to have blocking stuff just because we need the asynchronous Ani for the multiple clients in the server okay so we're going to do time server in a console application so let's start off by defining a method call the method setup server and we should probably define our global socket first we're going to have one socket for the server of course using system dead sockets I'm going to call this server socket and we're going to construct it right away with the typical overload address information internetwork protocol type tcp we have to specify the socket type before we do this I don't know why intellisense did that socket type string okay now let's begin setting up the server so I'm going to use the console window as the log window of course and we're going to have the ellipsis at the end of the our text here to indicate progress or some weird work in progress okay so let's do some work afterwards serversocket we're going to bind the socket to all available local interfaces so let's define our IP endpoint for some reason you can only define or this is the only constructor so we're going to bind on all available local interfaces on port 100s you can do whatever point you want as long as it's the same on the client okay after we bind the server or you can now start listening I'm going to specify a backlog of five the backlog is not important to us just because it's four do you want to worry about the backlog when you're handling multiple clients per second a lot of people say that the backlog is how many clients the server can handle simultaneously but that is not even close to what the backlog is the backlog specifies how many pending connections can exist if there's a bunch of attempts to connect to the server say there's six pending connections then the sixth connection in all the connections after that are going to be refused unless unless the server starts can accept in connections to faster that's what the backlog is it's kind of hard to under understand you don't really need to worry about it we can do one if we want to doesn't matter okay so what do we need to do afterwards we need to start listening for our connections so let's do that so we're socket begin accept and async callback leave that blank for now and I think we can pass in null for the object state I don't think we need an object state so let's define our connection accept callback here it is we'll just call this accept callback has to have ia sync result as a parameter okay let's put that in our begin except for a motor and begin writing in it okay and I'm going to have to define our client list now so let's define a list generic generic list and then sockets and I'm going to call this client sockets it's going to be a list of our sockets our clients to serve okay I have to make these static I forget to do a mark my global variable static every time okay so we're going to call and accept and that's going to give us our accepting connection a socket so we can send and receive information to our clients so our client socket really yeah we're going to pass in our icing results and then we're going to add this socket to our client socket list and we're going to begin accepting again because after it accepts this it's going to stop accepting connections unless we call begin accept so what I do the same thing as we done up here just here and that will allow us to accept more than one connection and whenever we add our socket to our connections we also need to begin receiving data for each client socket so sorry be easier if I do this socket dot begin receive and before getting into that I need I definitely need to define a global buffer so let's do that and the buffer to be sufficiently big it can be 1024 we don't really need too much space just because we're sending in receiving civil messages I'm definitely not going to do receive pass buffer size because that is more complexed in multiple clients for sure okay so let's set up begin receive here we're going to specify our buffer and I'm going to just find the overload that I want so this is the overload the zero for the offset and the received size will be buffer dot size or length socket Flags none blank async callback for now objects ting null actually we're going to pass in the current socket as the object state just so we can easily retrieve it and call and receive okay so let's define our receive callback you okay so let's get our socket so this socket here is this socket so just makes it easy for us we need to check to see if we have on what kind of request we have so let's go to request first we need to get there out how much data we received so and received we also need to call and receive of course so socket and receive pass in our 8i sync results and we're going to since we're not specifying any length parameters in our data being sent we're just going to trim it using how much data we've actually received so we don't have like null bytes in our string so I'm going to create a temporary buffer byte that's going to be called a debuff new byte array and we're going to specify reseed for buffer size and I'm going to use it array dot copy to copy the appropriate amount of bytes into our data buffer so the buffer actually accepted goes to our global buffer when a copier that's going to be a source it's going to go into data buff and how much we want to copy is received and now we can actually write that stuff down low to the console and do whatever so console first let's get the string I'm going to use encoding and get string I'm going to print the text received and after that we're going to check to see if it's a time command and then we're going to send if it is a time command then we're going to send the time asynchronously to the client to the appropriate client of course so let's do that sending sync nope that's not what I wanted begin send and we need to construct our data of course so point rank data is equal to encoding asking get bytes text and it's going to be not texts going to be date/time now too long time string that's the string that we're actually going to send to the appropriate line so begins send data 0 paint it on length socket flags none and erasing callback of course so new and object state I think we can just use null actually we're probably going to have to send the socket in through the object state now let's define our send callback basically all we have to do is end send we're going to get our current socket and send that's all we need to do and I'm going to write a method called San Dena or send text and use that instead of what I'm doing here you you so we can use Syntex to send the date/time otherwise we can send something like invalid string or invalid request okay so I'm just going to write a response here if text is or text to lower not equal to get time then we're going to set response to valid response of our request otherwise we're going to return the time okay and we also need to call begin receive again to start receiving get it I'm just going to copy the begin receive up here okay now we can actually call our methods here so you're just going to mark set observer as static and then everything else is called from it within that I suppose you might want to say when the client connects and that's all we need to do the problem with this is that there's nothing to block the main thread and the other threads are background threads so once the main thread is done executing the console just closes so that's why it's closing immediately you can do this and so that means once you press Enter then the server closes so we do that okay now we can write the time client which is going to be a lot simpler so let's write these the client socket set it up like we did before with address family internetwork socket type stream protocol type TCP and well I'm going to write a method called loop connect basically it's going to try to reconnect continuously until you close the application or until it actually connects so let's begin Clyde socket connect we can use synchronous operations we don't need to do anything else in the application so it's okay see the overload that we want to use I'm going to use IP address and port I'm going to specify the loopback so it will connect to itself and porches 100 I think and we're just going to try this if it doesn't work out if it's a socket exception then we're going to do something I'm going to put this in a while loop while Klein socket is not connected and I'm going to count how many connections there are so I think I'm going to write the attempts down here and when it actually does it connect will just clear the console and write connected and I'm going to do a realign here so it doesn't you know just close randomly so let's see what it does so it's trying to connect and I'm going to click I'm going to clear that so it doesn't do all these different lines that's better so let's start up our server and see how it works out there we go so on the server here it says connected and well the server it says client connected here and in the client it says connected I'm going to change the title so a console title is equal to server okay so now we got our connection loop we can actually have a you know ascend loop for sending data okay well true then let me do this I'm going to clear the console now I'm not going to clear it up right enter a request okay so we're prompting for a request now let's actually get a request so we'll use redline assign it to a string and we'll get that s bytes of course request okay we use the get bytes method and we'll send it so we'll just use our one socket that we created to send client socket send I'm going to send buffer and we will start receiving data as well so we're going to look for a response right away so clients all could receive and we're going to get how much data we were seeing the course and we are going to have to create a new buffer so butter a receive buffer make it sufficiently big and we're going to trim the amount of data we received by using the received integer here we don't really need to prefix anything with the length okay so let's create a new byte array and make it the size of how much we received and then copy the appropriate amount of data from the receive buffer the Banger buffer so I'll write on copy okay in console the right line received there we go okay let's try sending out a request so our client is connected let's enter a request get time and over on the server it says text receive get time and over on the client it says the actual time that we that the server sent to it and then it's prompting for a request again now let's start up another client so we go to clients and I'm going to send a request on the second client I can do this or I can just send an invalid request and the server will send and invalid request string and it should be able to handle a lot of clients so we can take a look at that I'm not going to do any error handling regarding properly closing the clients because that gets a bit crazy so let's actually make this use of a bit more resources so instead of blocking on the send loop using read line I'm basically going to send a predefined request we're actually going to get the time the proper time every time and we're going to going to stop the card thread or sleep it we're about 500 milliseconds so there we go so now the Kleins retrieving time a bit more crazy like and we can start up a couple more instances and it should be okay so I'll put the clients on the bottom actually where's the server oh here it is so then I'm serving a lot of different coins and they're still going if you want to distinguish the clients you're gonna have to look at the handle of the clients or use or send in a client name whenever you send strings around and I'm not going to go over that because that's just going to add a lot of complexity to the application and the concept and that's it see ya
Info
Channel: Brian
Views: 316,408
Rating: 4.9108543 out of 5
Keywords: csharp, c#, visual studio, programming, tutorial, learn, c sharp, how to, .net, socket, client, host, server, tcplient, tcplistener
Id: xgLRe7QV6QI
Channel Id: undefined
Length: 29min 7sec (1747 seconds)
Published: Sat Mar 09 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.