Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 25th, 2005, 6:00 PM   #1
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Exclamation [SOLVED]WinSock going good, but undefined references...?

Hey guys... I'm just learning the basics of WinSock... And was in need of some help Heres my code:
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winsock.h>

using namespace std;

int main(){
    cout << "SweetTalk will now start up..." << endl;
    WORD sockversion;
    WSADATA wsadata;
    int ret;
    sockversion = MAKEWORD(1, 1);	
    WSAStartup(sockversion, &wsadata);
    SOCKET listenS;
    listenS = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listenS == INVALID_SOCKET) 
    {
                WSACleanup();
                cout << "SweetTalk now needs to exit due to technical difficulties." << endl << "Press any key to quit" << endl;
                getch();
                return EXIT_SUCCESS;
    }
    SOCKADDR_IN info;
    info.sin_family = AF_INET;
    info.sin_addr.s_addr = INADDR_ANY;
    info.sin_port = htons(8888);
    ret = bind(listenS,(LPSOCKADDR)&info,sizeof(struct sockaddr));
    if (ret == SOCKET_ERROR)
    {
            WSACleanup();
            cout << "SweetTalk now needs to exit due to technical difficulties." << endl << "Press any key to quit" << endl;
            getch();
            return EXIT_SUCCESS;
    }
    
    ret = listen(listenS, 5);
    if (ret == SOCKET_ERROR)
    {
            WSACleanup();
            cout << "SweetTalk now needs to exit due to technical difficulties." << endl << "Press any key to quit" << endl;
            getch();
            return EXIT_SUCCESS;
    }
    
            
    
    SOCKET client;
    client = accept(listenS, NULL, NULL);
    
    if (client == INVALID_SOCKET)
    {
            WSACleanup();
            cout << "SweetTalk now needs to exit due to an invalid client." << endl << "Press any key to quit" << endl;
            getch();
            return EXIT_SUCCESS;
    }
    
    closesocket(client);
    closesocket(listenS);
    
    WSACleanup();
    getch();
    return 0;
}

Now I get these errors...:
Quote:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\MYNAME\Desktop\WinSock\main.cpp" -o "C:\Documents and Settings\MYNAME\Desktop\WinSock\main.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"
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x16f):main.cpp: undefined reference to `WSAStartup@8'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x18e):main.cpp: undefined reference to `socket@12'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x1a5):main.cpp: undefined reference to `WSACleanup@0'

