Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# sockets [modified]

c# sockets [modified]

Scheduled Pinned Locked Moved C#
csharpsysadmintutoriallearning
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ikurtz
    wrote on last edited by
    #1

    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

    N R 2 Replies Last reply
    0
    • I ikurtz

      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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      You need another thread which accepts messages from clients and process it. Take a look at this[^] MSDN page. New year wishes to you too.

      Best wishes, Navaneeth

      1 Reply Last reply
      0
      • I ikurtz

        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

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #3

        .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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups