Client Server Connection C#
I have been reading up on different ways to do it. Sockets and threading and such. Anyone have an example connecting and establishing a session? I would like to send data back and forth. Would I open a thread or use a socket? C# from what i find isn't as community wide developed so much as C++ or my language PHP.
I have been reading up on different ways to do it. Sockets and threading and such. Anyone have an example connecting and establishing a session? I would like to send data back and forth. Would I open a thread or use a socket? C# from what i find isn't as community wide developed so much as C++ or my language PHP.
Sockets and threads are two very different things. You can't use a thread to talk to a server. Maybe you're wondering about whether you should use asynchronous sockets or if you should use synchronous sockets in a dedicated thread? Here's a link to some network programming samples. I'm not sure I understand your last sentence...
I have been reading up on different ways to do it. Sockets and threading and such. Anyone have an example connecting and establishing a session? I would like to send data back and forth. Would I open a thread or use a socket? C# from what i find isn't as community wide developed so much as C++ or my language PHP.
A persistent session in PHP is not something that will be successful. PHP is built to handle one request, spit out a result, and then go away within seconds. A persistent connection goes against the entire grain of the ecosystem (even though you CAN run PHP from the command line, and you CAN call bind()/accept() on sockets in PHP...)
You would probably use at most one thread per physical core on the CPU -- or, to start, just a single thread. In C#, you'd use asynchronous I/O (either BeginReceive() or the more modern ReceiveAsync()). In C++, you'd probably use boost::asio, or libevent (on UNIX) or I/O completion ports (on Windows) or even simple select() for smaller servers.
The tutorials pointed at by the FAQ generally do a good job of starting you out!
[quote name='d124c0' timestamp='1321579364' post='4885178']
I have been reading up on different ways to do it. Sockets and threading and such. Anyone have an example connecting and establishing a session? I would like to send data back and forth. Would I open a thread or use a socket? C# from what i find isn't as community wide developed so much as C++ or my language PHP.
A persistent session in PHP is not something that will be successful. PHP is built to handle one request, spit out a result, and then go away within seconds. A persistent connection goes against the entire grain of the ecosystem (even though you CAN run PHP from the command line, and you CAN call bind()/accept() on sockets in PHP...)
You would probably use at most one thread per physical core on the CPU -- or, to start, just a single thread. In C#, you'd use asynchronous I/O (either BeginReceive() or the more modern ReceiveAsync()). In C++, you'd probably use boost::asio, or libevent (on UNIX) or I/O completion ports (on Windows) or even simple select() for smaller servers.
The tutorials pointed at by the FAQ generally do a good job of starting you out!
[/quote]
???? when i said php i was talking about the time it has been around and because it is open and very community driven. Also you can't make a desktop application with PHP so in this case I wouldn't compare them....although at least I know PHP so I would enjoy it much more.
A question though with C# would be be best to have an asynchronous connection for say the login server? Just basically to authenticate and such.
In C# how would I pass the connection off to the other part of the server? Being separate programs or even separate servers. You can just point me in the right direction I dun assume anyone is gonna take there time to explain it too much..... unless you want too.
[quote name='d124c0' timestamp='1321579364' post='4885178']
I have been reading up on different ways to do it. Sockets and threading and such. Anyone have an example connecting and establishing a session? I would like to send data back and forth. Would I open a thread or use a socket? C# from what i find isn't as community wide developed so much as C++ or my language PHP.
Sockets and threads are two very different things. You can't use a thread to talk to a server. Maybe you're wondering about whether you should use asynchronous sockets or if you should use synchronous sockets in a dedicated thread? Here's a link to some network programming samples. I'm not sure I understand your last sentence...
[/quote]
Thanks for that. Sorry I am still new to network programming. Also thanks for the link I read most of it and understood some of it. Still trying to pass a username and password to the server from a client....not able to get it working properly.
A question though with C# would be be best to have an asynchronous connection for say the login server? Just basically to authenticate and such.
Yes, you pretty much always want to use asynchronous I/O for networking. The pattern is generally to have some asynchronous system that pumps data from the network into a buffer, and then something else that looks at data in the buffer and decodes as many packets as it can out of that buffer, until there's not enough data for "the next" packet.
To "hand off" you typically return a service descriptor, which might be the IP address and port number of the other process you want the client to talk to. Or you create a gateway that does the connection/dispatching for you -- client connects to gateway, and gateway creates connections to servers, and then client just says "send this packet to server 52."
Thanks for that. Sorry I am still new to network programming. Also thanks for the link I read most of it and understood some of it. Still trying to pass a username and password to the server from a client....not able to get it working properly.
Just to help you narrow down you're reading, here are two small examples of using asynchronous sockets in C#:
Client (full example code with less explanation)
Server (full example code with less explanation)
Sockets can be a little difficult to wrap you're head around. It just takes some time. I remember distinctly when it all "clicked" for me. I'm far from being a pro, but there was a definite hurdle I almost felt myself get over. Asynchronous sockets are a little harder to understand than synchronous sockets (just because you have to register callbacks and be a little more careful with how you use your buffers).
If you're just beginning sockets, I'd suggest beginning with synchronous sockets just to get your head around how they work, then go on to the asynchronous stuff. Links for the synchronous examples are there at the bottom of the pages I linked to.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement