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.
@@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.
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.
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?
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?
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.
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.
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.
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.
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.
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.
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
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.
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?
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
... 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 !!
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
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.
@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.
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.
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.
+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?
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!
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.
@@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.
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.
@@JamesKulwicki github.com/pulpfreepress/WinFormServer
Jim, did you ever get it to work?
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?
Very simple and very helpful, and it works great! I cant wait to go through the book! Thankyou!!
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?
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.
If i want to add a video chat feature to this would there be an additional step
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.
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.
Hi Sarah, I'll do a video on that topic and post it later today.
I've completed the code and will post the video Saturday morning...
thanks alot
Sarah Javed i need help coding can you help me please
@@PulpFreePress very nice of you to do that. Very great video. Ive enjoyed it. I will look into purchasing your book
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.
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.
Fine, thanks Pulp, greetings from Argentina :)
Can we use that among two computers(Client and Server) that are in the same network?
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.
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
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.
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?
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.
Book name? plz
C# For Artists: The Art, Philosophy, And Science of Object-Oriented Programming
@@PulpFreePress how to get this book bro?
@@khilafat_pak pulpfreepress.com
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
... 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 !!
what about doing it with hex
Andre Els do you know how to code i need help
How to do it in windows form application?
Which part, the client or the server?
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
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.
@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.
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.
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
How do you compile in CMD? Can anyone tell me?
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.
thank you so much sir for lovely Examples...! :)
Thanks Yash. I'm happy you found it helpful!
Than you for the awesome lesson, bro!
I'm happy you found it helpful!
Is it possible to send to all?
Crni03 Yes. You would need to maintain a collection of incoming client connections
No Results Found on the code download link , please help i need the code, thanks
just go to pulpfreepress.com Website is being updated so old links no longer work. Haven't gotten around to updating the videos.
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.
Thanks!
nice video,
can explain how the server send message to client?
+Khan Javed it happened here: writer.WriteLine("FromServer -> " + s);Or do you need a more specific answer?
Who else got sent here by walsh
Omg YoruMaru famous youtuber im so honored to meet you
Thank you so much for this tutorial!
+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?
thank you very much very helpful