c# sockets [modified]
-
i need some guidance regarding c# winsock programming, im a beginner in programming and network applications. i understand the client/server idea. server -> socket() bind() listen() accept() {send() recv()} close() client -> socket() connect() {send() recv()} close() -- i also know Socket.Connected Property to test if connected. -- where i am having difficulty is in both server and client how to implement send() and recv() [events]. i know how to send and receive but to do it while keeping the connection seems to very vague in google links. most examples on google just creat the connection and may send some data or receive some data and the connection is closed. the while(true) on server runs in a loop and accepts new connections. i need to know how to implement send and recv (events) so the connected clients and server can keep on exchanging data using the same connection. lots of the web examples have the server running in a loop and the client makes a new connection whenever data needs to be sent, it doesn't seem efficient to me. i have also seen server loop running on its own thread to avoid gui lockups but there also the actual communication seems vague. so. i would like info on how to handle send and recv in a structured manner. thanks for reading and a very happy new year to you. --------------------- my current approach is wrong, it concerns blocking.. i should be using .NET asynchronous socket programming. thanks.
modified on Saturday, January 2, 2010 1:36 PM
-
i need some guidance regarding c# winsock programming, im a beginner in programming and network applications. i understand the client/server idea. server -> socket() bind() listen() accept() {send() recv()} close() client -> socket() connect() {send() recv()} close() -- i also know Socket.Connected Property to test if connected. -- where i am having difficulty is in both server and client how to implement send() and recv() [events]. i know how to send and receive but to do it while keeping the connection seems to very vague in google links. most examples on google just creat the connection and may send some data or receive some data and the connection is closed. the while(true) on server runs in a loop and accepts new connections. i need to know how to implement send and recv (events) so the connected clients and server can keep on exchanging data using the same connection. lots of the web examples have the server running in a loop and the client makes a new connection whenever data needs to be sent, it doesn't seem efficient to me. i have also seen server loop running on its own thread to avoid gui lockups but there also the actual communication seems vague. so. i would like info on how to handle send and recv in a structured manner. thanks for reading and a very happy new year to you. --------------------- my current approach is wrong, it concerns blocking.. i should be using .NET asynchronous socket programming. thanks.
modified on Saturday, January 2, 2010 1:36 PM
-
i need some guidance regarding c# winsock programming, im a beginner in programming and network applications. i understand the client/server idea. server -> socket() bind() listen() accept() {send() recv()} close() client -> socket() connect() {send() recv()} close() -- i also know Socket.Connected Property to test if connected. -- where i am having difficulty is in both server and client how to implement send() and recv() [events]. i know how to send and receive but to do it while keeping the connection seems to very vague in google links. most examples on google just creat the connection and may send some data or receive some data and the connection is closed. the while(true) on server runs in a loop and accepts new connections. i need to know how to implement send and recv (events) so the connected clients and server can keep on exchanging data using the same connection. lots of the web examples have the server running in a loop and the client makes a new connection whenever data needs to be sent, it doesn't seem efficient to me. i have also seen server loop running on its own thread to avoid gui lockups but there also the actual communication seems vague. so. i would like info on how to handle send and recv in a structured manner. thanks for reading and a very happy new year to you. --------------------- my current approach is wrong, it concerns blocking.. i should be using .NET asynchronous socket programming. thanks.
modified on Saturday, January 2, 2010 1:36 PM
.NET makes this stuff pretty straight forward. Server side, use a TcpListener to accept incoming connections from the client. Once accepted, which gives you a TcpClient, the ususal practice is to create a new thread and pass it the TcpClient and let it do its server side stuff. If the connection is likely to be brief, use the ThreadPool. On the client, just use a TcpClient to connect to the server. Generally, the connection stays open until either end closes it, or it *may* get closed due to inactivity timeouts - not sure. You can attach a NetworkStream to the TcpClient at both ends, so you just end up with a duplex stream you can read and write to. Cassini is a little web server from the early days of .NET which allowed people to develop ASP.NET applications. Have a look at the source of that for some patterns. http://blogs.msdn.com/dmitryr/archive/2005/09/27/474534.aspx[^]
Regards, Rob Philpott.