Jump to content

C# Network programming

Guest
Go to solution Solved by BobVonBob,

TCP sockets are, as a general rule, closed after every request. That's wrong. They will generally be closed when no subsequent data is required for some length of time. You don't want to make a new connection for every image on a webpage for instance. Your best bet to check what clients are connected would probably be a unique identifier sent in regular pings from the client, with a timeout so that if a client disconnects unexpectedly the server doesn't still think they are connected. This also allows for the client to change IP addresses without disconnecting, say they switch Wifi networks or use a mobile data connection.

 

Microsoft's help pages are actually very good with regards to network programming using .NET.

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/

I'm trying to work with TCP & I'm going quite mad.

Does anyone have an quality guides that explain step by step what goes on in C# sockets?

Does anyone suggest any alternatives?

I am writing a networked game (I won't use Unity's services. They don't provide the control I need.) & need to program network stuff like last week.

 

I've been trying to understand through this guide:
https://www.youtube.com/watch?v=GyuUtAqWVPU

(Part 22.2 & 22.1 as well) but I'm unsure of what to do.

I followed some other guide I found elsewhere but don't understand the code I wrote anymore. It has a ton of abstractions & weird stuff going on.


I'm sure I'm exhausted & mentally drained which isn't helping.

 

I'm writing a server & client (Right now I am perfectly okay with JUST using localhost with 1 server & 1 client.)

 

What I'm trying to do:

Spoiler

Setup:
Server-Has maps stored as .txt files. Has misc data as a class (enumerations in class)

Client-Has player character & can generate the maps based off the .txt data.

 

Step 1:
Server starts up

Step 2:
Client connects to server

Step 3:
Client tells server what map it wants

Step 4:

Server gives map to client

Step 5:
Client loads map

Step 6:

Client plays on map

Step 7:
Client decides to stop playing. Closes game

Step 8:

Map file is sent back to server

 

I would like to have all connected clients remembered whilst the server is running. I would appreciate the server knowing when clients disconnect & closing the connection when that occurs but one step at a time. (Perhaps ping the client every now & then to say "Are you still there?")

 

EDIT:
I'm not asking for the code to be written for me. I am asking for a guide that explains what's needed for TCP protocol, & what steps should be taken & why. If there's a guide that exists for beginning network programmers that know C#.

Link to comment
Share on other sites

Link to post
Share on other sites

TCP sockets are, as a general rule, closed after every request. That's wrong. They will generally be closed when no subsequent data is required for some length of time. You don't want to make a new connection for every image on a webpage for instance. Your best bet to check what clients are connected would probably be a unique identifier sent in regular pings from the client, with a timeout so that if a client disconnects unexpectedly the server doesn't still think they are connected. This also allows for the client to change IP addresses without disconnecting, say they switch Wifi networks or use a mobile data connection.

 

Microsoft's help pages are actually very good with regards to network programming using .NET.

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, BobVonBob said:

TCP sockets are, as a general rule, closed after every request.

Is this just how the TCP standard is setup?
Or is this how all network protocol are setup?

2 minutes ago, BobVonBob said:

Your best bet to check what clients are connected would probably be a unique identifier sent in regular pings from the client, with a timeout so that if a client disconnects unexpectedly the server doesn't still think they are connected. This also allows for the client to change IP addresses without disconnecting, say they switch Wifi networks or use a mobile data connection.

How would I store the client & know who the client is if he changes IP addresses?

 

As far as I know, the IP address is the only thing saying who you are/how to find you on the internet.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, fpo said:

Is this just how the TCP standard is setup?
Or is this how all network protocol are setup?

How would I store the client & know who the client is if he changes IP addresses?

 

As far as I know, the IP address is the only thing saying who you are/how to find you on the internet.

I messed up the first bit, or at least didn't word it right.

 

As for the second bit, use a unique identifier. A-la a Steam ID or Username. Although you'll need to perform more validation than that if you don't want people to be able to fake being a different person. Probably a pseudo-randomly generated token sent to the client and saved there. Session cookies but for an application.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, BobVonBob said:

I messed up the first bit, or at least didn't word it right.

How would you re-phrase it?

6 minutes ago, BobVonBob said:

As for the second bit, use a unique identifier. A-la a Steam ID or Username. Although you'll need to perform more validation than that if you don't want people to be able to fake being a different person. Probably a pseudo-randomly generated token sent to the client and saved there. Session cookies but for an application.

Ahh. That would be a good idea.
I don't know how advanced that'd be. As of right now, I don't understand why or how any network code is used. I'm reading through that guide you posted earlier however.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, fpo said:

How would you re-phrase it?

I edited the first post I made to make it more clear. You don't close after every request, you usually close after every block of requests.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, fpo said:

How would you re-phrase it?

Bonus, that other definition might also be wrong depending on your use case, you can use "keep alive" functionality on TCP sockets with Async IO to support many more simultaneous connections than you will ever need.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, BobVonBob said:

snip

Can you provide an example program with an explanation in what you're doing & why?

Link to comment
Share on other sites

Link to post
Share on other sites

48 minutes ago, fpo said:

Can you provide an example program with an explanation in what you're doing & why?

Probably not, I don't know too much about networking in C# in particular, but I've done network stuff in other languages before.

 

Here's Microsoft's examples, you'll probably be best served with the asynchronous client and server.

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/socket-code-examples

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, BobVonBob said:

snip

I think I'm getting it a lot better now.

I re-watched the videos in my OP with your input & reading through some of the information & I'm getting a more firm understanding.

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×