Winsock help?

This is a discussion on Winsock help? within the C++ Programming forums, part of the General Programming Boards category; I am learning winsock and I have a problem. Here's the code for my server: #include <winsock.h> #include <iostream.h> #include ...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Winsock help?

    I am learning winsock and I have a problem. Here's the code for my server:


    #include <winsock.h>
    #include <iostream.h>
    #include <stdio.h>
    #include <conio.c>

    class data
    {
    public:
    int write(SOCKET s, char szBuffer[1000], int len, int flags);
    int read(SOCKET s, char szBuffer[1000], int len, int flags);
    int end(SOCKET s, int how);
    };

    int data::write(SOCKET s, char szBuffer[1000], int len, int flags)
    {
    if(send(s, szBuffer, len, flags) == -1)
    {
    cout << "\n\nError sending data...";
    WSACleanup();
    }
    }
    int data::read(SOCKET s, char szBuffer[1000], int len, int flags)
    {
    if(recv(s, szBuffer, len, flags) == -1)
    {
    cout << "\n\nError receiving data...";
    WSACleanup();
    }
    }
    int data::end(SOCKET s, int how)
    {
    shutdown(s, how);
    }

    int main()
    {
    data wsdat;

    char buff[1000];
    int len;
    int tmpSock;

    SOCKET sockfd;
    SOCKADDR_IN saddr;
    WSADATA wsaDat;

    WSAStartup(0x101, &wsaDat);

    sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);

    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(20123);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if(bind(sockfd, (struct sockaddr *) &saddr, sizeof(struct sockaddr)) == -1)
    {
    cout << "\n\nError binding socket..Error #" << WSAGetLastError();
    WSACleanup();
    getch();
    }

    while(tmpSock == SOCKET_ERROR)
    {
    tmpSock = listen(sockfd, 10);
    }

    sockfd = tmpSock;

    if(connect(sockfd, (struct sockaddr *) &saddr, sizeof(struct sockaddr)) == -1)
    {
    cout << "\n\nError connecting to socket...Error #" << WSAGetLastError();
    WSACleanup();
    getch();
    }

    while(tmpSock == SOCKET_ERROR)
    {
    tmpSock = accept(sockfd, NULL, NULL);
    }

    sockfd = tmpSock;

    cout << "\n\nConnection accepted...\n\n";

    while(tmpSock == SOCKET_ERROR)
    {
    tmpSock = wsdat.read(sockfd, buff, 1000, 0);
    }

    cout << buff;

    wsdat.end(sockfd, 3);

    getch();

    return 0;
    }


    It won't accept the connection from my client. It just waits a certain amount of time and says my error message for connect:

    "Error connecting to socket...Error #10061"


    What do I do?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    what you should do is try posting this on the windows programming forum. should have better luck there.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I looked up the winsock error code 10061, and it means:

    Connection refused.

    Maybe someone else here could explain why.
    I guess you could try a different port.
    Last edited by swoopy; 02-06-2002 at 04:04 PM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    >>while(tmpSock == SOCKET_ERROR)
    {
    tmpSock = listen(sockfd, 10);
    }

    sockfd = tmpSock;

    listen does not return a SOCKET but an int. It returns an error code or zero if OK.
    So this will always fail

    >>if(connect(sockfd, (struct sockaddr *) &saddr, sizeof(struct sockaddr)) == -1)
    as sockfd is either zero or an error code.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

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