I like to receive data on SOCKET. I call the recv() method with required params:

char* buff = new char[len];
memset(buff, 0, len);
if(recv(sck, buff, len, MSG_PEEK) == SOCKET_ERROR) {
cerr << "String Receive Error - code: " << WSAGetLastError() << endl;
}

The recv() method copies data to the buff. That's right. But recv() method don't removes copied data from SOCKET sck. Why and can I do anything for removing?

> Why and can I do anything for removing?
Try using 0 if you want to fetch the message

PEEK implies look at the message, but DON'T remove it. You got what you asked for.

The other problem is, having filled your buffer with \0, if recv() also fills the buffer with data, then there will be NO \0 at the end, and thus it will not be a proper C-style string.

Oh, and before you post more code, figure out how to use code tags by reading the intro threads, and paying attention to the watermark at the back of the edit window.

If your code doesn't look like this, expect a less than positive response.

char* buff = new char[len];
memset(buff, 0, len);
if(recv(sck, buff, len, MSG_PEEK) == SOCKET_ERROR) {
    cerr << "String Receive Error - code: " << WSAGetLastError() << endl;
}

Thanx for quite advise, but the problem don't solved. That is calling of

recv(sck, buff, len, 0);

method read data from socket into buff but don't remove that from socket.

BR
MadeInHungary

Thanx for your help!

There is the code which like to receive a string from socket. First it receive length of string from socket. That's right. It receive accurate length of string. But, when it received the length, and like receive the string, on socket still is the length and not the string, Because recv() method don't remove the length from socket after it read that.

BR
MadeInHungary

******************
SOCKET ALLSpeechRet::getAllSpeechServer() {


//creating a socket
SOCKET AllSpeechSocketServer = INVALID_SOCKET;
try {
//initializing of Windows Socket DLL
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)  {
cerr << endl << "WinSock DLL not initialzed!" << endl;
return AllSpeechSocketServer;
}


AllSpeechSocketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(AllSpeechSocket != INVALID_SOCKET) {


//for remote endpoint address (host)
SOCKADDR_IN remHostAdr;
memset (&remHostAdr, 0, sizeof(remHostAdr));
remHostAdr.sin_family = AF_INET;
remHostAdr.sin_addr.s_addr = inet_addr("192.168.0.56");
remHostAdr.sin_port = htons(1975);


//connecting to the host
if(bind(AllSpeechSocketServer, (sockaddr*)&remHostAdr, sizeof(remHostAdr)) == SOCKET_ERROR) {
cerr << "Bind Error - code: " << WSAGetLastError() << endl;
closeAllSpeechConnection();
AllSpeechSocketServer = INVALID_SOCKET;
}


listen(AllSpeechSocketServer, QUEUE_SIZE);


while (1) { // block for connection request
//---- ACCEPT connection ------------------------------------
cerr << " in ALLSpeechRet::getAllSpeechServer() - Before Accept" << endl;
SOCKET sck_cr = accept(AllSpeechSocketServer, 0, 0);
if (sck_cr < 0) {
cerr << "Accept Error code: " << WSAGetLastError() << endl;
closeAllSpeechConnection();
AllSpeechSocketServer = INVALID_SOCKET;
} else {
cerr << "Accept a Connection!" << endl;
}


//for history
char* niez = receiveString(sck_cr);
cerr << "Funtction name is: " << niez << endl;
//for history


closesocket(sck_cr);
}


} else {
std::cerr << "Socket Server Initialization Error - code: " << WSAGetLastError() << endl;
}
} catch (std::exception& e) {
std::cout << "Exception: " << e.what();
}


return AllSpeechSocketServer;
}



******************
int ALLSpeechRet::receiveInteger(SOCKET sck) {
int ret;
u_long par = 0;
if(recv(sck, (char*)&par, sizeof(u_long), 0) == SOCKET_ERROR) {
cerr << "Integer Receive Error - code: " << WSAGetLastError() << endl;
ret = SOCKET_ERROR;
} else { ret = (int)ntohl(par); }
return ret;
}



