Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MS has beautiful examples of Server/Client Socket, they work really well:
https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example
https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-client-socket-example


How could I adapt it to work with WinForms using a TextBox to display data?

How do I call methods StartListening(); and StartClient(); using a button from UI?

When data is received, how do I actively invoke an EventHandler, which would process the message based on a switch structure, and then perform different tasks based on the data received?

Thanks

What I have tried:

I've tried to implement the console code in a WinForms projects without much success.
Posted
Updated 20-Mar-21 16:43pm
v7
Comments
Richard MacCutchan 17-Feb-21 4:28am    
Implementing sockets code in Winforms is exactly the same as in a Console app. The socket code should run in a background thread and pass information back to the Form code via event notifiers.
Member 14827175 17-Feb-21 10:11am    
If the StartListening() method is in another class (public class AsynchronousSocketListener), how should I reference it from the UI thread?
Richard MacCutchan 17-Feb-21 11:08am    
See OriginalGriff's article (link below) which contains the full example.
Member 14827175 17-Feb-21 11:11am    
Thanks I didn't see it yet, I'll take a look

1 solution

Have a look here: Double Clicking a File in Explorer and Adding It to Your App while it's Running[^] - it uses Sockets to communicate between two instances of the same application and pass data from one to the other.
 
Share this answer
 
Comments
Member 14827175 17-Feb-21 11:12am    
Thanks, I'll take a look at it in a while.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900