Thread: Permission Denied

  1. #1
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109

    Permission Denied

    I am writing a server for my game that I am making. Up to about 2 minutes ago, it compiled fine. Now, however, it gives me an error and I have changed nothing.

    Main Code:
    Code:
    #include <windows.h>
    #include <winsock2.h>
    #include <fstream>
    #include "definitions.hpp"
    using namespace std;
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[ ] = "DNMasterServer";
    HWND hwnd, connectlog;
    WORD sockVersion;
    WSADATA wsaData;
    int iResult;
    SOCKET m_socket;
    SOCKET AcceptSocket;
    HANDLE connectthread;
    char mstrmessage[2048];
    
    void WaitConnection() {
        while (1) {
                AcceptSocket = SOCKET_ERROR;
                while (AcceptSocket == SOCKET_ERROR) {
                    AcceptSocket = accept( m_socket, NULL, NULL );
                }
                m_socket = AcceptSocket;
                send(m_socket, mstrmessage, strlen(mstrmessage), 0);
                closesocket(m_socket);
                closesocket(AcceptSocket);
                SendMessage(connectlog, LB_ADDSTRING, 0, (LPARAM) "Master Message Sent...");
        }
        ExitThread(0);
    }
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        MSG messages;
        WNDCLASSEX wincl;
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        if (!RegisterClassEx (&wincl))
            return 0;
    
        hwnd = CreateWindowEx (
               0,
               szClassName,
               "Day and Night Master Server",
               WS_OVERLAPPEDWINDOW | WS_MINIMIZE,
               CW_USEDEFAULT,
               CW_USEDEFAULT,
               640,
               480,
               HWND_DESKTOP,
               NULL,
               hThisInstance,
               NULL
               );
    
        ShowWindow (hwnd, nFunsterStil);
    
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
    
        return messages.wParam;
    }
    
    
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hdc;
        PAINTSTRUCT ps;
        LPSTR szMessage = "Day and Night Master Server Program";
        LPSTR szMessage2 = "Set Master Server Message:";
        ifstream a_file;
        ofstream myfile;
        switch (message)
        {
            case WM_CREATE:
                 a_file.open("dnmstrsrvrmsg.txt", ios::in);
                 if(!a_file.is_open()) {
                     MessageBox(hwnd, "Failed to open file!", "Error!", MB_OK | MB_ICONSTOP);
                 }
                 while(!a_file.eof()) 
                 {
                     a_file.getline(mstrmessage, 2046);
                 }
                 a_file.close();
    
                 CreateWindow("Edit", mstrmessage, WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP | ES_MULTILINE, 32, 96, 320, 240, hwnd, (HMENU)ID_MESSAGE, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
                 CreateWindow("Button", "Submit New Message", WS_BORDER | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 112, 352, 150, 25, hwnd, (HMENU) ID_SUBMITNEW, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
                 connectlog = CreateWindow("Listbox", "", WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_BORDER | LBS_NOTIFY, 384, 128, 224, 208, hwnd, (HMENU) ID_CONNECTLOG, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); 
                 SendMessage(connectlog, LB_ADDSTRING, 0, (LPARAM) "Master Server Started...");
                 iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
                 if ( iResult != NO_ERROR ) {
                     MessageBox(hwnd, "Error initializing Winsock!", "Error!", MB_ICONSTOP | MB_OK);
                     exit(0);
                 }
                 m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
                 if ( m_socket == INVALID_SOCKET ) {
                     MessageBox(hwnd, "Error at socket():", "Error!", MB_ICONSTOP | MB_OK);
                     MessageBox(hwnd, _itoa(WSAGetLastError(), NULL, 10), "Error!", MB_ICONSTOP | MB_OK);
                     WSACleanup();
                     exit(0);
                 }
                 sockaddr_in service;
                 service.sin_family = AF_INET;
                 service.sin_addr.s_addr = INADDR_ANY;
                 service.sin_port = htons(19493);
                 if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
                     MessageBox(hwnd, "bind() Failed!", "Error!", MB_ICONSTOP | MB_OK);
                     closesocket(m_socket);
                     exit(0);
                 }
                 if ( listen( m_socket, 1 ) == SOCKET_ERROR ) {
                     MessageBox(hwnd, "Error listening on socket!", "Error!", MB_ICONSTOP | MB_OK);
                     exit(0);
                 }
                 connectthread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WaitConnection, NULL, 0, NULL);
                 break;
            case WM_PAINT:
                 hdc = BeginPaint(hwnd, &ps);
                 SetBkColor(hdc, 0x008500);
                 TextOut(hdc, 192, 16, szMessage, strlen(szMessage));
                 TextOut(hdc, 32, 64, szMessage2, strlen(szMessage2));
                 EndPaint(hwnd, &ps);
                 break;
            case WM_COMMAND:
                 switch(LOWORD(wParam)) {
                     case ID_SUBMITNEW:
                          char tempmessage[2048];
                          char checkmessage[] = "Are you sure you wish to change the Master Server Message to the following message:\n\n";
                          GetDlgItemText(hwnd, ID_MESSAGE, tempmessage, 2046);
                          strcat(checkmessage, tempmessage);
                          if(MessageBox(hwnd, checkmessage, "Change the Master Server Message?", MB_YESNO) == 6) {
                               GetDlgItemText(hwnd, ID_MESSAGE, mstrmessage, 2046);
                               myfile.open("dnmstrsrvrmsg.txt", ios::trunc);
                               if(!myfile.is_open()) {
                                    MessageBox(hwnd, "Failed to open file!", "Error!", MB_OK | MB_ICONSTOP);
                                    break;
                               }
                               myfile<<mstrmessage;
                               myfile.close();
                          }
                          break;
                 }
                 break;
            case WM_DESTROY:
                TerminateThread(connectthread, 0);
                closesocket(m_socket);
                closesocket(AcceptSocket);
                PostQuitMessage (0);
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    Header File:
    Code:
    #define ID_MESSAGE        100
    #define ID_SUBMITNEW      101
    #define ID_CONNECTLOG     102
    Error Messages:
    Permission denied
    ld returned 1 exit status
    C:\Dev-Cpp\Day and Night Master Server\Makefile.win [Build Error] ["Day] Error 1
    I was doing the exact same thing before and got the same warning for the second line, but now it tells me the other two things.

    I have not changed anything in my code since I last got it to compile. I have tried to rebuild the project and that didn't work. I tried closing it and reopening it and that didn't help. I have tried opening another project and then coming back to this one and that didn't help.

    I am using Dev-C++.


    Oh and I know that the code I use for checking connections the server is bad I am going to fix that.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I am guessing that your program is still running and the linker can't overwrite the executable. Check your processes and terminate the process if necessary.

  3. #3
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    That got it. I had to close it under bad circumstances.

    Thanks.
    Sic vis pacum para bellum. If you want peace, prepare for war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tmpnam() : permission denied when try to open temp file
    By happyclown in forum C Programming
    Replies: 3
    Last Post: 03-16-2009, 11:48 PM
  2. Replies: 1
    Last Post: 02-10-2009, 11:02 AM
  3. Running remove() gets permission denied error...
    By edomingox in forum C Programming
    Replies: 4
    Last Post: 01-11-2009, 12:55 PM
  4. access denied
    By laasunde in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2002, 02:38 PM
  5. Permission Denied error
    By ChazWest in forum C Programming
    Replies: 7
    Last Post: 03-08-2002, 05:21 PM