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. Sockets and Port number

Sockets and Port number

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 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.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi I have a conceptual question that relates with socket programming. I know that socket number = IP number + Port number and A TCP packet contains four numbers, source IP source port, destination IP destination port. How am I gonna picture port number and applications while socket programming. Is it problem to have two or more applications use same port? or must every application has distinct port number? Is it problem to have two or more clients making communication over same port? or must every client has distinct port number?

    B A 3 Replies Last reply
    0
    • S sawerr

      Hi I have a conceptual question that relates with socket programming. I know that socket number = IP number + Port number and A TCP packet contains four numbers, source IP source port, destination IP destination port. How am I gonna picture port number and applications while socket programming. Is it problem to have two or more applications use same port? or must every application has distinct port number? Is it problem to have two or more clients making communication over same port? or must every client has distinct port number?

      B Offline
      B Offline
      Bill SerGio The Infomercial King
      wrote on last edited by
      #2

      Your question is an excellent question. Most books and articles that talk about sockets FAIL to explain this and question goes to the heart of all real world examples of writing socket code. I will answer your question with a real world example that I worked on recently. Let's say that you want to show stock data about a stock or group of stocks that changes every second in a web page and that the source of this data is some TCP server that you would connect to and receive a callback from every second in the form of strings. So let's say the user select a stock symbol like "IBM" and wants to see the price and volume traded of IBM stock in their web browser every second. Another user might want to see the value of a different stock symbol like "ACL" for Alcon, Inc. How many IP addresses and Ports we would need? As you know we must use web Sockets to connect from a web page to a TCP Server--BUT there is another problem--none of the stock alert data servers have implemented the Web Sockets protocol handshake so we will need a web socket proxy between the web page and the Data Server. There are 2 samples of web Sockets Servers posted here on CodeProject that we can use. If we put the ip address and port INSIDE the web page and connect to a Web Sockets Server then that web sockets server must spin off a thread that will in side each thread open a TCP connection to Data Server on a given ip address and port. The data from the thread received from the data server will be sent to the web socket server and then back to the client web browser that requested the data and NOT to any other browsers. Each browser on connecting to the web socket server passes in a "settings string" like say the stock symbol so that 2,000 different web browsers would see different data for 2,000 different stock symbols where each browser would have a datagrid displaying say hundreds of rows of stock data per second for that one symbol they requested. When you set up this scenario--trial and error are good ways to learn socket programming--you will quickly realize that the amount of data that the web sockets data has to return is huge and the Data Server is passing the data for all 2,000 connected web browsers back to one place, namely the web sockets server--you could write a queue manager but that can't handle this kind of volume. Here is the solution and answer to you question. You create a web service to return different port numbers to the web page. The web page sits on the server along with your web sockets server so the web page will f

      1 Reply Last reply
      0
      • S sawerr

        Hi I have a conceptual question that relates with socket programming. I know that socket number = IP number + Port number and A TCP packet contains four numbers, source IP source port, destination IP destination port. How am I gonna picture port number and applications while socket programming. Is it problem to have two or more applications use same port? or must every application has distinct port number? Is it problem to have two or more clients making communication over same port? or must every client has distinct port number?

        A Offline
        A Offline
        Andrew Brock
        wrote on last edited by
        #3

        Your knowledge is correct from a simplistic point of view. Perhaps you should get some first hand experience with Wireshark[^]. Sniff a session, such as a opening the google homepage. For a TCP session to work the following must happen: 1. The server starts up and listens (binds) on a port. No other programs can be listening for data on this port (although they can send from it which is a different session) This port is almost always pre-determined (like 80 for HTTP) but if you specify 0 the OS will give a random port which the client must be sent by some other means (like an email) before connecting 2. The client connects to the IP of the server and the port from part 1. If you looked at Wireshark, this does the SYN, SYN/ACK and ACK that starts the session 3. Send/receive from both server and client. 4. Shutdown the socket when done with it from both ends, both server and client must shutdown both send and receive directions when finished. If you looked at Wireshark, this does the FIN/ACK and ACK once from each end. 5. Close the socket. This frees the system handle on the socket

        S 1 Reply Last reply
        0
        • A Andrew Brock

          Your knowledge is correct from a simplistic point of view. Perhaps you should get some first hand experience with Wireshark[^]. Sniff a session, such as a opening the google homepage. For a TCP session to work the following must happen: 1. The server starts up and listens (binds) on a port. No other programs can be listening for data on this port (although they can send from it which is a different session) This port is almost always pre-determined (like 80 for HTTP) but if you specify 0 the OS will give a random port which the client must be sent by some other means (like an email) before connecting 2. The client connects to the IP of the server and the port from part 1. If you looked at Wireshark, this does the SYN, SYN/ACK and ACK that starts the session 3. Send/receive from both server and client. 4. Shutdown the socket when done with it from both ends, both server and client must shutdown both send and receive directions when finished. If you looked at Wireshark, this does the FIN/ACK and ACK once from each end. 5. Close the socket. This frees the system handle on the socket

          S Offline
          S Offline
          sawerr
          wrote on last edited by
          #4

          @TV Mogul, thanks for the explanation. But I think my knowledge doesn't let me to understand it completely. In your answer, port numbers are used for clients. @Andrew Brock thank you too. We all know that, IP address is client specific. Every client has a distinct IP address. - If I am right, port number is application specific. If one application grabs a port no other application can use it. So, for example,that means one computer mustn't have 2 or more web server that listening port 80. Right? - But also a lot of clients can connect same port. But sometimes we need to serve them from different ports, ( I think TV Mogul's definition is about that) Which conditions should i make a decision that I need a new socket?

          A 1 Reply Last reply
          0
          • S sawerr

            @TV Mogul, thanks for the explanation. But I think my knowledge doesn't let me to understand it completely. In your answer, port numbers are used for clients. @Andrew Brock thank you too. We all know that, IP address is client specific. Every client has a distinct IP address. - If I am right, port number is application specific. If one application grabs a port no other application can use it. So, for example,that means one computer mustn't have 2 or more web server that listening port 80. Right? - But also a lot of clients can connect same port. But sometimes we need to serve them from different ports, ( I think TV Mogul's definition is about that) Which conditions should i make a decision that I need a new socket?

            A Offline
            A Offline
            Andrew Brock
            wrote on last edited by
            #5

            sawerr wrote:

            If one application grabs a port no other application can use it.

            Application specific would imply that each application could have its own set of ports. I think you meant to say "System Specific".

            sawerr wrote:

            So, for example,that means one computer mustn't have 2 or more web server that listening port 80. Right?

            That is correct. This is called System Specific. 1 per system.

            sawerr wrote:

            But also a lot of clients can connect same port.

            Yes, me and you can both connect to www.google.com on port 80 at the same time. (ignore the fact that we would almost certainly get different servers altogether). When a server is listening, it has the option of setting the maximum number of sessions. When you look at some code for starting a server you have something like this:

            SOCKET sConnection = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP); //Create a TCP stream socket with any address type (IPv4, IPv6, ...)
            //pAddress = get listening address, usually 0.0.0.0
            bind(sConnection, pAddress->ai_addr, pAddress->ai_addrlen);
            listen(sConnection, SOMAXCONN);
            for (;;) {
            SOCKADDR saClient;
            DWORD nClientAddrLen = sizeof(saClient);
            SOCKET sClient = accept(sConnection, &saClient, &nClientAddrLen);
            CreateThread(); //Create a new thread to handle the new connection
            //Now wait for the next connection in the new loop
            }

            1 Reply Last reply
            0
            • S sawerr

              Hi I have a conceptual question that relates with socket programming. I know that socket number = IP number + Port number and A TCP packet contains four numbers, source IP source port, destination IP destination port. How am I gonna picture port number and applications while socket programming. Is it problem to have two or more applications use same port? or must every application has distinct port number? Is it problem to have two or more clients making communication over same port? or must every client has distinct port number?

              B Offline
              B Offline
              Bill SerGio The Infomercial King
              wrote on last edited by
              #6

              Andrew Brock gave you a textbook answer. What I am trying to explain is that teh textbooks are WRONG and all the design model examples are wrong in that they only deal with a single client connecting or multiple clients where the data is the same for each client and only a little data is passed. The simple answer is this: The ip address can be same BUT the port number must change for each client that connects if you want the system to be practical. This means that you create a console app with a minimum amount of memory as the server and launch a new instance of this for each client that connects so that each client is connecting on a different port number. For example, I wrote a camera wall recently for the U.S. government and their specs stated that you may NOT use threads--that you must create a new instance of the server for each client connection. The reason for this is so that if one client goes down it will not take the whole system down. Each client uses the same ip address but has a unique port number. Andrew Brock's answer is a textbook answer which doesn't apply to teh real world. Get real Anrew!

              http://www.KabbalahCode.com

              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