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 / C++ / MFC
  4. How to implement WebSocket Client in C

How to implement WebSocket Client in C

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmintutorial
7 Posts 5 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.
  • P Offline
    P Offline
    Parthi_Appu
    wrote on last edited by
    #1

    Hi All, I have to hit a WebSocket server and have to maintain the connection alive. Please share some article about implementing a WebSocket Client application in C. Thanks All.


    Do your Duty and Don't expect the Result

    _ 1 Reply Last reply
    0
    • P Parthi_Appu

      Hi All, I have to hit a WebSocket server and have to maintain the connection alive. Please share some article about implementing a WebSocket Client application in C. Thanks All.


      Do your Duty and Don't expect the Result

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      For a socket client in Windows you basically need to call the following functions in order - WSAStartup[^] - Initialize winsock. socket[^] - Create the socket specifying the required protocol like TCP, UDP etc. connect[^] - Connect the previously created socket to the specified server. After this step the connection is ready. To communicate with the server you can use the following functions - send[^]/sendto[^] - Send a stream of bytes to the server. recv[^]/recvfrom[^] - Receive a stream of bytes from the server. To tear down the connection the following functions must be called - closesocket[^] - Disconnect WSACleanup[^] - Cleanup winsock.

      «_Superman_»  I love work. It gives me something to d

      P 1 Reply Last reply
      0
      • _ _Superman_

        For a socket client in Windows you basically need to call the following functions in order - WSAStartup[^] - Initialize winsock. socket[^] - Create the socket specifying the required protocol like TCP, UDP etc. connect[^] - Connect the previously created socket to the specified server. After this step the connection is ready. To communicate with the server you can use the following functions - send[^]/sendto[^] - Send a stream of bytes to the server. recv[^]/recvfrom[^] - Receive a stream of bytes from the server. To tear down the connection the following functions must be called - closesocket[^] - Disconnect WSACleanup[^] - Cleanup winsock.

        «_Superman_»  I love work. It gives me something to d

        P Offline
        P Offline
        Parthi_Appu
        wrote on last edited by
        #3

        Thanks for the reply.. but i'm not looking for normal Socket implementation. Any way i got little help here http://en.wikipedia.org/wiki/WebSockets[^]


        Do your Duty and Don't expect the Result

        M 1 Reply Last reply
        0
        • P Parthi_Appu

          Thanks for the reply.. but i'm not looking for normal Socket implementation. Any way i got little help here http://en.wikipedia.org/wiki/WebSockets[^]


          Do your Duty and Don't expect the Result

          M Offline
          M Offline
          markkuk
          wrote on last edited by
          #4

          The WebSocket protocol works on top of a normal TCP socket connection, so you must understand Winsock first before you can start implementing WebSockets.

          P 1 Reply Last reply
          0
          • M markkuk

            The WebSocket protocol works on top of a normal TCP socket connection, so you must understand Winsock first before you can start implementing WebSockets.

            P Offline
            P Offline
            Parthi_Appu
            wrote on last edited by
            #5

            Thanks for the reponses.. guess i'm not clear with my question.. I'm clear about the WinSocket APIs.. just need to know the WebSocket Handshake details.. I got the below header from Wikipedia & i want to know what are all the things are variable values in this header?

            GET /demo HTTP/1.1
            Upgrade: WebSocket
            Connection: Upgrade
            Host: example.com
            Origin: http://example.com
            Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
            Sec-WebSocket-Key2: 12998 5 Y3 1 .P00

            ^n:ds[4U

            As far as i know the /demo in first line, Host & Origin will change.. but exactly what will be the values? Will the values of Key1, Key2 & the last 8 byte values also change? I have to hit the server which has the format as ws://xxx.xxx.xxx.xxx:8181/websock Also is it possible to send any extra information in the above handshake header? if so what will be its format? Thanks Again.


            Do your Duty and Don't expect the Result

            I M 2 Replies Last reply
            0
            • P Parthi_Appu

              Thanks for the reponses.. guess i'm not clear with my question.. I'm clear about the WinSocket APIs.. just need to know the WebSocket Handshake details.. I got the below header from Wikipedia & i want to know what are all the things are variable values in this header?

              GET /demo HTTP/1.1
              Upgrade: WebSocket
              Connection: Upgrade
              Host: example.com
              Origin: http://example.com
              Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
              Sec-WebSocket-Key2: 12998 5 Y3 1 .P00

              ^n:ds[4U

              As far as i know the /demo in first line, Host & Origin will change.. but exactly what will be the values? Will the values of Key1, Key2 & the last 8 byte values also change? I have to hit the server which has the format as ws://xxx.xxx.xxx.xxx:8181/websock Also is it possible to send any extra information in the above handshake header? if so what will be its format? Thanks Again.


              Do your Duty and Don't expect the Result

              I Offline
              I Offline
              Indivara
              wrote on last edited by
              #6

              For anyone who finds this thread - Article - Web Socket Server[^] and update[^] can be used as references.

              1 Reply Last reply
              0
              • P Parthi_Appu

                Thanks for the reponses.. guess i'm not clear with my question.. I'm clear about the WinSocket APIs.. just need to know the WebSocket Handshake details.. I got the below header from Wikipedia & i want to know what are all the things are variable values in this header?

                GET /demo HTTP/1.1
                Upgrade: WebSocket
                Connection: Upgrade
                Host: example.com
                Origin: http://example.com
                Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
                Sec-WebSocket-Key2: 12998 5 Y3 1 .P00

                ^n:ds[4U

                As far as i know the /demo in first line, Host & Origin will change.. but exactly what will be the values? Will the values of Key1, Key2 & the last 8 byte values also change? I have to hit the server which has the format as ws://xxx.xxx.xxx.xxx:8181/websock Also is it possible to send any extra information in the above handshake header? if so what will be its format? Thanks Again.


                Do your Duty and Don't expect the Result

                M Offline
                M Offline
                Manu Dhundi
                wrote on last edited by
                #7

                Can websockets be implemented using CURL ? Also it seems that once the client receives "HTTP/1.1 101 WebSocket Protocol Handshake" it should just wait on recv or recvfrom. Am I correct ? If so, who closes the socket connection once all communication is done? The server or the client ? Thanks

                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