WinSock

This is a discussion on WinSock within the C++ Programming forums, part of the General Programming Boards category; Hi! This time I tried to learn winsock. The problem is- I can't receive anything. Here's my code: Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    WinSock

    Hi! This time I tried to learn winsock. The problem is- I can't receive anything. Here's my code:
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    #pragma comment (lib, "wsock32.lib")
    #define NETWORK_ERROR -1
    #define NETWORK_OK     0
    
    int host();
    int join();
    int cinis;
    
    int main()
    {
    	WSADATA wsaData;
    WORD version = MAKEWORD( 1, 1 );
    
    int error = WSAStartup( version, &wsaData );
    	cout<<"1. Host\n2. Join\n";
        cin>>cinis;
    	cin.ignore();
    	if(cinis==1)
    	host();
    	else if(cinis==2)
    	join();
    	else
    	return 0;
    	cin.get();
    }
    
    int host()
    {
    SOCKET server;
    server = socket( AF_INET, SOCK_STREAM, 0 );
    struct sockaddr_in sin;
    
    memset( &sin, 0, sizeof sin );
    
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(8888);
    
    if ( bind(server, (LPSOCKADDR)&sin, sizeof(struct sockaddr)) == SOCKET_ERROR )
    {
        cout<<"Error";
    	std::cin.get();
    }
    while ( listen( server, SOMAXCONN ) == SOCKET_ERROR )
    {
    	cout<<"Connecting";
    	system("cls");
    }
    SOCKET client;
    int length;
    length = sizeof sin;
    client = accept( server, (LPSOCKADDR)&sin, &length );
    char * msg= "yay";
    send(client, msg, 10, 0);
    closesocket( server );
    closesocket(client);
    WSACleanup();
    return 1;
    }
    
    int join()
    {
    SOCKET client;
    
    client = socket( AF_INET, SOCK_STREAM, 0 );
    /*struct hostent*/ LPHOSTENT host;
    in_addr iaHost;
    iaHost.s_addr = inet_addr("127.0.0.1");
    host = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
    struct sockaddr_in sin;
    memset( &sin, 0, sizeof sin );
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = ((struct in_addr *)(host->h_addr))->s_addr;
    sin.sin_port = htons( 21 );
    if ( connect( client, (LPSOCKADDR)&sin, sizeof sin ) == SOCKET_ERROR )
    {
        /* could not connect to server */
    	cout<<"Can't connect";
    }
    else cout<<"connected!";
    char * msg1="";
    recv(client, msg1, 10, 0);
    cout<<"received: "<<msg1;
    closesocket(client);
    WSACleanup();
    return 1;
    }
    To test, I just open two instances of my program and try to connect between them, is that correct? Also, the program hangs up when I try to host. I used this and this tutorial. Any assistance or links to better tutorials (or am I the only problem?) appreciated.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > char * msg1="";
    > recv(client, msg1, 10, 0);
    1. msg1 isn't pointing at memory you can write to.
    2. If you could write to it, it couldn't store a string since it's only got room for the \0
    3. You don't check the return result of recv
    4. You don't append a \0 to make the result a proper c-style string

    Code:
    char msg[10];
    n = recv( client, msg, sizeof(msg) - 1, 0 );
    if ( n > 0 ) {
      msg[n] = '\0';
      cout << msg;
    }
    > WSACleanup();
    So that you don't lose track of them, put the WSAStartup and WSACleanup calls in the same function, then call to the appropriate. If the called functions have various error returns (which they should have), you don't have to worry about getting WSACleanup called in all the right places.

    > char * msg= "yay";
    > send(client, msg, 10, 0);
    1. Use say strlen() to determine the amount of data to send
    2. Check the return result.

    Finally, your indentation needs work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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