Thread: C++ Winsock with VB

  1. #1
    Grausherra
    Guest

    C++ Winsock with VB

    Ok, I've recently started experimenting with winsock in C++. I had intended to write a server-type program in C++, and then use MSVB 6.0 for the client side. I can set up a socket, open it, and accept connection from a C++ server to a C++ client. However, If I write the server in C++ and try to conenct using a client program I wrote in VB onto the same port, the connection is not found. My understanding is that winsock allowed 2 computers to communicate regardless of the underlying language. Here I will post my code in both languages. I understand that this is not a VB board, was just hoping someone might be able to shed some light onto the situation. BTW the code is not all my own, so I make no claims to ownership of it.


    C++ Server:

    //Assumes:you MUST add the "mpr.lib wsock32.lib "


    #include <iostream.h>
    #include <winsock.h>
    #include <string.h>
    // Prototypes



    /////////////////////
    // Functions
    /////////////////////


    // mian Function

    int main()
    {
    while(true)
    {


    WSADATA WsaDat; //Declares what is necessary to use winsock
    int ip1(127), ip2(0), ip3(0), ip4(1); //Holders for IP address, 127.0.0.1 used for simplicity
    if (WSAStartup(MAKEWORD(1,1), &WsaDat) != 0) //Initiallizes the WSA
    {
    cout << "WSA initialization failed.";
    cout << "Closing. . .";
    Sleep(MAKEWORD(10,30));
    return 0;
    }


    SOCKET mysocket; //Our Handy Dandy Socket
    mysocket = socket(AF_INET, SOCK_STREAM, 0); //Sets the properties of our socket
    if (mysocket == INVALID_SOCKET) //Checks to see if it created correctly
    {
    cout << "Socket creation failed!";
    cout << "Closing. . .";
    Sleep(MAKEWORD(10,30));
    return 0;
    }

    SOCKADDR_IN SockAddr; //this keeps track of the socket's address info
    SockAddr.sin_port = 10119;
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.S_un.S_un_b.s_b1 = ip1;
    SockAddr.sin_addr.S_un.S_un_b.s_b2 = ip2;
    SockAddr.sin_addr.S_un.S_un_b.s_b3 = ip3;
    SockAddr.sin_addr.S_un.S_un_b.s_b4 = ip4;

    char Servername[20], CCommand[20]; // self-explanatory

    strcpy(Servername,"Dynastic1");

    // bind to IP address. . .
    if (bind(mysocket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
    {
    cout << "Attempt to bind failed" << endl;
    cout << "Closing. . .";
    Sleep(MAKEWORD(10,30));
    return 0;
    }


    cout << "Waiting for client. . ." << endl;
    //go into "Listen" mode. . .
    listen(mysocket, 1);
    //when request received to connect, accept it
    SOCKET TempSock = SOCKET_ERROR;
    while (TempSock == SOCKET_ERROR)
    {
    TempSock = accept(mysocket, NULL, NULL);
    }
    mysocket = TempSock;
    //send your screen name to the client
    cout << "Client Found. Sending data" << endl;
    send(mysocket, Servername, sizeof(Servername), 0);
    bool acomplish;
    acomplish = false;
    while(acomplish == false)
    {
    int WaitforCommand = SOCKET_ERROR;
    while (WaitforCommand == SOCKET_ERROR)
    {
    WaitforCommand = recv(mysocket, CCommand, 20, 0);
    if ((WaitforCommand == 0)||(WaitforCommand == WSAECONNRESET))
    {
    cout << "Connection closed at the other end while receiving screen name! RATS!" << endl;
    cout << "Closing. . .";
    Sleep(MAKEWORD(10,30));
    return 0;
    }

    }
    //////////////////////////////////////////////////
    ///And here is where all hte coding takes place, for debug only///
    //////////////////////////////////////////////////
    if ((strcmp(CCommand, "bob")) ==0)
    {
    cout << "They sent bob" << endl << endl;
    acomplish = true;
    }
    else
    cout << "Didnt send bob, so thats it" << endl;
    acomplish = true;

    }



    closesocket(mysocket);
    }

    // end prog
    cout << "Closing. . .";
    Sleep(MAKEWORD(10,30));
    return 0;

    }


    ************************************************
    VB Client, in the formload with only a single component, a winsock activeX with name sckClient:

    Private Sub Form_Load()
    sckClient.RemoteHost = "127.0.0.1"
    sckClient.RemotePort = 10119
    sckClient.Connect

    End Sub

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    And this code even compiles??

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Re: C++ Winsock with VB

    Originally posted by Grausherra

    SOCKADDR_IN SockAddr;
    SockAddr.sin_port = 10119;
    Try doing this ;

    Code:
      SockAddr.sin_port = htons(10119);
    Could also try reading through this tutorial on winsock for c++ http://energon.home.mindspring.com/t..._tutorial.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  3. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  4. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM