når jeg kører mit program siger windows at mit program har fundet en fejl og afsluttes (selvom det nok er windows som har fundet den)
så hvad er fejlen i mit program? og visual c++ finder ingen fejl
her er kildekoden:
#include <time.h>
#include <errno.h>
#include <windows.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#pragma comment(lib,"wsock32.lib")
#define TIMEOUT 5 // Max waiting time in sec
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
char *hostname = "messenger.hotmail.com";
int port = 1863;
int sd, status, len, ix, tmp;
struct sockaddr local,remote;
struct hostent *hostinfo;
fd_set ReadSet;
struct timeval Time;
// create
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd == SOCKET_ERROR)
{
std::cerr << "Error creating socket: " << WSAGetLastError() << std::endl;
return 0;
}
// bind
local.sa_family = AF_INET;
memset(local.sa_data, 0, sizeof(local.sa_data));
status = bind(sd, &local, sizeof(local));
if(status == SOCKET_ERROR)
{
std::cerr << "Error binding socket: " << WSAGetLastError() << std::endl;
closesocket(sd);
return 0;
}
// lookup host
hostinfo = gethostbyname(hostname);
if(!hostinfo)
{
std::cerr << "Error looking up host: " << hostname << ", " << WSAGetLastError() << std::endl;
closesocket(sd);
return 0;
}
// connect to host
remote.sa_family = hostinfo->h_addrtype;
memcpy(remote.sa_data + 2, hostinfo->h_addr_list[0], hostinfo->h_length);
*((short *)remote.sa_data) = port;
tmp = remote.sa_data[0];
remote.sa_data[0] = remote.sa_data[1];
remote.sa_data[1] = tmp;
status = connect(sd, &remote, sizeof(remote));
if(status)
{
std::cerr << "Error connecting to host: " << WSAGetLastError() << std::endl;
closesocket(sd);
return 0;
}
char resp[256]; // Read at most 128 bytes
char sending[256];
strcpy("VER 1 MSNP10 CVR0\\r\\n", sending);
// send request
send(sd, sending, strlen(sending),0);
// read response */
ix = 0;
memset(&ReadSet, 0, sizeof(ReadSet));
FD_SET(sd, &ReadSet);
Time.tv_sec = TIMEOUT;
Time.tv_usec = 0;
len = 1;
while(true) {
while(len > 0 && select(sd + 1, &ReadSet, 0, 0, &Time) > 0)
{
len = recv(sd, resp + ix, sizeof(resp) - ix - 1, 0);
ix += len;
Time.tv_sec = TIMEOUT;
Time.tv_usec = 0;
}
resp[ix] = 0;
closesocket(sd);
std::cout << "Received: " << resp << "\\n";
if (resp[0] = 'V') {
strcpy("CVR 2 0x0409 win 4.10 i386 MSNMSGR 5.0.0544 MSMSGS andreas_the_programmer@homtail.com\\r\\n", sending);
}
else
{
std::cout << "Afslutter...\\n";
return 0;
}
std::cout << "Sending: " << sending << "\\n";
status = send(sd, (const char*) sending, strlen(sending), 0);
if(status == SOCKET_ERROR) {
std::cerr << "Error sending request: " << WSAGetLastError() << std::endl;
closesocket(sd);
return 0;
}
}
return 0;
}