WinSock

This is a discussion on WinSock within the C++ Programming forums, part of the General Programming Boards category; Good Day! When I'm clicking first time on button on my MFC project, I'm receiving host's time. But next clicks ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    15

    WinSock

    Good Day!
    When I'm clicking first time on button on my MFC project, I'm receiving host's time. But next clicks don't change time in my textbox. What should I do in oreder to receive correct time every time?
    Thanks!
    Here code:
    Code:
    void CS3Dlg::OnButton1() 
    {
        // TODO: Add your control notification handler code here
        SOCKET s=socket(AF_INET,SOCK_STREAM,0);
        sockaddr_in a1, a2;
        a1.sin_family=AF_INET;
        a1.sin_port=htons(5001);
        a1.sin_addr.s_addr=htonl(INADDR_ANY);
        if(bind(s,(LPSOCKADDR) &a1, sizeof(a1))==SOCKET_ERROR)
        {
            WSACleanup();
            return;
        }
        a2.sin_family=AF_INET;
        a2.sin_port=htons(13);
        a2.sin_addr.s_addr=inet_addr("192.168.120.100");
        if(connect(s,(LPSOCKADDR) &a2,sizeof(a2))==SOCKET_ERROR)
        {
            WSACleanup();
            return;
        }
        char buf[80];
        recv(s,buf,sizeof(buf),0);
        m_s.Format(_T("%s"), buf);
        UpdateData(FALSE);
        WSACleanup();
    }
    Last edited by Salem; 07-27-2004 at 07:44 AM. Reason: Code tagged - please remember to use them in future

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Welcome to the forums!

    We don't bind() a client socket. We let the operating system auto-bind() to an available port when we call connect(). This is for the reason you are experiencing. Only one socket can be bound to a port and subsequent attempts to bind() to the same port will fail. Also, we need to closesocket() our sockets when we are finished with them. Finally, you should not call WSACleanup on every button press. WSAStartup() should be called only once at program startup and WSACleanup() should be called only once when the program is finishing.

    [edit]
    We also need to deal with the fact that input from the network may not be null terminated. Therefore, we have to do it explicitly.
    This is typically done something like:

    Code:
    char buf[100];
    int ret = recv(s,buf,sizeof(buf) - 1,0);
    if (ret != SOCKET_ERROR)
    {
        buf[ret] = '\0';
        // use buffer safely
    }
    I'll leave it up to you to add that to your code.
    [/edit]

    So, delete the red code and add the green:
    Code:
    void CS3Dlg::OnButton1() 
    {
        // TODO: Add your control notification handler code here
        SOCKET s=socket(AF_INET,SOCK_STREAM,0);
        sockaddr_in a1, a2;
        a1.sin_family=AF_INET;
        a1.sin_port=htons(5001);
        a1.sin_addr.s_addr=htonl(INADDR_ANY);
        if(bind(s,(LPSOCKADDR) &a1, sizeof(a1))==SOCKET_ERROR)
        {
            WSACleanup();
            return;
        }
        a2.sin_family=AF_INET;
        a2.sin_port=htons(13);
        a2.sin_addr.s_addr=inet_addr("192.168.120.100");
        if(connect(s,(LPSOCKADDR) &a2,sizeof(a2))==SOCKET_ERROR)
        {
            WSACleanup();
            closesocket(s);
            return;
        }
        char buf[80];
        recv(s,buf,sizeof(buf),0);
        closesocket(s);
        m_s.Format(_T("%s"), buf);
        UpdateData(FALSE);
        WSACleanup();
    }
    Last edited by anonytmouse; 07-27-2004 at 09:35 AM.

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. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 05:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 01:39 PM
  4. 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
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21