CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2007
    Posts
    119

    Question Receive messages in a dialog based application through socket

    I am using following code to send message to remote machine through sockets, I am calling mentioned code when a button of MFC dialog based app is clicked and it is working fine. But I don't know how to receive messages from remote machine through sockets.
    Can anyone please guide me how to receive messages in dialog based application through socket?

    WSADATA w; //Winsock startup info
    SOCKADDR_IN target; //Information about host

    int error = WSAStartup (0x0202, &w); // Fill in WSA info

    if (error)
    return 0;

    m_sckt = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket
    if (m_sckt == INVALID_SOCKET)
    {
    return 0;
    }

    target.sin_family = AF_INET; // address family Internet
    target.sin_port = htons (PortNo); // set server’s port number
    target.sin_addr.s_addr = hostname; // set server’s IP
    //Try connecting...
    if (connect(m_sckt, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR) //Try binding
    { // error
    return 0;
    }
    send(m_sckt, "Test", 5, 0);

    Thanks,
    Mushq

  2. #2
    Join Date
    Feb 2008
    Location
    India
    Posts
    28

    Re: Receive messages in a dialog based application through socket

    If your remote machine sends messages to your machine, then configure same port on both side(in your dialog based application). make a server client relation between both machine.
    And to recieve messages use recv() method.
    Code:
    int recv( SOCKET s,  char* buf,  int len,  int flags);
    Click here for detail: http://msdn.microsoft.com/en-us/libr...21(VS.85).aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured