Solved

Socket question - "active open" vs "passive open"

Posted on 2013-05-14
3
509 Views
Last Modified: 2013-05-15
I am new to sockets.  Trying to communicate with a peice of equipment whose documentation is not very good.  It is set up to use multiple socket connections (one for 'connectivity heartbeat', another for 'main control', another for 'image transmission', etc).  All but one of these is working fine.  For all these sockets, I do the following to configure a socket and make it do what I would call a "blocking listen":

    WSADATA wsaData;
    SOCKET s, S;
    SOCKADDR_IN addr, ADDR;
 
    WSAStartup(MAKEWORD(1, 1), &wsaData);

    s = socket(AF_INET, SOCK_STREAM, 0);

    addr.sin_family = AF_INET;
    addr.sin_port = htons(ThePort);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(s, (SOCKADDR *)&addr, sizeof(addr);
 
    listen(s, 1);

    S = accept(s, (SOCKADDR *)&ADDR, &len);

    int len = recv(S, lpszBuffer, 4096, 0);

It blocks at the "recv" call until something comes in.  Thats all fine.  But I can't get the one last socket to work (the "image transmission" socket).  Finally, I noticed that in the crappy documentation, it shows the other sockets (that I have working) as "passive open" for my side, but the one I'm having trouble with they show as "active open" from my side.  So, I'm not sure what is meant, but I'm guessing it means I should be the 'client' instead of the 'server' for this connection.   It is a connection on which I am to receive a bunch of data, and (apparently) I needn't send out anything -- so I thought that my normal sequence (code above) to open the socket and listen for data would be correct.  But, nothing ever comes, and it looks like I never get past the "accept" call.  

So.... is there a different way I should set up this socket?  Looking at some other socket code, it looks like I see the same preamble as in my code above, but in place of the "bind" call, just do a "connect" call at that point.  But, I don't really know what I'm doing, so I'm hoping a socket expert might have an idea for me!
0
Comment
Question by:tullhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39167269
Going just by what you have written above, I would say that you guess in probably on the mark. And so yes, you will want to "connect" the socket rather than bind & accept. The main difference is that instead of using the INADDR_ANY address, you will need to know the address of the piece of equipment. Now in most cases this should be easy, just get it from your ADDR struct (the one you passed to "accept()", it fills the struct with the address info of the remote end that is connecting to you). So you probably just need to change the port number within ADDR, to the port that you have to connect to for "image transmission". So something along the lines of (add this after the code you have above, and also add appropriate error checking, etc)...
SOCKET imageSocket = socket(AF_INET, SOCK_STREAM, 0);
ADDR.sin_port = htons(ImagePort);
connect(imageSocket, (SOCKADDR *)&ADDR, len);

Open in new window

If the "connect()" call succeeds, you can now call "recv()" etc on the imageSocket to retrieve any data that the equipment pushes back to you.
0
 

Author Closing Comment

by:tullhead
ID: 39169849
It works!!
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39170032
It works!!
That's good, glad I could help! :)
0

Featured Post

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Embarcadero WebBroker REST server endpoint parameters 2 96
max float value 3 59
Application timeout 4 42
Understanding Extended-Access List 6 35
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question