• 0

C++ Socket Programming


Question

After doing a Google search and being unsatisfied with the results, I figured I'd come here to ask this question.

I'm new to Socket Programming in C++. Basically, I'm making a very basic Client application that talks to a server and listens for a response. Is it possible to create multiple sockets in a program, one socket that actually talks to a server and the other that receives its response?

If so, would the following block of code be needed each time I initialize a new socket, or can it just be initialized once in the program?

   WSADATA WsaData;
    if( WSAStartup( MAKEWORD( 1, 1 ), &WsaData ) != 0 )
    {
        printf( "Error!");
        exit( 0 );
    }

Remember, I'm quite new to this! Thanks :)

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

That chunk of code is only needed once.

And no, I don't believe you can do that. You simply create a socket and connect to an address with it. You send and receive through you. I recommend you do this async, which allows the rest of your application to continue execution until data is received.

Link to comment
Share on other sites

  • 0

Like vcv said, WSAStartup is only needed once in your app.

It initialize the tcp/ip functions so u can use them.

You could use two sockets to send and receive, but there no advantage to do so, since your doing a simple client. I'd doubt you'd want to send data at the same time you'd receive data....

And I recommend not using async sockets for that. That would only complicate this more than you need it!

Create a socket, connect it to the remote endpoint, send() something with it, then recv() the answer and parse it, and so on, until ur done with the connection.

Link to comment
Share on other sites

  • 0
Looks like a great guide... I wish I would have known about it 24 hours ago. But at least I can learn from my mistakes now.

Thanks :)

Haha yeah no worries. Best way to learn is by having a go and making mistakes.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.