Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket programming problem

Status
Not open for further replies.

matematik

Programmer
Joined
Aug 13, 2007
Messages
8
Location
SI
I'm using this class to connect to server:
bool ClientSocket::Povezi(String naslov, String port) {
// spremenljivke
WSADATA wsaData;
this->Povezava = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;

// koda
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
MessageBox(NULL, "WSAStartup failed: " + iResult, "Napaka!", MB_ICONERROR|MB_OK);
return FALSE;
}

ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

iResult = getaddrinfo(naslov.c_str(), port.c_str(), &hints, &result);
if ( iResult != 0 ) {
MessageBox(NULL, "getaddrinfo failed: " + iResult, "Napaka!", MB_ICONERROR|MB_OK);
WSACleanup();
return FALSE;
}

for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

// Create a SOCKET for connecting to server
this->Povezava = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (this->Povezava == INVALID_SOCKET) {
MessageBox(NULL, "Error at socket(): " + WSAGetLastError(), "Napaka!", MB_ICONERROR|MB_OK);
freeaddrinfo(result);
WSACleanup();
return FALSE;
}

// Connect to server.
iResult = connect( this->Povezava, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(this->Povezava);
this->Povezava = INVALID_SOCKET;
continue;
}
break;
}

freeaddrinfo(result);

if (this->Povezava == INVALID_SOCKET) {
MessageBox(NULL, "Unable to connect to server!\n", "Napaka!", MB_ICONERROR|MB_OK);
WSACleanup();
return FALSE;
}

return TRUE;
}

void ClientSocket::Prekini() {
closesocket(this->Povezava);
WSACleanup();
}

String ClientSocket::Dobi() {
char recvbuf[512];
for (int i = 0; i <= 511; i++) {
recvbuf = '\0';
}
int iResult = 0;
iResult = recv(this->Povezava, recvbuf, 512, 0);
if ( iResult > 0 ) {
String for_ret = recvbuf;
return for_ret;
} else if ( iResult == 0 ) {
this->Prekini();
return "";
} else {
MessageBox(NULL, "recv failed: " + WSAGetLastError(), "Napaka!", MB_ICONERROR|MB_OK);
return "";
}
}

bool ClientSocket::Poslji(String za_poslat) {
int iResult = send(this->Povezava, za_poslat.c_str(), (int)za_poslat.Length(), 0 );
if (iResult == SOCKET_ERROR) {
MessageBox(NULL, "send failed: " + WSAGetLastError(), "Napaka!", MB_ICONERROR|MB_OK);
this->Prekini();
return FALSE;
}
return TRUE;
}
And this is my code:
ClientSocket test;
test.Povezi("pop3.mail386.com", "110");
String dobitev;
dobitev = test.Dobi();
test.Poslji("USER nekdo");
String dobitev1;
dobitev1 = test.Dobi();
test.Poslji("PASS krneki");
dobitev = test.Dobi();
test.Poslji("quit");
dobitev = test.Dobi();
test.Prekini();

When I call test.Dobi() for first time it works fine, but when I call it again, it doesn't work. It stops at iResult = recv(this->Povezava, recvbuf, 512, 0);.
What's the problem?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top