Тёмный

TCP Socket Tutorial (C#, Visual Studio, Networked Console Apps) 

wolfs cry games
Подписаться 961
Просмотров 57 тыс.
50% 1

Опубликовано:

 

11 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 136   
@NickFromIT
@NickFromIT 4 года назад
sorry for my bad english ….. this is good, but it only works in local IP … have you some examples (Windows Form) about chat for 2 or 3 PC over internet with different connection ? … thanks
@wolfscrygames
@wolfscrygames 4 года назад
Hello, I have some examples but it's hard for me to test right now before I get back to you on this. There are different issues depending on the operating system. Are you working with Windows, Mac, or Linux?
@NickFromIT
@NickFromIT 4 года назад
@@wolfscrygames WIndows (10)
@wolfscrygames
@wolfscrygames 4 года назад
@@NickFromIT have you set up port forwarding on the host computer's network?
@NickFromIT
@NickFromIT 4 года назад
@@wolfscrygames i dont know how ... i need to see or read .... take your time ... I'm in no hurry :-)
@wolfscrygames
@wolfscrygames 4 года назад
@@NickFromIT we've identified the issue. To connect from an external network, you need to host with the local address and connect with the external address. You also need to do port forwarding in most cases because routers block incoming traffic. Instructions are different for each router and internet provider, but the concept is the same. Lastly, verify the host PC is not blocking inbound traffic with firewall. You can either turn off the firewall or set up a rule to allow incoming traffic.
@FlanderDev
@FlanderDev Год назад
Bruh, this Video is pure gold. THANK YOU SO MUCH.
@brodyentertainment6605
@brodyentertainment6605 Год назад
Exactly what i was looking for. Explained it perfectly and still working 3 years later. Thanks!!
@zeph0shade
@zeph0shade 2 года назад
I'm only halfway through it so far but already wanna say I appreciate this video immensely. As someone who has ADHD and has been repeatedly hyperfocused on learning to code games for over a year, I am often extremely dissatisfied with how hard it can be to find tutorials that are TRULY teaching the subject at the most basic level. I've been interested in learning to build multiplayer games for quite a while, yet have made zero progress in months because of how much info out there assumes what you really want is a pre-built networking solution. I'll settle for templates and prebuilt solutions AFTER I'm satisfied that I understand what they're really doing under the hood, and to understand that, I need more content like this. Thank you!
@wolfscrygames
@wolfscrygames 2 года назад
I'm glad you found it helpful. Keep up the good work!
@somerandomguywastaken
@somerandomguywastaken 2 года назад
Thank you for the detailed breakdown. Its so hard to try understand the raw code without any understanding of networking syntax. Thanks!!!!!!!
@wolfscrygames
@wolfscrygames 2 года назад
Happy to have helped
@viviengemai9796
@viviengemai9796 3 года назад
Thanks, I needed a simple and clear explanation. Searching for System.Net / Sockets etc. did mostly turn up useless junk or ASP.Net even though all I needed was communication between applications for an internal network with C# alone. I'll see if I can set this up tomorrow. The goal is for an async polling to handle the client communication to the server when the database is updated to ensure any database change gets an instant update for all registered clients/IPs in the network. Your tutorial looks to be what I needed.
@wolfscrygames
@wolfscrygames 3 года назад
Sounds like it'll work out. Good luck!
@aba22125
@aba22125 2 года назад
I'm an advanced networking user, but kinda new to C# networking in particular, and this tutorial was amazing, thank you dude. Btw, you should teach about encryption -- I don't see any tutorials covering it, it's just a simple plaintext communication, which is a hella bad idea if you're going to allow external connections.
@wolfscrygames
@wolfscrygames 2 года назад
Thanks for the feedback. I'll definitely think about encryption topics for the future, but I will need to brush up on best practices since that can be a critical topic for scaling up a company. I have a rock paper scissors royale tutorial ready to go. Should be re-recorded and public by the weekend if you're interested.
@AddColors2525
@AddColors2525 Год назад
It was easy even for me as a beginner software engineer to understand. Thank you!
@MaxIzrin
@MaxIzrin 3 года назад
Might have to use this. Every connectivity package out there relies on players being in the same scene, and that's just not something I can rely on, since I have an asymmetric simulation. Good video, thanks.
@RoberBot
@RoberBot 2 года назад
This is awesome, finally someone that explains what hes doing You are awesome
@olotolow
@olotolow 4 года назад
Very good explained and a tcp connection is actually way easier then i thought.
@wolfscrygames
@wolfscrygames 4 года назад
Glad it helped! Took me a while to get the hang of it, but you're right, tcp connections are definitely easy enough to handle.
@btc76
@btc76 3 года назад
excellent video, thanks for this !! I will try and adapt your teachings to C# WPF app now :)
@paulanerspezi1634
@paulanerspezi1634 Год назад
Really nice tutorial! Works perfectly fine after 3 years!
@benzeyne99
@benzeyne99 4 года назад
very helpful and well explained video, thanks!
@wolfscrygames
@wolfscrygames 4 года назад
Happy to be of service. Hope it works the way you want it to.
@benzeyne99
@benzeyne99 4 года назад
@@wolfscrygames it worked out great, using the StreamReader and StreamWriter was the missing piece of the puzzle.
@edgarbarnett8348
@edgarbarnett8348 3 года назад
Very helpful and easy to follow. Keep up the great job!
@shara6281
@shara6281 2 года назад
Thanks for the Video, very helpful and well explained !
@wolfscrygames
@wolfscrygames 2 года назад
Glad it helped out.
@Video-ep4ij
@Video-ep4ij 3 года назад
why are we adding +1 to messageToSend variable while calculating byte size?
@shubhamsati3145
@shubhamsati3145 3 года назад
Thanks man it helped me understand the networking a little bit. I was really having a hard time finding some basic tutorials. Could you make a video about connecting unity clients to a dedicated server.
@samuelward9058
@samuelward9058 3 года назад
This works in unity aswell. Me: just realized this is a 4 month old comment 😅
@martinavallsrodriguez4321
@martinavallsrodriguez4321 4 года назад
Thanks ,very well explained and helpful!
@b0x3
@b0x3 2 года назад
بيض الله وجهك يالامير 🌹
@ryujinjakka8385
@ryujinjakka8385 3 года назад
This video helped me alot, thanks
@wolfscrygames
@wolfscrygames 3 года назад
Glad to help out
@rasmingabriellefiestada2357
@rasmingabriellefiestada2357 3 года назад
Hello, i tried this today and whenever i run it, on the client app, it only says (request received) without the "My name is Minmin"' .. can u pls help..?
@maurice3855
@maurice3855 4 года назад
Helped a lot! Thank you sir
@wolfscrygames
@wolfscrygames 4 года назад
Glad to be of service
@iQuickGaming
@iQuickGaming 3 года назад
i made an application and the data gets sent once but the second time i try sending something it doesnt work
@Eithanz
@Eithanz 2 года назад
in the server, you have sr but you dont use the streamreader???
@algum504
@algum504 2 года назад
i have never worked with streams (don't usually use Visual Studio, but have to check if VS can connect and communicate with our existing non-VS server) but i hope it has status functions (call read only if something is in stream) else it will just lock up the program if you are just waiting to read something
@wolfscrygames
@wolfscrygames 2 года назад
Streamreader and streamwriter just let you work with strings instead of byte arrays. Non-blocking sockets can be used, in which case you'll need to check status. Blocking sockets do prevent code progression like you mentioned.
@hiagomarcal5000
@hiagomarcal5000 4 года назад
Thank you.
@wolfscrygames
@wolfscrygames 4 года назад
Glad to be of service
@twiddle7125
@twiddle7125 4 года назад
Great video. Can the server be connected to multiple clients simultaneously?
@wolfscrygames
@wolfscrygames 4 года назад
Thanks! Yes, multiple clients can connect at the same time. Each client will connect through TCP using its IP Address behind the scenes.
@twiddle7125
@twiddle7125 4 года назад
@@wolfscrygames Great, thank you. Now I have to find an equivalent video for python, lol.
@migthundra2
@migthundra2 3 года назад
How would you modify this to allow you to receive protocol buffer serials?
@badactor9628
@badactor9628 3 года назад
really useful thanks
@erickcarballo1454
@erickcarballo1454 2 года назад
Excuse me, this can be use in WinForms?
@wolfscrygames
@wolfscrygames 2 года назад
It's a standard c# feature, so it would also work in forms. That being said, ensure your network appropriately allows incoming traffic, otherwise the data won't get past the router.
@noobicalcuriosity3436
@noobicalcuriosity3436 3 года назад
man ur very helpful
@johnmarkremotigue8985
@johnmarkremotigue8985 3 года назад
My server didn't receive the message "My name is Neo"
@JG-wv6bj
@JG-wv6bj 2 года назад
I know it's been a while, but I just stumbled across this to assist with an issue I was having and I found a slight issue with the code in the description. In the server-side code you've declared the string for the request but then done nothing with it. You need to include the request string in the line below otherwise the users won't see the messageToSend from the client.
@wolfscrygames
@wolfscrygames 2 года назад
Good idea! That would definitely increase clarity.
@gonzalolo
@gonzalolo Год назад
Hello there! I've been working on setting up a TCP system for streaming VR videos on Oculus. I've had trouble connecting from outside my network and your video explanation really helped. Do you think TCP is a good choice for my project? I plan to turn it into an asset, but I'm concerned about users having to deal with router setups for P2P connections. Any suggestions? Thanks a lot for your video!
@wolfscrygames
@wolfscrygames Год назад
Hello, You may be interested in looking up "nat hole punching".
@gonzalolo
@gonzalolo Год назад
@@wolfscrygames thanks mate, will check it up right now!
@gonzalolo
@gonzalolo Год назад
Mmm thing is , i want it to be p2p and i dont want to involve a 3rd party over the process, so this approach looks like it doesnt fit my needs :/ , but thanks a lot
@wolfscrygames
@wolfscrygames Год назад
For p2p, they'll need to either configure their networks or make simultaneous requests. A 3rd party in hole punching handles the synchronization, then allows the two clients to communicate independent of server. The main server only does the handshake to start the connection.
@gonzalolo
@gonzalolo Год назад
@@wolfscrygamesthanks for your answers :)!, im using photon fusion service, the service should have a special capability? or can i do the handshake just with an RPC?
@CuteRay
@CuteRay 3 года назад
Thx bro 😎
@BdyPhrk
@BdyPhrk 3 года назад
How to fix Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host ?
@wolfscrygames
@wolfscrygames 3 года назад
Sounds like a firewall issue. Any time you open a network connection, it needs to be allowed by firewall as it is technically an inherent security vulnerability.
@bynarie
@bynarie 2 года назад
NEVER, EVER, EVER use a goto statement. Great tutorial, except the goto statement. These are NOT needed. Just wrap your code into a function, and then call it again from within the catch() block. Thanks!
@wolfscrygames
@wolfscrygames 2 года назад
I appreciate the input, but goto statements are like salt: fine when used sparingly but harmful in excess. As you can see, a single goto is not going to produce spaghetti code. It's more clear for learners than a while loop and it's certainly more clear than a function that calls itself.
@olivieraudet-yang7576
@olivieraudet-yang7576 3 года назад
thx! :)
@wolfscrygames
@wolfscrygames 3 года назад
Happy to be of service
@zkkmtadndavideolar149
@zkkmtadndavideolar149 3 года назад
this is not only for LAN right ?
@wolfscrygames
@wolfscrygames 3 года назад
No, it also works for public, but you have to change the way you write in your ip address so the computer established a public socket instead of a local socket. Also, the client has to connect to the public ip address and port forwarding is required in the router setup.
@thisguythatguyify
@thisguythatguyify 4 года назад
Hey, how do I use this to connect to another computer? if I change the Ip address I get an error System.Net.Sockets.SocketException: 'The requested address is not valid in its context'
@wolfscrygames
@wolfscrygames 4 года назад
drive.google.com/file/d/1T_rn-3OWFhfdInJvBjwGlSIlMhPm8cMd/view You can format it as you see in this screenshot and it should accept your ip address.
@wolfscrygames
@wolfscrygames 4 года назад
There's a comment below with a long thread where I discussed in more detail. I'm on my phone and it won't let me copy it for you.
@siavashmehmandoost2198
@siavashmehmandoost2198 3 года назад
Thanks for your amazing tutorial! I successfully connected my client on unity to server. I'm new to networking. how to transfer another data such as player position, destruction of players, reducing health etc. Could you please help me through your own client script please?
@wolfscrygames
@wolfscrygames 3 года назад
Hello, This was meant to be an introduction, and I haven't done too much experimenting with networking in unity yet, but basically you need to create a protocol for transferring data. A simple example would be to add a prefix to any data you want to send, then parse(split) and handle it with switch statements. If the prefix is 0, that might mean player position was sent, or if the prefix is 1, maybe player health was sent, etc. Since this system can be confusing, it's best to use an enum instead of integers to keep your code readable.
@siavashmehmandoost2198
@siavashmehmandoost2198 3 года назад
@@wolfscrygames Thanks man! It'd be great if you could create a video focusing on that. I saw a tutorial focusing on transferring data based on tcp and asynchronous but It's just a bunch of words and codes without any explanation. You know I can just copy and paste that code, But I want to do it by myself! I saw an asset in Unity store named FMETP. is it helpful? assetstore.unity.com/packages/templates/packs/fmetp-stream-143080#reviews
@wolfscrygames
@wolfscrygames 3 года назад
@@siavashmehmandoost2198 I might be able to put something together. That asset you linked looks like it's for video streaming. You might be more interested in Forge Networking, Mirror networking, or PUN2.
@zaebalzaebal2820
@zaebalzaebal2820 4 года назад
Why you just dont make it while(true) all your code rather then that goto?
@wolfscrygames
@wolfscrygames 4 года назад
There's nothing inherently wrong with goto statements, although they are generally cautioned against because of misuse. As long as goto statements are used sparingly and sensibly, they can make code easy to read. A while(true) should accomplish the same task. Goto can also be used to exit a nested for loop instead of doing break; if (condition) break; In this case, the goto statement just reminds me at the bottom of my code that I'm going back to the top.
@axyscripting
@axyscripting 3 года назад
When I try to run the server on a different device in my house the client cant connect to the server. Pls help
@wolfscrygames
@wolfscrygames 3 года назад
If it's all on the same network, you just need the server ip address. If it's on a different network, such as cell service, you need to use port forwarding.
@axyscripting
@axyscripting 3 года назад
@@wolfscrygames I’m new to this networking. What is port forwarding?
@wolfscrygames
@wolfscrygames 3 года назад
@@axyscripting you're going to need the information to log into your router. It's usually printed on a sticker on the side of the router. Basically, your router has a built in firewall and denies all incoming traffic unless you form the connection first. To turn off the firewall on one port(out of about 65000) you designate a port and send all traffic for your app to that port. Your router then sends it to your pc. You can look up instructions for your specific router model.
@axyscripting
@axyscripting 3 года назад
@@wolfscrygames how do you send all the traffic to the port?
@wolfscrygames
@wolfscrygames 3 года назад
@@axyscripting port forwarding. You'll have to look up that part on your own since everyone has different routers.
@hajkotv5558
@hajkotv5558 4 года назад
So it will work in my LAN? Also with more clients? Thanks for answer
@wolfscrygames
@wolfscrygames 4 года назад
Yes, you just need to type the ip part differently for it to work outside localhost. On server use the local ip, 192.168.x.x, on client use the external ip aka public ip. Don't share these values with others as they should be private.
@wolfscrygames
@wolfscrygames 4 года назад
drive.google.com/file/d/1T_rn-3OWFhfdInJvBjwGlSIlMhPm8cMd/view?usp=sharing
@hajkotv5558
@hajkotv5558 4 года назад
@@wolfscrygames thanks
@skymix5274
@skymix5274 3 года назад
Hi i know is late but Can i use it to trojan programs?
@wolfscrygames
@wolfscrygames 3 года назад
A Trojan is just a program in disguise, so in a way, probably. I wouldn't recommend writing Trojans as you can get into serious legal trouble.
@skymix5274
@skymix5274 3 года назад
@@wolfscrygames its a ask i Will dont use it to it i only ask
@pollomagico271
@pollomagico271 3 года назад
@@skymix5274 its just for a friend huh
@user-vc1bk4fw7e
@user-vc1bk4fw7e 4 года назад
can you give me the code of the unity chaat ??please
@wolfscrygames
@wolfscrygames 4 года назад
@@user-vc1bk4fw7e The code is in the description
@user-vc1bk4fw7e
@user-vc1bk4fw7e 4 года назад
@@wolfscrygames IN UNITY .PLEASE
@donfoumare
@donfoumare 4 года назад
5:27 Well, I saw more or less the same example code (lines 18-20) in other "tutorials" with almost the same variable names which to me looks like copy & paste. Why on earth do you first allocate memory with the new operator, assign that to sendData and than assign something else to sendData? What's the point of lines 18 and 19? Do you even know the basics? Things like "what is a variable", "what does the 'new' operator do", "what does the '=' operator do"...
@wolfscrygames
@wolfscrygames 4 года назад
I do it because it works. It's okay to have your own coding style and it's actually preferred practice to copy and paste. When the code is the same in multiple places, it makes it easier to find solutions when problems come up.
@donfoumare
@donfoumare 4 года назад
@@wolfscrygames ​While it is convenient to reuse code so you don't have to reinvent the wheel each time, you should make sure the code you copied is ok. Especially if you use it for educational purposes. Having an own "coding style" is one thing, but arbitrarily allocating unnecessary memory for no reason is just bad programming. Despite that, it is too obvious that the 'new byte[..]' instruction is unnecessary, unless you don't know how assignments work. Even if the compiler removes the instructions as part of it's optimizations it will still be in the source code for no reason. While i'm writing this i realize that i maybe overreact a bit but I saw this in a couple of videos in the last hours.. I know nobody is perfect and you may be a beginner in programming (i don't know you) but you should double-check your sources in the future before using them. And while programming looks like an easy task these days with all the frameworks, libraries, tutorials and sources in the internet, it is still worth to take the time and dig somewhat deeper to fully understand what happens behind the scenes. This will help you to avoid errors, make your code more efficient, more reliable, makes it easier to find bugs and to maintain the code.
@wolfscrygames
@wolfscrygames 4 года назад
@@donfoumare I'll look into removing those two lines. I'm a high school teacher full time and often only have 10-30 minutes to make these videos. Thanks to Coronavirus I now have a unique opportunity with schools closing to work on my channel. I started programming 3 years ago, and this topic is something that was hard to learn and hard to find info for.
Далее
C# Async Sockets Part 1: Basics
22:15
Просмотров 48 тыс.
Cute
00:16
Просмотров 6 млн
Growing fruit art
00:33
Просмотров 2,9 млн
C# Socket Programming - Multiple Clients
29:08
Просмотров 361 тыс.
Socket Programming in C# (Overview)
4:32
Просмотров 23 тыс.
How To Create a Chat App and Server Tutorial WPF C#
55:29
Cursor Is Beating VS Code (...by forking it)
18:00
Просмотров 70 тыс.
Socket in C#
50:18
Просмотров 21 тыс.