Thread: I olny get "4 bytes" of the memory!???

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    76

    I olny get "4 bytes" of the memory!???

    I've taken alot of advice and help and have gotten this far...




    Sender/Reader:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <socket.cpp>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    int size = 0;
    	
    	ServerSocket sok;
    	sok.StartHosting(6500);
    	
    	ifstream bob("Hi.bmp",ios::in|ios::binary);
    
    	
    	bob.seekg(0, ios::end);
    
    	size = bob.tellg();
    	
    	cout<<size<<"\n";
    
    	bob.seekg(0, ios::beg);
    	
    	char memblock[size];
    	
    	bob.read(memblock,size);
    	
    	ofstream joe("Bye.bmp",ios::out|ios::binary);
    
    	
    	sok.SendData(memblock);
    	
    		joe.write(memblock,size);
    	
    	system("PAUSE");
    	return EXIT_SUCCESS;
    }








    Reciver/Writer:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <socket.cpp>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	ClientSocket sok;
    	sok.ConnectToServer("127.0.0.1",6500);
    	
    	char *memblock;
    	sok.RecvData(memblock,sizeof(memblock));
    	
    	ofstream joe("Bye.bmp",ios::out|ios::binary);
    	joe.write(memblock,sizeof(memblock));
    	
    	cout<<sizeof(memblock);
    	
    	system("PAUSE");
    	return EXIT_SUCCESS;
    }

    SOCKET.CPP CODE:

    Code:
    //Socket.cpp
    #include "Socket.h"
    
    Socket::Socket()
    {
        if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
        {
            cerr<<"Socket Initialization: Error with WSAStartup\n";
            system("pause");
            WSACleanup();
            exit(10);
        }
    
        //Create a socket
        mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    
        if ( mySocket == INVALID_SOCKET )
        {
            cerr<<"Socket Initialization: Error creating socket"<<endl;
            system("pause");
            WSACleanup();
            exit(11);
        }
    
        myBackup = mySocket;
    }
    
    Socket::~Socket()
    {
        WSACleanup();
    }
    
    bool Socket::SendData( char *buffer )
    {
        send( mySocket, buffer, strlen( buffer ), 0 );
        return true;
    }
    
    bool Socket::RecvData( char *buffer, int size )
    {
        int i = recv( mySocket, buffer, size, 0 );
        buffer[i] = '\0';
        return true;
    }
    
    void Socket::CloseConnection()
    {
        //cout<<"CLOSE CONNECTION"<<endl;
        closesocket( mySocket );
        mySocket = myBackup;
    }
    
    void Socket::GetAndSendMessage()
    {
        char message[STRLEN];
        cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
        //cout<<"Send > ";
        cin.get( message, STRLEN );
        SendData( message );
    }
    
    void ServerSocket::StartHosting( int port )
    {
         Bind( port );
         Listen();
    }
    
    void ServerSocket::Listen()
    {
        //cout<<"LISTEN FOR CLIENT..."<<endl;
        
        if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
        {
            cerr<<"ServerSocket: Error listening on socket\n";
            system("pause");
            WSACleanup();
            exit(15);
        }
        
        //cout<<"ACCEPT CONNECTION..."<<endl;
        
        acceptSocket = accept( myBackup, NULL, NULL );
        while ( acceptSocket == SOCKET_ERROR )
        {
            acceptSocket = accept( myBackup, NULL, NULL );
        }
        mySocket = acceptSocket;
    }
    
    void ServerSocket::Bind( int port )
    {
        myAddress.sin_family = AF_INET;
        myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
        myAddress.sin_port = htons( port );
        
        //cout<<"BIND TO PORT "<<port<<endl;
    
        if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
        {
            cerr<<"ServerSocket: Failed to connect\n";
            system("pause");
            WSACleanup();
            exit(14);
        }
    }
    
    void ClientSocket::ConnectToServer( const char *ipAddress, int port )
    {
        myAddress.sin_family = AF_INET;
        myAddress.sin_addr.s_addr = inet_addr( ipAddress );
        myAddress.sin_port = htons( port );
        
        //cout<<"CONNECTED"<<endl;
    
        if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
        {
            cerr<<"ClientSocket: Failed to connect\n";
            system("pause");
            WSACleanup();
            exit(13);
        } 
    }




    Still olny writes 4 bytes of data. (nothing?)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you expect sizeof(memblock) to be something other than 4? If so, why?

    (For that matter, do you expect to have actual memory being pointed to by memblock? If so, why?)

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    To get the size idk actually how, if not that.

    Could I just put 100000 in all the "size" areas. My test file is pretty small.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think you've quite got the point. Your receiver/writer, as it stands, can currently write a maximum of zero (0) bytes. You have nowhere to put any data that is received. Once you acquire memory to put the data into, then you will know what the size of the data is.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're also trying to send binary data using strlen. That would most likely cause only a fraction of the data to be transmitted.
    Consider what happens if the first character is the nul character, i.e. '\0'. What will strlen tell you the number of bytes to send is?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Is this what the memset(name,letter,number); function does?
    Last edited by azjherben; 05-29-2009 at 05:43 AM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by azjherben View Post
    Is this what the memset(chesse,cake); function does?
    What do you mean by "this"?

    If you want to know what a function does, you can look it up online. If you can't understand what the documentation tells you or whether or not it is the thing you're needing, then you're probably not competent enough to use it yet.
    Look for examples of it being used in other people's code, or look for code that does the kind of thing you need to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Could anyone actually suggest what I should do?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by azjherben View Post
    Could anyone actually suggest what I should do?
    Obtain sufficient memory to place your text somewhere (using one of the alloc functions, I suppose) and then use that memory to write to.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    alloc?

    I'll google it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. available memory from task manager
    By George2 in forum Tech Board
    Replies: 10
    Last Post: 01-18-2008, 02:32 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM