Über Winsock Datei senden funktioniert nicht



  • Ich überlege jetzt schon eien weile und komm einfach nicht auf den fehler in meinem programm...
    ich habe einen server der eine datei annimmt und diese speichert.
    und ich habe einen client der sie sendet.

    vom server bekomm ich den fehler "recv failed: 10054". damit weiß ich schonmal wo sich das programm aufhängt. der client beendet sich einfach ohne dass die "pause" anweisung aufgerufen wird.

    die datei die der server empfangen soll erstellt er auch, sie wird aber nicht mit daten gefüllt die empfangen werden sollten.

    hier der code vom SERVER:

    [cpp]
    #include <fstream>
    #include <iostream>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    
    #define DEFAULT_BUFLEN 512
    #define DEFAULT_PORT "27015"
    
    int __cdecl main(void) 
    {
        WSADATA wsaData;
        SOCKET ListenSocket = INVALID_SOCKET,
               ClientSocket = INVALID_SOCKET;
        struct addrinfo *result = NULL,
                        hints;
        char recvbuf[DEFAULT_BUFLEN];
        int iResult, iSendResult;
        int recvbuflen = DEFAULT_BUFLEN;
    
        // Initialize Winsock
        iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
        if (iResult != 0) {
            printf("WSAStartup failed: %d\n", iResult);
    		system("pause");
            return 1;
        }
    
        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
        hints.ai_flags = AI_PASSIVE;
    
        // Resolve the server address and port
        iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
        if ( iResult != 0 ) {
            printf("getaddrinfo failed: %d\n", iResult);
            WSACleanup();
    		system("pause");
            return 1;
        }
    
        // Create a SOCKET for connecting to server
        ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
        if (ListenSocket == INVALID_SOCKET) {
            printf("socket failed: %ld\n", WSAGetLastError());
            freeaddrinfo(result);
            WSACleanup();
    		system("pause");
            return 1;
        }
    
        // Setup the TCP listening socket
        iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            printf("bind failed: %d\n", WSAGetLastError());
            freeaddrinfo(result);
            closesocket(ListenSocket);
            WSACleanup();
    		system("pause");
            return 1;
        }
    
        freeaddrinfo(result);
    
        iResult = listen(ListenSocket, SOMAXCONN);
        if (iResult == SOCKET_ERROR) {
            printf("listen failed: %d\n", WSAGetLastError());
            closesocket(ListenSocket);
            WSACleanup();
    		system("pause");
            return 1;
        }
    
        // Accept a client socket
        ClientSocket = accept(ListenSocket, NULL, NULL);
        if (ClientSocket == INVALID_SOCKET) {
            printf("accept failed: %d\n", WSAGetLastError());
            closesocket(ListenSocket);
            WSACleanup();
    		system("pause");
            return 1;
        }
    
        // No longer need server socket
        closesocket(ListenSocket);
    
    	ofstream ausgabe("datei2.bin", ios_base::out | ios_base::app | ios_base::binary);
        // Receive until the peer shuts down the connection
        do {
    		// EINLESEN UND SCHREIBEN VON DATEI --------------------------------------
    
            iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
            if (iResult > 0) {
                printf("Bytes received: %d\n", iResult);
    
    			ausgabe.write(recvbuf, sizeof(recvbuf));		
    		// ENDE --------------------------------------------------------------------	
    
    		/*
            // Echo the buffer back to the sender
                iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
                if (iSendResult == SOCKET_ERROR) {
                    printf("send failed: %d\n", WSAGetLastError());
                    closesocket(ClientSocket);
                    WSACleanup();
                    return 1;
                }
                printf("Bytes sent: %d\n", iSendResult);
    			*/
            }
            else if (iResult == 0)
                printf("Connection closing...\n");
            else  {
                printf("recv failed: %d\n", WSAGetLastError());
                closesocket(ClientSocket);
                WSACleanup();
    			system("pause");
                return 1;
            }
    
        } while (iResult > 0);
    	ausgabe.close();
    	system("pause");
    
        // shutdown the connection since we're done
        iResult = shutdown(ClientSocket, SD_SEND);
        if (iResult == SOCKET_ERROR) {
            printf("shutdown failed: %d\n", WSAGetLastError());
            closesocket(ClientSocket);
            WSACleanup();
    	system("pause");
            return 1;
        }
    
        // cleanup
        closesocket(ClientSocket);
        WSACleanup();
        system("pause");
        return 0;
    }
    [/cpp]
    

    und hier der code vom CLIENT (wird mit " *.exe IP " geöffnet:

    [cpp]
    #include <fstream>
    #include <iostream>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    
    #define DEFAULT_BUFLEN 512
    #define DEFAULT_PORT "27015"
    
    int __cdecl main(int argc, char **argv) 
    {
        WSADATA wsaData;
        SOCKET ConnectSocket = INVALID_SOCKET;
        struct addrinfo *result = NULL,
                        *ptr = NULL,
                        hints;
        char *sendbuf = "this is a test";
    //    char recvbuf[DEFAULT_BUFLEN];
        int iResult;
    //    int recvbuflen = DEFAULT_BUFLEN;
    
    	// Validate the parameters
        if (argc != 2) {
            printf("usage: %s server-name\n", argv[0]);
    		system("pause");
            return 1;
        }
    
    	// Initialize Winsock
        iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
        if (iResult != 0) {
            printf("WSAStartup failed: %d\n", iResult); 
    	system("pause");
            return 1;
        }
    
    	ZeroMemory( &hints, sizeof(hints) );
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
    
        // Resolve the server address and port
        iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
        if ( iResult != 0 ) {
            printf("getaddrinfo failed: %d\n", iResult);
            WSACleanup();
    		system("pause");
            return 1;
        }
    
    	// Attempt to connect to an address until one succeeds
        for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
    
            // Create a SOCKET for connecting to server
            ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
                ptr->ai_protocol);
            if (ConnectSocket == INVALID_SOCKET) {
                printf("Error at socket(): %ld\n", WSAGetLastError());
                freeaddrinfo(result);
                WSACleanup();
    	    system("pause");
                return 1;
            }
    
            // Connect to server.
            iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
            if (iResult == SOCKET_ERROR) {
                closesocket(ConnectSocket);
                ConnectSocket = INVALID_SOCKET;
                continue;
            }
            break;
        }
    
    	freeaddrinfo(result);
    
        if (ConnectSocket == INVALID_SOCKET) {
            printf("Unable to connect to server!\n");
            WSACleanup();
    		system("pause");
            return 1;
        }
    
    	// EINLESEN UND SENDEN VON DATEI
    
    	char buf[1];
    	char bufsend[DEFAULT_BUFLEN];
        int bytes;
    	int i;
        ifstream eingabe("datei1.bin", ios_base::in | ios_base::binary);
        if (eingabe.good())
    	{
           eingabe.seekg(0L,ios::end);
           bytes=eingabe.tellg();
           cout << bytes << endl;
           eingabe.seekg(0L,ios::beg);
        }
    
        for (i=0; i<bytes;)
    	{
    		for (int z=0; z<DEFAULT_BUFLEN; z++)
    		{
    			eingabe.read(buf, sizeof(buf));
    		    strcat(bufsend,buf);
    			i++;
    			cout << "i = " << i << endl;
    			if (i==bytes)
    			{
    				sendbuf=bufsend;
    				iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    				if (iResult == SOCKET_ERROR)
    				{
    					printf("send failed: %d\n", WSAGetLastError());
    					closesocket(ConnectSocket);
    					WSACleanup();
    					system("pause");
    					return 1;
    			    }
    			}
    		}
    		cout << bufsend << endl;
    		if (i<bytes)
    		{
    			sendbuf=bufsend;
    			// Send an initial buffer
    			iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    			if (iResult == SOCKET_ERROR)
    			{
    				printf("send failed: %d\n", WSAGetLastError());
    				closesocket(ConnectSocket);
    				WSACleanup();
    				system("pause");
    				return 1;
    			}
    			printf("Bytes Sent: %ld\n", iResult); 
    		}
        }
        eingabe.close();
        system("pause"); // ---------------------------------------
    
        // shutdown the connection since no more data will be sent
        iResult = shutdown(ConnectSocket, SD_SEND);
        if (iResult == SOCKET_ERROR) {
            printf("shutdown failed: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
    		system("pause");
            return 1;
        }
    /*
        // Receive until the peer closes the connection
        do {
    
            iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
            if ( iResult > 0 )
                printf("Bytes received: %d\n", iResult);
            else if ( iResult == 0 )
                printf("Connection closed\n");
            else
                printf("recv failed: %d\n", WSAGetLastError());
    
        } while( iResult > 0 );
    */
        // cleanup
        closesocket(ConnectSocket);
        WSACleanup();
    
        // << FUNKTIONUCKELT <<
    
    system("pause");
    
    	return 0;
    }
    [/cpp]
    

    vielen dank schonmal vorab für die hilfe!


Anmelden zum Antworten