Jump to content
  • Advertisement
Sign in to follow this  
Denisius

Networking problem

This topic is 3911 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have to pieces of code, server and client. Now, the client should be able to connect to my own computer, or any other using the ip address, and then send a string of data, but when i try to do so, it crashes both the client and the server. The client:
// This program will start connect to a server, and send a string of data to it



// Code Block 2.1 - Header Includes
// This code block includes all of the standard Sockets API/Winsock headers
#ifdef WIN32                // windows 95 and above
    #include "winsock2.h"
    #include "Ws2tcpip.h"

#else                       // UNIX/Linux
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <arpa/inet.h>
#endif
// End Code Block 2.1 - Header Includes
   


// Code Block 2.2 - Redefinitions and globals For Cross-Compatibility
#ifdef WIN32                // windows 95 and above
    WSADATA g_wsadata;      // winsock data holder
    #define CloseSocket closesocket
    #define GetSocketError WSAGetLastError
    #define StartSocketLib WSAStartup( MAKEWORD( 2, 2 ), &g_wsadata );
    #define CloseSocketLib WSACleanup();

    #ifndef socklen_t
        typedef int socklen_t;
    #endif
#else                       // Unix/Linux
    #define CloseSocket close
    #define GetSocketError errno
    #define StartSocketLib {}
    #define CloseSocketLib {}
#endif

// End Code Block 2.2 - Redefinitions and globals For Cross-Compatibility



#include <iostream>             // load the iostream library
#include <string.h>
using namespace std;            // use the std namespace

int main()
{
    // BEGIN CODE BLOCK 2.4 - creating a connecting data socket
    int err;                    // for getting errors
    char message[128] = "Hello Internet!";
    char ip[16] = "";
    unsigned long ipaddr;

    // start the socket library
    StartSocketLib;

    cout << "Enter the IP address to connect to: ";
    cin >> ip;

    // convert the IP address.
    ipaddr = inet_addr( ip );
    if( ipaddr == -1 )
    {
        cout << "Error: invalid IP address" << endl;
        return 0;
    }

    // create a socket
    int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    // check if socket was created
    if( sock == -1 )
    {
        cout << "Socket creation error!" << endl;
        return 0;
    }
    cout << "Socket created!" << endl;

    // create a sockaddr_in for connection, on port 4000
    struct sockaddr_in socketaddress;
    socklen_t sa_size = sizeof( struct sockaddr_in );
    socketaddress.sin_family = AF_INET;
    socketaddress.sin_port = htons( 4000 );
    socketaddress.sin_addr.s_addr = ipaddr;
    memset( &(socketaddress.sin_zero), 0, 8 );

    // connect the socket
    err = connect( sock, (struct sockaddr*)&socketaddress, sa_size );

    if( err == -1 )
    {
        cout << "Socket connection error!" << endl;
        return 0;
    }
    cout << "Socket connected!" << endl;
    // END CODE BLOCK 2.4 - creating a connecting data socket

    cout << "sending message: " << message << "..." << endl;

    // send data
    err = send( sock, message, strlen( message ) + 1, 0 );

    if( err == -1 )
    {
        cout << "Socket sending error!" << endl;
        return 0;
    }

    cout << "Data Sent!" << endl;
    
    shutdown( sock, 2 );
    CloseSocket( sock );

    CloseSocketLib;
}

The server:
// This program will start up a server on port 4000, listen for a connection,
// then wait for 128 bytes of data, and print it out as a string.



// Code Block 2.1 - Header Includes
// This code block includes all of the standard Sockets API/Winsock headers
#ifdef WIN32                // windows 95 and above
    #include "winsock2.h"
    #include "Ws2tcpip.h"

#else                       // UNIX/Linux
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <arpa/inet.h>
#endif
// End Code Block 2.1 - Header Includes

#include <memory.h>

// Code Block 2.2 - Redefinitions and globals For Cross-Compatibility
#ifdef WIN32                // windows 95 and above
    WSADATA g_wsadata;      // winsock data holder
    #define CloseSocket closesocket
    #define GetSocketError WSAGetLastError
    #define StartSocketLib WSAStartup( MAKEWORD( 2, 2 ), &g_wsadata );
    #define CloseSocketLib WSACleanup();

    #ifndef socklen_t
        typedef int socklen_t;
    #endif