******************
char* ALLSpeechRet::receiveString(SOCKET sck) {
int len = receiveInteger(sck) - sizeof(u_long) + 1;


int wrkI;
char* buff = new char[len];
memset(buff, 0, len);


//if((wrkI = recv(sck, buff, len, MSG_PEEK)) == SOCKET_ERROR) {
if((wrkI = recv(sck, buff, len-1, 0)) == SOCKET_ERROR) {
cerr << "String Receive Error - code: " << WSAGetLastError() << endl;
}


cerr << " in ALLSpeechRet::receiveString(3) arrived string length (wrkI): " << wrkI << endl;
cerr << " in ALLSpeechRet::receiveString(4) arrived string : <" << buff << ">" << endl;


return buff;
}

Apparently, I was talking to myself when I said this
> Oh, and before you post more code, figure out how to use code tags by reading the intro
> threads, and paying attention to the watermark at the back of the edit window.

******************
SOCKET ALLSpeechRet::getAllSpeechServer() {

//creating a socket
SOCKET AllSpeechSocketServer = INVALID_SOCKET;
try {
//initializing of Windows Socket DLL
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { 
cerr << endl << "WinSock DLL not initialzed!" << endl;
return AllSpeechSocketServer;
}

AllSpeechSocketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(AllSpeechSocket != INVALID_SOCKET) { 

//for remote endpoint address (host)
SOCKADDR_IN remHostAdr;
memset (&remHostAdr, 0, sizeof(remHostAdr));
remHostAdr.sin_family = AF_INET;
remHostAdr.sin_addr.s_addr = inet_addr("192.168.0.56");
remHostAdr.sin_port = htons(1975);

//connecting to the host
if(bind(AllSpeechSocketServer, (sockaddr*)&remHostAdr, sizeof(remHostAdr)) == SOCKET_ERROR) {
cerr << "Bind Error - code: " << WSAGetLastError() << endl;
closeAllSpeechConnection();
AllSpeechSocketServer = INVALID_SOCKET;
}

listen(AllSpeechSocketServer, QUEUE_SIZE); 

while (1) { // block for connection request
//---- ACCEPT connection ------------------------------------
cerr << " in ALLSpeechRet::getAllSpeechServer() - Before Accept" << endl;
SOCKET sck_cr = accept(AllSpeechSocketServer, 0, 0);
if (sck_cr < 0) {
cerr << "Accept Error code: " << WSAGetLastError() << endl;
closeAllSpeechConnection();
AllSpeechSocketServer = INVALID_SOCKET;
} else {
cerr << "Accept a Connection!" << endl;
}

//for history
char* niez = receiveString(sck_cr);
cerr << "Funtction name is: " << niez << endl;
//for history

closesocket(sck_cr);
}

} else {
std::cerr << "Socket Server Initialization Error - code: " << WSAGetLastError() << endl;
}
} catch (std::exception& e) { 
std::cout << "Exception: " << e.what(); 
}

return AllSpeechSocketServer;
}


******************
int ALLSpeechRet::receiveInteger(SOCKET sck) {
int ret;
u_long par = 0;
if(recv(sck, (char*)&par, sizeof(u_long), 0) == SOCKET_ERROR) {
cerr << "Integer Receive Error - code: " << WSAGetLastError() << endl;
ret = SOCKET_ERROR;
} else { ret = (int)ntohl(par); }
return ret;
}


******************
char* ALLSpeechRet::receiveString(SOCKET sck) {
int len = receiveInteger(sck) - sizeof(u_long) + 1;

int wrkI;
char* buff = new char[len];
memset(buff, 0, len);

//if((wrkI = recv(sck, buff, len, MSG_PEEK)) == SOCKET_ERROR) {
if((wrkI = recv(sck, buff, len-1, 0)) == SOCKET_ERROR) {
cerr << "String Receive Error - code: " << WSAGetLastError() << endl;
} 

cerr << " in ALLSpeechRet::receiveString(3) arrived string length (wrkI): " << wrkI << endl;
cerr << " in ALLSpeechRet::receiveString(4) arrived string : <" << buff << ">" << endl;

return buff;
}

Thanx for your help!

There is the code which like to receive a string from socket. First it receive length of string from socket. That's right. It receive accurate length of string. But, when it received the length, and like receive the string, on socket still is the length and not the string, Because recv() method don't remove the length from socket after it read that.

BR
MadeInHungary

It still lacks that "indented" quality which makes reading more than 5 lines of code a lot easier.

Salem,
what problem with 5 lines of code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.