Keresés

Új hozzászólás Aktív témák

  • Trub

    tag

    Azt hogy lehet megcsinálni, hogy a programban jelen legyen egy már lefodított exe, vagy kép, vagy egy akármilyen tipusú fájl, és azt ki tudja másolni a programom egy megadott helyre? Visual studio-t használok, és a neten nem találtam semmit erről a témáról. Egyáltalán lehetséges ez?

  • Trub

    tag

    Lenne egy kis problémám, a winsock2-vel.
    Megírtam msdn-ről a próbakódot, a server, és az ügyfél részét, de nem akar működni.
    Lefordul, nincs hiba. Windows 7-en nem működik megfelelően, nem bír csatlakozni az ügyfél a szerverhez, xp-n már el sem indul, mert hiányol pár dll-t, de miután ezeket pótoltam xp-n tökéletesen működik, ahogy kell.

    Az ügyfél:
    #include <WinSock2.h>
    #include <ws2tcpip.h>
    #include <iostream>
    #pragma comment(lib, "ws2_32")




    int main(int argc,char* argv[])
    {
    WSADATA wsaData;

    int iResult;

    // Initalize Winsok
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) std::cout << "WSAStartup failed" << iResult;

    //declare addrinfo
    struct addrinfo *result=NULL, *ptr=NULL, hints;
    ZeroMemory(&hints, sizeof(hints));

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    //get addressinfo
    #define DEFAULT_PORT "27015"
    iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);

    if (iResult != 0)
    {
    printf("getaddrinfo failed: %d\n", iResult);
    WSACleanup();
    }

    SOCKET ConnectSocket = INVALID_SOCKET;

    // Attempt to connect to the first address returned by
    // the call to getaddrinfo
    ptr=result;

    // 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();
    }

    // Connect to server.
    iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR)
    {
    closesocket(ConnectSocket);
    ConnectSocket = INVALID_SOCKET;
    }

    // Should really try the next address returned by getaddrinfo
    // if the connect call failed
    // But for this simple example we just free the resources
    // returned by getaddrinfo and print an error message

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET)
    {
    printf("Unable to connect to server!\n");
    WSACleanup();
    }

    //sending and recieving Data on the Client
    #define DEFAULT_BUFLEN 512

    int recvbuflen = DEFAULT_BUFLEN;
    char *sendbuf = "this is a test";
    char recvbuf[DEFAULT_BUFLEN];

    // 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();
    }

    printf("Bytes Sent: %ld\n", iResult);

    // shutdown the connection for sending since no more data will be sent
    // the client can still use the ConnectSocket for receiving data
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR)
    {
    printf("shutdown failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    }

    // Receive data until the server 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);

    // shutdown the send half of 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();
    }

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();





    std::cin.get();
    return 0;
    }

    A server:
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iostream>
    #pragma comment(lib, "ws2_32")


    int main(int argc, _TCHAR* argv[])
    {
    WSADATA wsaData;
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0)
    {
    printf("WSAStartup failed: %d\n", iResult);
    }

    //Define addinfo
    #define DEFAULT_PORT "27015"
    struct addrinfo *result=NULL,*ptr=NULL, hints;

    ZeroMemory(&hints, sizeof (hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if (iResult != 0)
    {
    printf("getaddrinfo failed: %d\n", iResult);
    WSACleanup();
    }

    SOCKET ListenSocket = INVALID_SOCKET;
    // Create a SOCKET for the server to listen for client connections
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET)
    {
    printf("Error at socket(): %ld\n", WSAGetLastError());
    freeaddrinfo(result);
    WSACleanup();
    }

    // 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();
    }

    //Listening on a Socket
    if ( listen( ListenSocket, SOMAXCONN ) == SOCKET_ERROR )
    {
    printf( "Error at bind(): %ld\n", WSAGetLastError() );
    closesocket(ListenSocket);
    WSACleanup();
    }


    //Accepting a Clientsocket
    SOCKET ClientSocket = INVALID_SOCKET;
    ClientSocket = accept(ListenSocket, NULL, NULL);

    if (ClientSocket == INVALID_SOCKET)
    {
    printf("accept failed: %d\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    }

    //Receiving and Sending Data on the Server
    #define DEFAULT_BUFLEN 512

    char recvbuf[DEFAULT_BUFLEN];
    int iSendResult;
    int recvbuflen = DEFAULT_BUFLEN;
    // Receive until the peer shuts down the connection
    do
    {
    iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0) {
    printf("Bytes received: %d\n", iResult);

    // 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();
    return 1;
    }

    } while (iResult > 0);

    // shutdown the send half of the connection since no more data will be sent
    iResult = shutdown(ClientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR)
    {
    printf("shutdown failed: %d\n", WSAGetLastError());
    closesocket(ClientSocket);
    WSACleanup();
    }
    // cleanup
    closesocket(ClientSocket);
    WSACleanup();





    std::cin.get();
    return 0;
    }

    Visual studio 2010-vel fordítom.

    [ Szerkesztve ]

  • Trub

    tag

    válasz Trub #634 üzenetére

    Újraírtam a kódot, és már működik. Nem tudom mi lehetett a baj, lehet, hogy csak a port volt folglat.

Új hozzászólás Aktív témák