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. Simple socks TCP connecting

Simple socks TCP connecting

Scheduled Pinned Locked Moved C / C++ / MFC
comsysadminhelpannouncementlounge
11 Posts 2 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.
  • J Johpoke

    Im (trying) to create a very, very simple TCP client and server which will connect and send a string of data that is 64 characters long. Im using Server Client Sockets[^] and Simple TCP Server[^] I have pretty much copy paste the code, and the actual 64 characters data transfer works fine. But now im moving on to making the client (which connectets to the listeing server) connect again maybe 100 ms later, to update the data. Notice that the cleint is re-doing all of the socks code, the whole WSAstartup, socket init etc. this part can work fine for a few hundred connections, but sometimes, at what seems random intervals the connecting just dies for a while, a few seconds later its back up and going. Im not getting any error, or return, its just like the socket initing freezes for a few seconds, then keeps going. I tried making the connection stay open, so it just revc's but i could not get it to work. (im very new to socks...) Im guessing windows/socks doesnt like my program rapidly disconnecting and then creating a new connection all the time, because in my firewall/netstat it seems my program has loads of open connections to many other ports (and i only want it to use 23) TCP thecomp:3172 localhost:telnet TIME_WAIT TCP thecomp:3174 localhost:telnet TIME_WAIT TCP thecomp:3175 localhost:telnet TIME_WAIT TCP thecomp:3176 localhost:telnet TIME_WAIT TCP thecomp:3177 localhost:telnet TIME_WAIT TCP thecomp:3178 localhost:telnet TIME_WAIT TCP thecomp:3179 localhost:telnet TIME_WAIT TCP thecomp:3182 localhost:telnet TIME_WAIT TCP thecomp:3183 localhost:telnet TIME_WAIT TCP thecomp:3185 localhost:telnet TIME_WAIT TCP thecomp:3186 localhost:telnet TIME_WAIT TCP thecomp:3187 localhost:telnet TIME_WAIT TCP thecomp:3190 localhost:telnet TIME_WAIT TCP thecomp:3191 localhost:telnet TIME_WAIT TCP thecomp:3193 localhost:telnet CLOSE_WAIT TCP thecomp:3194 localhost:telnet TIME_WAIT TCP thecomp:3196 localhost:telnet TIME_WAIT TCP thecomp:3197 localhost:telnet C

    M Offline
    M Offline
    Moak
    wrote on last edited by
    #2

    Hi Johannes, the information you see with netstat looks okay (a normal behaviour of TCP). Can you describe your error behaviour a bit more or post the client code? Basically it is possible to open a connection every 100ms, but it could be much more effective if you stay connected and only reconnect if the connection was interrupted (depends on what you want to do). /M

    My Webchat

    J 1 Reply Last reply
    0
    • M Moak

      Hi Johannes, the information you see with netstat looks okay (a normal behaviour of TCP). Can you describe your error behaviour a bit more or post the client code? Basically it is possible to open a connection every 100ms, but it could be much more effective if you stay connected and only reconnect if the connection was interrupted (depends on what you want to do). /M

      My Webchat

      J Offline
      J Offline
      Johpoke
      wrote on last edited by
      #3

      Thanks for the reply. I was recently suggested to add shutdown(socket, SD_BOTH); this made it more reliable, but still not 100%. At the moment it seems that connect function just freezes for a few seconds and then returns that it failed (to connect to the server) The connection is however fine, as both before and after it kept going. This seems to happend at random intervals. (between ~100 connections to ~2500) Right now though, im probably re connecting every 50-100ms (cant remember)... Note that what i pasted from netstat is just a little of it, there were so many i cant even copy them all as they go beyong max content of a normal cmd window... I would (if possible) like to stay with a non-constant connection, as this is easier for me (if i get it to work) + faster on a unreliable connection. Thanks

      //Johannes

      M 1 Reply Last reply
      0
      • J Johpoke

        Thanks for the reply. I was recently suggested to add shutdown(socket, SD_BOTH); this made it more reliable, but still not 100%. At the moment it seems that connect function just freezes for a few seconds and then returns that it failed (to connect to the server) The connection is however fine, as both before and after it kept going. This seems to happend at random intervals. (between ~100 connections to ~2500) Right now though, im probably re connecting every 50-100ms (cant remember)... Note that what i pasted from netstat is just a little of it, there were so many i cant even copy them all as they go beyong max content of a normal cmd window... I would (if possible) like to stay with a non-constant connection, as this is easier for me (if i get it to work) + faster on a unreliable connection. Thanks

        //Johannes

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

        Hi again! Without the error code it is hard to figure out what is going wrong and I can only guess. Maybe your are using hostnames (rather than IPs) and the DNS resolution blocks when connecting or you didn't close sockets properly (in client or server code) and reached an upper limit per process. In any case, your application will only work inside the LAN very well, on the internet you will probably encounter longer round trips than 50-100ms and the TCP handshake alone probably takes more time than sending your payload data. Again I am only guessing... it would be helpful to see code, error codes or your application requirements. ;) Hope it helps.

        My Webchat

        J 1 Reply Last reply
        0
        • M Moak

          Hi again! Without the error code it is hard to figure out what is going wrong and I can only guess. Maybe your are using hostnames (rather than IPs) and the DNS resolution blocks when connecting or you didn't close sockets properly (in client or server code) and reached an upper limit per process. In any case, your application will only work inside the LAN very well, on the internet you will probably encounter longer round trips than 50-100ms and the TCP handshake alone probably takes more time than sending your payload data. Again I am only guessing... it would be helpful to see code, error codes or your application requirements. ;) Hope it helps.

          My Webchat

          J Offline
          J Offline
          Johpoke
          wrote on last edited by
          #5

          Hey, connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo)); is returning -1 ( SOCKET_ERROR ). It seems to happend just randomy. This time it occured after ~7000 connections. My program does resolve a DNS name, which is placed inside the hosts file (like localhost), that should work instantly... It just errord again, once more now. There is a very long time between between connect attemp, and when it returns -1, maybe like ~15 seconds... Yea, my program is only gonna run on a direct LAN, or maybe between a few switches/router. I want a simple and reliable way to send a 64char string, if i use TCP/UDP/ICMP/Multicast doesnt really matter, and my server side doesnt need to know if the data came to the client or not... Thanks!

          //Johannes

          M 1 Reply Last reply
          0
          • J Johpoke

            Im (trying) to create a very, very simple TCP client and server which will connect and send a string of data that is 64 characters long. Im using Server Client Sockets[^] and Simple TCP Server[^] I have pretty much copy paste the code, and the actual 64 characters data transfer works fine. But now im moving on to making the client (which connectets to the listeing server) connect again maybe 100 ms later, to update the data. Notice that the cleint is re-doing all of the socks code, the whole WSAstartup, socket init etc. this part can work fine for a few hundred connections, but sometimes, at what seems random intervals the connecting just dies for a while, a few seconds later its back up and going. Im not getting any error, or return, its just like the socket initing freezes for a few seconds, then keeps going. I tried making the connection stay open, so it just revc's but i could not get it to work. (im very new to socks...) Im guessing windows/socks doesnt like my program rapidly disconnecting and then creating a new connection all the time, because in my firewall/netstat it seems my program has loads of open connections to many other ports (and i only want it to use 23) TCP thecomp:3172 localhost:telnet TIME_WAIT TCP thecomp:3174 localhost:telnet TIME_WAIT TCP thecomp:3175 localhost:telnet TIME_WAIT TCP thecomp:3176 localhost:telnet TIME_WAIT TCP thecomp:3177 localhost:telnet TIME_WAIT TCP thecomp:3178 localhost:telnet TIME_WAIT TCP thecomp:3179 localhost:telnet TIME_WAIT TCP thecomp:3182 localhost:telnet TIME_WAIT TCP thecomp:3183 localhost:telnet TIME_WAIT TCP thecomp:3185 localhost:telnet TIME_WAIT TCP thecomp:3186 localhost:telnet TIME_WAIT TCP thecomp:3187 localhost:telnet TIME_WAIT TCP thecomp:3190 localhost:telnet TIME_WAIT TCP thecomp:3191 localhost:telnet TIME_WAIT TCP thecomp:3193 localhost:telnet CLOSE_WAIT TCP thecomp:3194 localhost:telnet TIME_WAIT TCP thecomp:3196 localhost:telnet TIME_WAIT TCP thecomp:3197 localhost:telnet C

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #6

            Hey, just a quick question about your requirements: Do you want to send 64 bytes from client to server without a response in regular intervalls... or the other way around where the server sends data to every incoming client?

            My Webchat

            J 1 Reply Last reply
            0
            • M Moak

              Hey, just a quick question about your requirements: Do you want to send 64 bytes from client to server without a response in regular intervalls... or the other way around where the server sends data to every incoming client?

              My Webchat

              J Offline
              J Offline
              Johpoke
              wrote on last edited by
              #7

              The server (which binds to port 23) is listening to incoming clients, and as soon as one connects, it sends the 64 characters, and then disconects the client, waiting for a new one. So the second way in you message. But yea, the server doesnt need to know anything really, just send the string upon new client. (the server doesnt have any idea when i cleint is coming either (ie in what interval) When i was testing it a few mins ago, the interval was Sleep(50); Thanks!

              //Johannes

              M 1 Reply Last reply
              0
              • J Johpoke

                Hey, connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo)); is returning -1 ( SOCKET_ERROR ). It seems to happend just randomy. This time it occured after ~7000 connections. My program does resolve a DNS name, which is placed inside the hosts file (like localhost), that should work instantly... It just errord again, once more now. There is a very long time between between connect attemp, and when it returns -1, maybe like ~15 seconds... Yea, my program is only gonna run on a direct LAN, or maybe between a few switches/router. I want a simple and reliable way to send a 64char string, if i use TCP/UDP/ICMP/Multicast doesnt really matter, and my server side doesnt need to know if the data came to the client or not... Thanks!

                //Johannes

                M Offline
                M Offline
                Moak
                wrote on last edited by
                #8

                Maybe your are running out of socket objects. Again I am only guessing... it would be helpful to see source code and detailed error codes (WSAGetLastError).

                My Webchat

                J 1 Reply Last reply
                0
                • J Johpoke

                  The server (which binds to port 23) is listening to incoming clients, and as soon as one connects, it sends the 64 characters, and then disconects the client, waiting for a new one. So the second way in you message. But yea, the server doesnt need to know anything really, just send the string upon new client. (the server doesnt have any idea when i cleint is coming either (ie in what interval) When i was testing it a few mins ago, the interval was Sleep(50); Thanks!

                  //Johannes

                  M Offline
                  M Offline
                  Moak
                  wrote on last edited by
                  #9

                  Hej, with this requirements I would use a design like this: - server delivers data when a client connects or sends a (zero) byte - server closes connection only if there's no activity for 3 seconds or network/send error - server closes connection after a persistent connection stayed 60 seconds - server has an upper limit of x incoming sockets (more will be ignored) - clients stay connected and may request data repeatedly - clients close connection in case of network/receive/send error - clients close connection if the received data was not according to protocol - clients automatically reconnect only if they have been sucessfully connected and identified a server in last attempt - clients could do multiple reconnect attempts, but with a delay (e.g. 60 seconds) and an upper limit of x reconnect attempts This should give you a fairly efficient use of TCP and clients do not flood the LAN by error. Since you are using the telnet port on your server you could also use newline as a delimiter, which allows you do test with a telnet client.

                  My Webchat

                  1 Reply Last reply
                  0
                  • M Moak

                    Maybe your are running out of socket objects. Again I am only guessing... it would be helpful to see source code and detailed error codes (WSAGetLastError).

                    My Webchat

                    J Offline
                    J Offline
                    Johpoke
                    wrote on last edited by
                    #10

                    If i call WSAGetLastError() after the connect function fails it returns a 0. I dont know what that suggests though... What other detailed error codes can i find in my program? How would I convert the code i linked to above into a open connection type? Or maybe there is some example code of open TCP connection on codeproject?

                    //Johannes

                    M 1 Reply Last reply
                    0
                    • J Johpoke

                      If i call WSAGetLastError() after the connect function fails it returns a 0. I dont know what that suggests though... What other detailed error codes can i find in my program? How would I convert the code i linked to above into a open connection type? Or maybe there is some example code of open TCP connection on codeproject?

                      //Johannes

                      M Offline
                      M Offline
                      Moak
                      wrote on last edited by
                      #11

                      Johpoke wrote:

                      If i call WSAGetLastError() after the connect function fails it returns a 0. I dont know what that suggests though... What other detailed error codes can i find in my program?

                      Morning Johannes, maybe the error occurs earlier in your source code, check function return codes.

                      Johpoke wrote:

                      How would I convert the code i linked to above into a open connection type?

                      Which code, sorry I don't understand what you mean. /M

                      My Webchat

                      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