C# Network Programming: Create a Multithreaded Client-Server Application

Поділитися
Вставка
  • Опубліковано 23 гру 2024

КОМЕНТАРІ • 63

  • @JamesKulwicki
    @JamesKulwicki 4 роки тому +1

    Great Tutorial! How would I send to a specific socket while it's connected? Do I need to make a dictionary of all connected clients? Thanks you!

    • @PulpFreePress
      @PulpFreePress  4 роки тому +1

      Hi Jim, if you're talking from the server to connected clients, yes, you'd need some type of collection. Since in this example client/server communication is handled in a separate thread, you'd need a shared reference (one both the server and process handling the client connection have access to) and add each client to it as they connect to the server. A dictionary will work fine if you need to access each connected client by some unique key.

    • @JamesKulwicki
      @JamesKulwicki 4 роки тому

      @@PulpFreePress Thanks for the quick reply! I have searched, and searched and searched some more. I have taken your code from Chapter 19, and added a Dictionary, as well as added the client object and introduced a incrementing ClientID but I still dont' understand how to directly send to a particular client.

    • @PulpFreePress
      @PulpFreePress  4 роки тому +1

      Jim Kulwicki if you’re using the multithreaded client/server code as a starting point, at the moment a client connects, a TcpClient object is created and passed to another thread for processing. From there, the client program usually sends something to the server and the server responds. To have the server send something to the client, the client program needs to be ready to receive incoming data, not just send data and wait for a response, as the client is currently written. So, you need to modify both the server and client side code. I have a video how to do this on UA-cam already. Look for Bi-directional Client Server. I will post the code to GitHub and send you a link.

    • @PulpFreePress
      @PulpFreePress  4 роки тому +1

      @@JamesKulwicki github.com/pulpfreepress/WinFormServer

    • @PulpFreePress
      @PulpFreePress  4 роки тому +1

      Jim, did you ever get it to work?

  • @loicbest5712
    @loicbest5712 8 років тому

    thank you very much for the tutorial.
    in my case it is not compiling, it doesn't recognise "csc *.cs"
    it says "csc" is not recognised as a internal or external command, operable program or bath file.
    may it be the difference of Microsoft window version?

  • @jsmythib
    @jsmythib 9 років тому +3

    Very simple and very helpful, and it works great! I cant wait to go through the book! Thankyou!!

  • @binghyong-baebang2236
    @binghyong-baebang2236 6 років тому

    hi, loved your video. I need to do exactly what you wrote, but I need to receive and transmit byte arrays instead of strings. Do you knoow how to do that?

    • @PulpFreePress
      @PulpFreePress  6 років тому

      Hi Bing, I'm posting a set of client-server videos. When I finish with them, I'll do one on transmitting byte arrays. Look for it in the next week or two.

  • @davidwilliams8860
    @davidwilliams8860 5 років тому

    If i want to add a video chat feature to this would there be an additional step

    • @PulpFreePress
      @PulpFreePress  5 років тому

      Hi David, I'm sure there'd be quite a few additional steps. This simple example doesn't even come close. I'll have to put some thought into how to demonstrate some fundamental video chat approaches.

  • @sarahjaved7185
    @sarahjaved7185 6 років тому

    it is a great video. i need to know if we can translate this whole program for server in a winform application?
    i need to creat a server which keeps sending it's client a particular command every 5 sec and receives a response.
    in winform server halts if we dont use threads.

    • @PulpFreePress
      @PulpFreePress  6 років тому +1

      Hi Sarah, I'll do a video on that topic and post it later today.

    • @PulpFreePress
      @PulpFreePress  6 років тому +2

      I've completed the code and will post the video Saturday morning...

    • @sarahjaved7185
      @sarahjaved7185 6 років тому +1

      thanks alot

    • @taysblazedclutches5101
      @taysblazedclutches5101 6 років тому +1

      Sarah Javed i need help coding can you help me please

    • @cartermanley1544
      @cartermanley1544 5 років тому

      @@PulpFreePress very nice of you to do that. Very great video. Ive enjoyed it. I will look into purchasing your book

  • @lborealtv
    @lborealtv 7 років тому +3

    I dont understand because you use this:
    StreamReader myStreamReader = new StreamReader(client.GetStream()); // I can understand it because the reader receive a Stream from Client.GetStream()
    But in the StreamWriter?? Why i should receive a Stream if i am using a Writer??
    Sorry for my english, grettings from Argentina.

    • @PulpFreePress
      @PulpFreePress  7 років тому +1

      These are StreamReader and StreamWriter objects. They write to a Stream so they need to call the TCPClient.GetStream() method to access the underlying network stream. Note that using these classes hides a lot of complexity of using Sockets directly. Yes, you're using a "Writer", but the writer is sending text across the network as a stream of bytes. You can reduce everything sent over the network to a stream of bytes at the most fundamental level.

    • @lborealtv
      @lborealtv 7 років тому

      Fine, thanks Pulp, greetings from Argentina :)

  • @dennzii
    @dennzii 6 років тому

    Can we use that among two computers(Client and Server) that are in the same network?

    • @PulpFreePress
      @PulpFreePress  6 років тому

      Hi Deniz, Yes, you can. As I recall, in this particular example the Server class is programmed to listen on any IP address and port 5000. You would probably want to add the ability to specify the port via either a configuration file or UI. The Client class is hard coded to connect to 127.0.0.1 (the localhost IP), and port 5000. You would need to modify the client class to accept an IP address and port from the user so that it would be able to connect to any computer on the network running the Server application.

    • @dennzii
      @dennzii 6 років тому

      Thank you so much, but i couldn't understand this part "You would probably want to add the ability to specify the port via either a configuration file or UI." could you explain that again? :) I am new on theese network subjects

    • @PulpFreePress
      @PulpFreePress  6 років тому

      No problem, I remember when I was trying to figure this stuff out...it's not easy. But keep at it. So, if you watch the video, the server runs in a console window without any user interaction. So all the settings are hard-coded in the program, like what port it should listen on. In order to allow the server to listen on a specified port, you'll either what to add a User Interface (a window) that would allow a user to set the port before actually starting the TcpListener, or use a properties file and have the server code read the properties when it starts up. --- The client does have a little window but the IP address and port are hard-coded into the program. If you intend to run the Server application on a remote computer, you're going to want to set the IP address and port before the Client application starts up or after it starts up but before it attempts to connect to the server.

    • @dennzii
      @dennzii 6 років тому

      Thank you for your help, One more and the last question , you are not have to answer that question :), Probably we cannot communicate among two different computers that are conneted to different networks by using that program, as i search, it causes from port forwarding. How can fix it?

    • @PulpFreePress
      @PulpFreePress  6 років тому

      That should work, too, as long as the Server is running on a computer you control on a free port the firewall is allowing to connect.

  • @khilafat_pak
    @khilafat_pak 4 роки тому

    Book name? plz

    • @PulpFreePress
      @PulpFreePress  4 роки тому

      C# For Artists: The Art, Philosophy, And Science of Object-Oriented Programming

    • @khilafat_pak
      @khilafat_pak 4 роки тому

      @@PulpFreePress how to get this book bro?

    • @PulpFreePress
      @PulpFreePress  4 роки тому

      @@khilafat_pak pulpfreepress.com

  • @andreels
    @andreels 8 років тому

    thanks for these videos. C# novice here, just starting out. i am looking to create a socket server that listens on a specific TCP port, then echos or translates everything received on this tcp connection to the com ports on the same PC and vice versa, is that possible ? I can find loads to map a tcp port 1:1 to a com port, but I need one tcp port to many com ports ? thanks

    • @andreels
      @andreels 8 років тому

      ... yes, had the 800GS, but now got the S1000XR :) thanks for the tips, i will have a play, just started with Arduino stuff and find it very interesting. now I want to create a little program to send ascii (clear text) to all my arduinos connected to a different PC over the TCP socket and back again from all arduinos. massive learning curve for me. I might need some more help :) thanks again for the reply, ride safe !!

    • @alexanderalegre3133
      @alexanderalegre3133 7 років тому

      what about doing it with hex

    • @taysblazedclutches5101
      @taysblazedclutches5101 6 років тому

      Andre Els do you know how to code i need help

  • @aywon9531
    @aywon9531 6 років тому

    How to do it in windows form application?

    • @PulpFreePress
      @PulpFreePress  6 років тому

      Which part, the client or the server?

    • @aywon9531
      @aywon9531 6 років тому

      The server and the client. I already made client to client on different machines. Setting up ip address and port by user input. Now i need to learn how to make one machine aa server and other machines as client on different machines. Not in one pc only thank you. Im using LAN to connect ny computer and laptop

    • @PulpFreePress
      @PulpFreePress  6 років тому

      OK, for the server, you can implement any GUI you see fit based on your needs, but when setting up the socket use IPAddress.Any so that it listens for incoming connections on all IPs assigned to the machine, and handle incoming client connections via a separate Thread as is shown in this video. Here's a link to some code you may find helpful: warrenworks.com/ITP_136/source_code_samples/NetRat_ClientUpdating/ There are two folders: Server, and Client. IMPORTANT: You will first need to make a change to the Server.cs code and change this line: _listener = new TcpListener(local_address, 5001); to look like this: _listener = new TcpListener(IPAddress.Any, 5001); If you want to listen on a different port, you'll need to change that as well. Next, compile Rat.cs into a .dll and share that dll with the client. Then compile the remaining server code referencing the Rat.dll, and do the same for the Client code. Start the server then start as many clients as you want.

  • @Chiramisudo
    @Chiramisudo 6 років тому

    @Pulp Free Press Thanks for the demo. :)
    How about an example with a client that pools data from multiple servers? Such as a middleware that grabs data from multiple IoT devices in real-time and does something with it.

    • @PulpFreePress
      @PulpFreePress  6 років тому

      Hi Chiramisu. Thank you for the suggestion. It'd be a challenging video from the purely C# perspective because processing IoT streams implies Big Data, and I'd need to throw in some additional frameworks to really do the topic justice. I'll put some thought into it.

    • @PulpFreePress
      @PulpFreePress  6 років тому

      Also, here's a book that gives you some idea as to the scope of the problem: www.amazon.com/Internet-Things-Hands-Approach-Arshdeep/dp/0996025510

  • @douwedevries8725
    @douwedevries8725 7 років тому

    How do you compile in CMD? Can anyone tell me?

    • @PulpFreePress
      @PulpFreePress  7 років тому

      Best if you use two folders: one dedicated to the server code and another dedicated to the client code. Edit the port settings in the client and server code if you need to change the port the apps a connecting to. Compile the server code from the command line like so:
      c:\server_folder csc *.cs
      and in the client folder compile the client code like so:
      c:\client_folder csc *.cs
      Start the server then start the client. I cover the entire process in the book.

  • @yashsejani1070
    @yashsejani1070 5 років тому +3

    thank you so much sir for lovely Examples...! :)

    • @PulpFreePress
      @PulpFreePress  5 років тому +1

      Thanks Yash. I'm happy you found it helpful!

  • @Гипотеза-о2о
    @Гипотеза-о2о 3 роки тому +1

    Than you for the awesome lesson, bro!

  • @Crni03
    @Crni03 7 років тому

    Is it possible to send to all?

    • @PulpFreePress
      @PulpFreePress  7 років тому

      Crni03 Yes. You would need to maintain a collection of incoming client connections

  • @registertools
    @registertools 7 років тому

    No Results Found on the code download link , please help i need the code, thanks

    • @PulpFreePress
      @PulpFreePress  7 років тому +1

      just go to pulpfreepress.com Website is being updated so old links no longer work. Haven't gotten around to updating the videos.

  • @dhavaldave1545
    @dhavaldave1545 7 років тому +1

    Hi, thanks for this video, that helped me a lot. But can you upload video how to connect multiple server to one client?
    Thank you.

  • @fares.abuali
    @fares.abuali 3 роки тому

    Thanks!

  • @KhanJavedhero
    @KhanJavedhero 8 років тому

    nice video,
    can explain how the server send message to client?

    • @MichaelBeck_profile
      @MichaelBeck_profile 8 років тому

      +Khan Javed it happened here: writer.WriteLine("FromServer -> " + s);Or do you need a more specific answer?

  • @maru3497
    @maru3497 3 роки тому +3

    Who else got sent here by walsh

    • @Oakscroll007
      @Oakscroll007 3 роки тому +1

      Omg YoruMaru famous youtuber im so honored to meet you

  • @MichaelBeck_profile
    @MichaelBeck_profile 8 років тому

    Thank you so much for this tutorial!

    • @MichaelBeck_profile
      @MichaelBeck_profile 8 років тому

      +Junq Bond Hi! At the moment I'm struggling a bit with understanding the tcp/ip connection and how the server client communication behaves below the surface. In a current project I have to use delimiters to get the end of the message but I really don't understand how this is accomplished. Strangely enough I can't find any good literature on message framing. Do you have a tipp for me?

  • @LuxTheKarma
    @LuxTheKarma 6 років тому

    thank you very much very helpful