C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x21c):main.cpp: undefined reference to `htons@4'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x246):main.cpp: undefined reference to `bind@12'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x25d):main.cpp: undefined reference to `WSACleanup@0'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x2cb):main.cpp: undefined reference to `listen@8'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x2e2):main.cpp: undefined reference to `WSACleanup@0'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x358):main.cpp: undefined reference to `accept@12'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x36f):main.cpp: undefined reference to `WSACleanup@0'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x3d2):main.cpp: undefined reference to `closesocket@4'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x3e3):main.cpp: undefined reference to `closesocket@4'
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp/ccU9baaa.o(.text+0x3eb):main.cpp: undefined reference to `WSACleanup@0'
collect2: ld returned 1 exit status

Execution terminated
Now, that's all the errors I get... I've done some googling, but everything I find doesn't pinpoint my EXACT problem... Which is a problem
If anyone would help me out, I'd really, really, really appreciate it! And then if you could go check out my GetSystemTimes problem too, that'd be neat!

EDIT: Replaced my name with "MYNAME"... :p

Last edited by layer; Apr 25th, 2005 at 6:55 PM. Reason: put a solved tag in title if i can edit the title!
layer is offline   Reply With Quote
Old Apr 25th, 2005, 6:19 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
Are you linking with wsock32.lib?
Eggbert is offline   Reply With Quote
Old Apr 25th, 2005, 6:47 PM   #3
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
I'm linking... Yes, but with wsock32.lib, no... How would I got about doing that, do I do I go "Tools>Compiler Options" and then type in whatever I got to type into the linker command line options... Or, do I go "Execute>Paremeters" and type in whatever I got to type in... Or does it matter?
But the question is, what do I type in?

EDIT: I'm using Dev-Cpp
layer is offline   Reply With Quote
Old Apr 25th, 2005, 6:54 PM   #4
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Haha! Got it, Eggbert you're the best! With a little searching on google, I found the answer here: http://forums.devshed.com/archive/t-116433 on the 3 post down! Thanks again for the help
layer is offline   Reply With Quote
Old Apr 25th, 2005, 8:58 PM   #5
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Sorry... Need some help again First, I'll start with the code:
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winsock.h>

using namespace std;

int main(){
    system("title SweetTalk");
    char buffer[30];
    int ip;
    cout << "SweetTalk will now start up..." << endl;
    cout << "What is the IP address you would like to connect to?: " << endl;
    cin.getline(buffer,30);
    ip = atof(buffer);
    WORD sockversion;
    WSADATA wsadata;
    int ret;
    sockversion = MAKEWORD(1, 1);	
    WSAStartup(sockversion, &wsadata);
    LPHOSTENT host;
    in_addr eHost;
    eHost.s_addr = inet_addr(ip);
    host = gethostbyaddr((const char*)&eHost, sizeof(struct in_addr), AF_INET);
    if (!host)
    {
              cout << "You specified a wrong addrress, SweetTalk will now exit!" << endl;
              cout << "Press any key to exit" << endl;
              getch();
              WSACleanup();
              return EXIT_SUCCESS;
    }
    SOCKET socketB;
    socketB = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (socketB == INVALID_SOCKET)
    {
                cout << "Failed to create socket, press any key to exit" << endl;
                WSACleanup();
                getch();
                return EXIT_SUCCESS;
    }
    SOCKADDR_IN sInfo;
    sInfo.sin_family = AF_INET;
    sInfo.sin_addr = *((LPIN_ADDR)*host->h_addr_list);
    sInfo.sin_port = htons(80);
    ret = connect(socketB, (LPSOCKADDR)&sInfo, sizeof(struct sockaddr));
    if (ret == SOCKET_ERROR)
    {
            cout << "Failed to connect, press any key to exit!" << endl;
            WSACleanup();
            getch();
    }
    
    closesocket(socketB);
    WSACleanup();
    return 0;
}

Now, when trying to compile that, with the "-lwinsock32" in the compiler options under Dev-Cpp using the MingW compiler, I get the following errors (only 3 ):
Quote:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\MYNAME\Desktop\WinSock\mainb.cpp" -o "C:\Documents and Settings\MYNAME\Desktop\WinSock\mainb.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\mainb.cpp: In function `int main()':
C:\Documents and Settings\MYNAME\Desktop\WinSock\mainb.cpp:16: warning: converting to `int' from `double'
C:\Documents and Settings\MYNAME\Desktop\WinSock\mainb.cpp:24: error: invalid conversion from `int' to `const char*'
C:\Documents and Settings\MYNAME\Desktop\WinSock\mainb.cpp:24: error: initializing argument 1 of `long unsigned int inet_addr(const char*)'

Execution terminated
Any ideas on how to fix these conversion things...? Never was so good when I got the conversion errors please
Thanks agaiN!
layer is offline   Reply With Quote
Old Apr 25th, 2005, 9:17 PM   #6
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>ip = atof(buffer);
atof returns a double, but ip is declared as int.

>eHost.s_addr = inet_addr(ip);
inet_addr expects a string, not an integer.

One possible solution is to remove ip from the equation completely:
cin.getline(buffer,30);
//...
eHost.s_addr = inet_addr(buffer);
Eggbert is offline   Reply With Quote
Old Apr 25th, 2005, 9:41 PM   #7
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Talking

Geez man, thanks again! Works like a charm! Here's the working code (soon, I will have my first instant messenger together ) And notice how there is no errors at the bottom Thanks Eggbert!
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winsock.h>

using namespace std;

int main(){
    system("title SweetTalk");
    char buffer[30];
    cout << "SweetTalk will now start up..." << endl;
    cout << "What is the IP address you would like to connect to?: " << endl;
    cin.getline(buffer,30);
    WORD sockversion;
    WSADATA wsadata;
    int ret;
    sockversion = MAKEWORD(1, 1);	
    WSAStartup(sockversion, &wsadata);
    LPHOSTENT host;
    in_addr eHost;
    eHost.s_addr = inet_addr(buffer);
    host = gethostbyaddr((const char*)&eHost, sizeof(struct in_addr), AF_INET);
    if (!host)
    {
              cout << "You specified a wrong addrress, SweetTalk will now exit!" << endl;
              cout << "Press any key to exit" << endl;
              getch();
              WSACleanup();
              return EXIT_SUCCESS;
    }
    SOCKET socketB;
    socketB = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (socketB == INVALID_SOCKET)
    {
                cout << "Failed to create socket, press any key to exit" << endl;
                WSACleanup();
                getch();
                return EXIT_SUCCESS;
    }
    SOCKADDR_IN sInfo;
    sInfo.sin_family = AF_INET;
    sInfo.sin_addr = *((LPIN_ADDR)*host->h_addr_list);
    sInfo.sin_port = htons(80);
    ret = connect(socketB, (LPSOCKADDR)&sInfo, sizeof(struct sockaddr));
    if (ret == SOCKET_ERROR)
    {
            cout << "Failed to connect, press any key to exit!" << endl;
            WSACleanup();
            getch();
    }
    
    closesocket(socketB);
    WSACleanup();
    return 0;
}
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 9:47 PM.

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