#else                       // Unix/Linux
    #define CloseSocket close
    #define GetSocketError errno
    #define StartSocketLib {}
    #define CloseSocketLib {}
#endif

// End Code Block 2.2 - Redefinitions and globals For Cross-Compatibility



#include <iostream>             // load the iostream library
using namespace std;            // use the std namespace

int main()
{
    int err;                    // for getting errors


    // start the socket library
    StartSocketLib;

    // BEGIN CODE BLOCK 2.3 - Create a Listening Socket on port 4000
    // create a socket
    int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    // check if socket was created
    if( sock == -1 )
    {
        cout << "Socket creation error!" << endl;
        return 0;
    }
    cout << "Socket created!" << endl;

    // create a sockaddr_in for binding, listening on port 4000
    struct sockaddr_in socketaddress;
    socklen_t sa_size = sizeof( struct sockaddr_in );
    socketaddress.sin_family = AF_INET;
    socketaddress.sin_port = htons( 4000 );
    socketaddress.sin_addr.s_addr = htonl( INADDR_ANY );
    memset( &(socketaddress.sin_zero), 0, 8 );

    // bind the socket
    err = bind( sock, (struct sockaddr*)&socketaddress, sa_size );

    if( err == -1 )
    {
        cout << "Socket binding error!" << endl;
        return 0;
    }
    cout << "Socket bound!" << endl;


    // listen on the socket
    err = listen( sock, 16 );

    if( err == -1 )
    {
        cout << "Socket listening error!" << endl;
        return 0;
    }
    cout << "Socket listening, waiting for connection..." << endl;
    // END CODE BLOCK 2.3 - Create a Listening Socket on port 4000


    // wait for an incomming connection now
    int datasock;
    datasock = accept( sock, (struct sockaddr*)&socketaddress, &sa_size );

    if( datasock == -1 )
    {
        cout << "Socket accepting error!" << endl;
        return 0;
    }
    cout << "Socket accepted, waiting for data..." << endl;


    // receive data
    char buffer[128];
    err = recv( datasock, buffer, 128, 0 );

    if( err == -1 )
    {
        cout << "Socket receiving error!" << endl;
        return 0;
    }

    cout << "Data received:" << endl;
    cout << buffer << endl;
    
    shutdown( datasock, 2 );
    CloseSocket( datasock );

    shutdown( sock, 2 );
    CloseSocket( sock );

    CloseSocketLib;
}

I compiled it on VS2005, and had two warning, on both the client and the server.
Quote:
------ Build started: Project: Demo02-01, Configuration: Release Win32 ------ Compiling... Demo02-01.cpp .\Demo02-01.cpp(63) : warning C4244: 'initializing' : conversion from 'SOCKET' to 'int', possible loss of data .\Demo02-01.cpp(106) : warning C4244: '=' : conversion from 'SOCKET' to 'int', possible loss of data Linking... Embedding manifest... Build log was saved at "file://c:\MUD Game Programming\Demos\Chapter02\Demo02-01\Release\BuildLog.htm" Demo02-01 - 0 error(s), 2 warning(s) ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
Advertisement
Right. On unixlikes, socket is an int. In winsock it's not (though is convertible to one iirc). Either way, the build warnings are just warnings and not likely the cause of the failure.

Load up the debugger, see where it fails and why.

Share this post


Link to post
Share on other sites
The debugging of the server:

Quote:

'Demo02-01.exe': Loaded 'C:\MUD Game Programming\Demos\Chapter02\Demo02-01\Release\Demo02-01.exe', Symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\mswsock.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\hnetcfg.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\lpk.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\usp10.dll', No symbols loaded.
'Demo02-01.exe': Loaded 'C:\WINDOWS\system32\wshtcpip.dll', No symbols loaded.
'Demo02-01.exe': Unloaded 'C:\WINDOWS\system32\wshtcpip.dll'
'Demo02-01.exe': Unloaded 'C:\WINDOWS\system32\hnetcfg.dll'
The program '[1324] Demo02-01.exe: Native' has exited with code 0 (0x0).

Share this post


Link to post
Share on other sites
Try ctrl+F5 to launch the program. Its not crashing it is just closing before you are reading the text.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!