Solved

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

Posted on 2013-05-14
3
497 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now