![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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:
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! |
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
Are you linking with wsock32.lib?
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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 ![]() |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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 ![]() |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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:
![]() Thanks agaiN! ![]() |
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>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); |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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;
}![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|