Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 28th, 2005, 12:26 AM   #1
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Arrow Could someone proof-read this please?

Hehe, well, I said I was done for the day, but it's now 12:10 AM and it's a new day. So, I have a question, would someone mind proof-reading this segment of code? It's a function to listen for WinSock. I know that it is probably way off and not functional, but I'm trying, especially with the paremeters in the function, I don't think I placed them 'inside' the function correctly...
int WinSockListen(int Ip, int Port, int backlog){
    listenS = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listenS == INVALID_SOCKET) 
    {
                WinSockCloseSocket(listenS);
                WinSockClose();
                return EXIT_SUCCESS;
    }
    SOCKADDR_IN info;
    info.sin_family = AF_INET;
    info.sin_addr.s_addr = Ip;
    info.sin_port = htons(Port);
    ret = bind(listenS,(LPSOCKADDR)&info,sizeof(struct sockaddr));
    if (ret == SOCKET_ERROR)
    {
            WinSockCloseSocket(listenS);
            WinSockClose();
            return EXIT_SUCCESS;
    }
    ret = listen(listenS, backlog);
    if (ret == SOCKET_ERROR)
    {
            WinSockCloseSocket(listenS);
            WinSockClose();
            return EXIT_SUCCESS;
    }
}
I think Ip (the first paremeter) should be a string, but I get this error when I label it as string inside the paremeters:
Quote:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp" -o "C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lwsock32
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:28: error: `string' was not declared in this scope
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:28: error: expected primary-expression before "int"
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:28: error: expected primary-expression before "int"
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:28: error: initializer expression list treated as compound expression
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:28: error: expected `,' or `;' before '{' token

Execution terminated
But when I change Ip to int in the paremeters, it compiles fine... But could someone please point out where I may be going wrong somewhere in the code?

Ok, I just tested the "WinSockListen" function roughly, and heres what I get when trying to compile... I know it's gotta be something simple because its an invalid conversion! :eek: Am I supposed to change the paremeters in WinSockListen? Well, here is the attmept I have made to use the WinSockListen function in my code:
case WM_COMMAND:
         if((HWND)lParam == hconnectIP)
             {
                         char IpMem[40];
                         char sPort[10];
                         GetWindowText(hPortData, sPort, sizeof(sPort)); 
                         char sIp[30];
                         GetWindowText(hIPData, sIp, sizeof(sIp));
                         strcat(IpMem, sIp);
                         strcat(IpMem, ":");
                         strcat(IpMem, sPort);
                         MessageBox(NULL, IpMem, "Ip and Port", MB_OK);
                         WinSockListen(sIp, sPort, 5);
                         MessageBox(NULL, "worked?", "DEBUG", MB_OK);
             }
        break;
And here are the errors I get when trying to compile all of my code with that:
Quote:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp" -o "C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lwsock32
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp: In function `LRESULT WindowProcedure(HWND__*, UINT, WPARAM, LPARAM)':
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:214: error: invalid conversion from `char*' to `int'
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:214: error: initializing argument 1 of `int WinSockListen(int, int, int)'
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:214: error: invalid conversion from `char*' to `int'
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:214: error: initializing argument 2 of `int WinSockListen(int, int, int)'

Execution terminated
Soo... Would someone be oh so kind and point out where I'm going wrong? I hope I provided enough information (I think I provided plenty )
Again, thanks in advance...

PS,
You don't have to write any code for me :p I'd rather learn, I just need to know where I'm going wrong so I can do a little research and see how to fix it :o
layer is offline   Reply With Quote
Old Apr 28th, 2005, 12:50 PM   #2
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Well, obviously it's because sIp and sPort are declared as "char"'s when the paremeters for WinSockListen() are "int"'s. So, any wa around this?
layer is offline   Reply With Quote
Old Apr 28th, 2005, 1:31 PM   #3
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
Well, first of all you need to make the function consistent with the parameters you pass. Anyway, this thread should help:

http://www.programmingforums.org/for...ead.php?t=3602

It discusses how to convert a string to an int, so it may prove useful.

kirkl_uk
kirkl_uk is offline   Reply With Quote
Old Apr 28th, 2005, 2:19 PM   #4
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Gotchya, totally forgot about atoi... :o Thanks!!
layer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC