I am trying to have my client connect to the server (both C++ & winsock2) and do authentication but always failed. The server seems okay because I can telnet to the server and manually type in the password(which is 888) and get the response from server. The telnet session looks like this:
telnet works fine with server
I have search through internet for usage of send and recv but they just don't work here. I have also trying to append "\r" or "\n" after the password and also not working.
Here is the server code:
strcpy_s(sendBuff, "200 Welcome.\r\n");
send(client, sendBuff, strlen(sendBuff), 0);
while (true)
{
memset(rcvBuffer, 0,1024);
input="";
int n;
while ((n = recv(client, rcvBuffer, sizeof(rcvBuffer),0))>0) <<- server keeps waiting here
{
if(strchr(rcvBuffer, '\n') != 0)
break;
if (n>0)
input.append(rcvBuffer, n);
}
if (n == SOCKET_ERROR) {
cout << "Detect client disconnection" << endl;
break;
}
cout <<"Got message from client["<< input << "] sizeof(input) is" <<input.size()<< endl;
if (ParseCmd(input.c_str(), cmd, params)){
if (cmd == "QUIT"||cmd=="quit")
break;
if (cmd == "AUTH"||cmd=="auth"){
for (struct_user u : user) {
if (params == u.passwd){
this_user=u;
auth = true;
strcpy_s(sendBuff, "200 You are logged in.\r\n");
send(client, sendBuff, strlen(sendBuff), 0);
}
}
if (auth != true) {
strcpy_s(sendBuff, "401 Bad password.\r\n");
send(client, sendBuff, strlen(sendBuff), 0);
cout << "Bad Password" << endl;
}
}
if (cmd == "gethsi"||cmd=="GETHSI"){
... some codes here
} // end of gethsi
}
else
{
strcpy_s(sendBuff, "400 Invalid command.\r\n");
send(client, sendBuff, strlen(sendBuff), 0);
cout << "Invalid command" << endl;
}
Here is the client code:
memset(rcvbuff, 0, 512);
rcvResult = recv(ConnectSocket, rcvbuff, 512, 0); //Get welcome message
rcvbuff[rcvResult] = 0;
cout << "rcvbuf [welcome] " << rcvbuff << endl;
if (rcvbuff[0] == '2'&&rcvbuff[1] == '0'&&rcvbuff[2] == '0') {
memset(sendbuff, 0, 512);
strcpy_s(sendbuff, "auth 888");
sendResult = send(ConnectSocket, sendbuff, strlen(sendbuff), 0);
if (sendResult == SOCKET_ERROR) {
std::printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
std::printf("Bytes Sent: %ld\n", sendResult);
memset(rcvbuff, 0, 512);
rcvResult = recv(ConnectSocket, rcvbuff, 512, 0); <<-- keep waiting here
rcvbuff[rcvResult] = 0;
cout << "rcvbuf [authenticate] " << rcvbuff << endl;
The client has received the "200 Welcome" message and send out the "auth 888" message and keep waiting in " rcvResult = recv(ConnectSocket, rcvbuff, 512, 0);" as it has not receive anything from server. Server is waiting in the recv command waiting for input.
I was also thinking appending "\r\n" after the "auth 888" but very strange thing happens. See below picture. The upper half is client and lower half is server:
After appending "\r\n" to send string
Basically the server can only receive null string from the client as the size is 0.
I spend whole day and could not figure out why and hope somebody can help here. Thank you very much in advance.
P.S. I do not have enough reputation to post image. Hope somebody can help to post it for me. Thanks!
Nelson
Related
So my problem is that when I use WinSock "send" in my do-while loop, PUTTY receives it twice.
char sendBuffer[] = "this is my message to the world";
do
{
iResult = recv(clientSocket, recvBuffer, DEFAULTBUFFERLEN, 0);
if(iResult > 0)
{
std::cout << "Received bytes: " << iResult << std::endl;
std::cout << recvBuffer << std::endl;
//Successful receive, now send back a message
Message("Now it's your turn to say something!");
//std::cin.getline(sendBuffer, DEFAULTBUFFERLEN);
std::cin.get();
iSendResult = send(clientSocket, sendBuffer, sizeof(sendBuffer), 0);
if(iSendResult == SOCKET_ERROR)
{
Message("Error with send. ERROR: " + WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 8;
}
std::cout << "Bytes sent: " << iSendResult << std::endl;
}
else if(iResult == 0)
{//Nothing has been received, client has disconnected
Message("Closing connection with client");
}else
{// there was an errror and the connection needs to be closed
Message("Error Receiving. Error: " + WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 9;
}
}while(iResult > 0);
The code works otherwise works well, compiles and overall, goes smoothly, the only problem is that the send call sends it twice for some reason.
I call send once before this only to say "Welcome to the server" but that actually only sends once. It's only the one in this bit that sends twice.
I'm using mingw to compile and this is my build script
g++ server.cpp -o server -lws2_32 -lwsock32
Edit: According to my debugger, the code runs once when it receives an initial message from the client but then runs again and is receiving "\r\n" when it runs again, hence the repeated message without waiting for a new client message
So after some debugging I found out the problem, putty sends a an extra message when you press enter(or something to this effect. So the solution was to wrap the code handling received messages with an if statement with these conditions:
if((std::string)recvBuffer != "\r\n" || (std::string)recvBuffer != "\n")
I'm following the tutorial (big code block near the bottom of that section)
here:http://beej.us/guide/bgnet/output/html/multipage/advanced.html#select
And the main server code code is like so:
while (true)
{
read_fds = master;
if (select(fd_max + 1, &read_fds, NULL, NULL, NULL) == -1)
{
cerr << "ERROR. Select failed" << endl;
return -1;
}
for (int i = 0; i <= fd_max; i++)
{
if (FD_ISSET(i, &read_fds))
{
if (i == welcome_socket)
{
cout << "NEW CONNECTION" << endl;
client_len = sizeof(struct sockaddr_in);
client_sock = accept(welcome_socket, (struct sockaddr *) &client_addr, &client_len);
if (client_sock != -1)
{
FD_SET(client_sock, &master);
if (client_sock > fd_max)
{
fd_max = client_sock;
}
}
}
else
{
int length, total_read = 0;
// CONNECTION CLOSED BY CLIENT
if (safe_recv(client_sock, &length, sizeof(int)) <= 0)
{
cout << "CONNECTION CLOSED" << endl;
close(i);
FD_CLR(i, &master);
}
else
{
char *message = (char *)memset((char *)malloc(length + 1), 0, length);
// while ((total_read += safe_recv(client_sock, message + total_read, length - total_read)) < length) {}
safe_recv(client_sock, message, length);
// RESPOND WITH MESSAGE
cout << "MESSAGE: " << message << endl;
write(client_sock, process(message), length);
free(message);
}
}
}
}
}
What I'm doing is first sending (from the client) the length of the string, then the string itself. Then the server sends back process(message).
When I only have 1 connection, I'm seeing correct behaviour. However if 1 is connected already and I connect a new client, what I'm seeing is:
1st client no longer sends or receives anything from server (concluded because nothing is printed to stdout on client side)
2nd client is working as expected
When 2nd connection exits, server counts that as both connections exiting (prints CONNECTION CLOSED twice)
I've tried to keep this very similar to the tutorial code. I've run the tutorial server, and that works as intended with several clients.
I'm new to network programming, so I apologise if this is a beginner problem or just something dumb I overlooked.
The code reads from and writes to only client_sock, and client_sock is replaced with the new socket in the accept handling portion of the code.
Most likely you want to interact with i rather than client_sock.
I'm working on a client application that sends sensor data one way to a remote server. After the initial login there is no return data from the server. My problem is when the ethernet is disconnected such as a hard disconnect i.e. wireless link goes down, my application does not get a error return value after attempting a 'send' call. I am using a single non-blocking socket instance. The thread checks for a 'recv' each loop using 'select'. It does eventually get an error on 'recv' but never on 'send'.
When the remote PC has a internet connectivity loss it will cause the program to be disconnected from the server for minutes to hours before it recognises the connection loss happened and switches to re-login the server. What can be done to help detect the hard disconnect?
void checkConnect(NTRIP& server)
{
//1st check for recv or gracefully closed socket
char databuf[SERIAL_BUFFERSIZE];
fd_set Reader, Writer, Err;
TIMEVAL Timeout;
Timeout.tv_sec = 1; // timeout after 1 seconds
Timeout.tv_usec = 0;
FD_ZERO(&Reader);
FD_ZERO(&Err);
FD_SET(server.socket, &Reader);
FD_SET(server.socket, &Err);
int iResult = select(0, &Reader, NULL, &Err, &Timeout);
if(iResult > 0)
{
if(FD_ISSET(server.socket, &Reader) )
{
int recvBytes = recv(server.socket, databuf, sizeof(databuf), 0);
if(recvBytes == SOCKET_ERROR)
{
cout << "socket error on receive call from server " << WSAGetLastError() << endl;
closesocket(server.socket);
server.connected_IP = false;
}
else if(recvBytes == 0)
{
cout << "server closed the connection gracefully" << endl;
closesocket(server.socket);
server.connected_IP = false;
}
else //>0 bytes were received so read data if needed
{
}
}
if(FD_ISSET(server.socket, &Err))
{
cout << "select returned socket in error state" << endl;
closesocket(server.socket);
server.connected_IP = false;
}
}
else if(iResult == SOCKET_ERROR)
{
cout << "ip thread select socket error " << WSAGetLastError() << endl;
closesocket(server.socket);
server.connected_IP = false;
}
//2nd check hard disconnect if no other data has been sent recently
if(server.connected_IP == true && getTimePrecise() - server.lastDataSendTime > 5.0)
{
char buf1[] = "hello";
cout << "checking send for error" << endl;
iResult = send(server_main.socket, buf1, sizeof(buf1), 0);
if(iResult == SOCKET_ERROR)
{
int lasterror = WSAGetLastError();
if(lasterror == WSAEWOULDBLOCK)
{
cout << "server send WSAEWOULDBLOCK" << endl;
}
if(lasterror != WSAEWOULDBLOCK)
{
cout << "server testing connection send function error " << lasterror << endl;
closesocket(server.socket);
server.connected_IP = false;
}
}
else
{
cout << "sent out " << iResult << " bytes" << endl;
}
server.lastDataSendTime = getTimePrecise();
}
}
It is not possible to detect disconnect until you try to send something.
The solution for you is the following:
You detect that you have received no data for a certain period of time and you want to check is the connection is alive.
You send some data to the server using send function. It could be protocol-specific ping packet or either garbage. The send function returns immediately, because it does not wait for actual data send. It only fills internal send buffer.
You begin waiting for socket read.
While you are waiting, OS tries to send the data in the send buffer to the server.
When OS detects that it cannot deliver data to the server, then the connection is marked as erroneous.
Now you will get an error when calling recv and send functions.
The send timeout is system specific and can be configured. Usually, it is about 20 seconds (Linux) - 2 minutes (Windows). It means that you need to wait a lot before you receive an error.
Notes:
You can also turn on TCP keep alive mechanism, but I don't recommend you to do this.
You can also modify TCP timeout intervals. It can be helpful when you want the connection to survive the temporary network disconnect.
That's how TCP works and is intended to work. You will get an error from a subsequent send, but never from the first send after the disconnect. There is buffering, and retry, and retry timeout to overcome before an error is signalled.
I'm making a server, client app in c++ console based.
What I did so far:
I can connect to the server.
I can send messages to the server.
The server can send the messages back.
But what I can't figure out, how can I let the server act also as a client to send messages to the client while he is processing received messages from the client?
People can use it as an example as well :D
Well I will post also some parts of the code:
server:
#include "stdafx.h"
using namespace std;
//our main function
void main()
{
int numClients;
long antwoord;
char chatname[100];
char bericht[250]; //messages
char sbericht[250]; //smessages
//here we set the Winsock-DLL to start
WSAData wsaData;
WORD DLLVERSION;
DLLVERSION = MAKEWORD(2,1);
//here the Winsock-DLL will be started with WSAStartup
//version of the DLL
antwoord = WSAStartup(DLLVERSION, &wsaData);
if(antwoord != 0)
{
WSACleanup();
exit(1);
}
else
{
cout << "WSA started successfully" <<endl;
cout << "The status: \n" << wsaData.szSystemStatus <<endl;
}
//the DLL is started
//structure of our socket is being created
SOCKADDR_IN addr;
//addr is our struct
int addrlen = sizeof(addr);
//socket sListen - will listen to incoming connections
SOCKET sListen;
//socket sConnect - will be operating if a connection is found.
SOCKET sConnect;
//setup of our sockets
//opgezocht op internet - AF_INET bekend dat het lid is van de internet familie
//Sock_STREAM betekenend dat onze socket een verbinding georiƫnteerde socket is.
sConnect = socket(AF_INET,SOCK_STREAM,NULL);
//now we have setup our struct
//inet_addr is our IP adres of our socket(it will be the localhost ip
//that will be 127.0.0.1
addr.sin_addr.s_addr = inet_addr("192.168.1.103");
//retype of the family
addr.sin_family = AF_INET;
//now the server has the ip(127.0.0.1)
//and the port number (4444)
addr.sin_port = htons(4444);
//here we will define the setup for the sListen-socket
sListen = socket(AF_INET,SOCK_STREAM,NULL);
if (sConnect == INVALID_SOCKET)
{
cout << "Error at socket(): \n" << WSAGetLastError() <<endl;
WSACleanup();
}
else
{
cout << "Connect socket() is OK!" <<endl;
}
if(sListen == INVALID_SOCKET)
{
cout << "Error at socket(): \n" << WSAGetLastError() <<endl;
WSACleanup();
}
else
{
cout << "Listen socket() is OK!" <<endl;
}
//here the sListen-socket will be bind
//we say that the socket has the IP adress of (127.0.0.1) and is on port (4444)
//we let the socket become the struct "addr"
if(bind(sListen, (SOCKADDR*)&addr, sizeof(addr)) == SOCKET_ERROR)
{
cout << "bind() failed: \n" << WSAGetLastError() <<endl;
WSACleanup();
exit(1);
}
else{
cout << "bind() is OK!" <<endl;
}
//here we will tell what the server must do when a connection is found
//therefor we will create an endless loop
cout << "Waiting for a incoming connection..." <<endl;
for(;;)
{
//now we let the socket listen for incoming connections
//SOMAXCOMM heeft het nut dat het dan voordurend luisterd naar inkomende verbindingen zonder limiet
listen(sListen, SOMAXCONN);
while(numClients < SOMAXCONN)
{
//if a connection is found: show the message!
if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
{
cout << "A Connection was found!" <<endl;
antwoord = send(sConnect, "Welcome to our chat:", 21,NULL);
if(antwoord > 1)
{
antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL);
antwoord = recv(sConnect, chatname, sizeof(chatname), NULL);
while(antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL) && (antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL)) )
{
antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL);
antwoord = send(sConnect, chatname, sizeof(chatname), NULL);
}
}
else
{
cout << "The connection to the client has been lost... \n" << "please exit the server." <<endl;
break;
}
numClients++;
}
}
}
}
Client:
// ChatServer.cpp : Defines the entry point for the console application.
//
//include of the stdafx.h file where importent files are being included
#include "stdafx.h"
using namespace std;
void smessage()
{
}
//our main function
int main()
{
//here we set the Winsock-DLL to start
string bevestiging;
char chatname[100];
char bericht[250];
char sbericht[250];
string strbericht;
string strsbericht;
long antwoord;
//here the Winsock-DLL will be started with WSAStartup
//version of the DLL
WSAData wsaData;
WORD DLLVERSION;
DLLVERSION = MAKEWORD(2,1);
antwoord = WSAStartup(DLLVERSION, &wsaData);
if(antwoord != 0)
{
exit(1);
}
else
{
cout << "WSA started successfully" <<endl;
cout << "The status: \n" << wsaData.szSystemStatus <<endl;
}
SOCKADDR_IN addr;
int addrlen = sizeof(addr);
SOCKET sConnect;
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
if (sConnect == INVALID_SOCKET)
{
cout << "Error at socket(): \n" << WSAGetLastError() <<endl;
}
else
{
cout << "socket() is OK!\n" <<endl;
}
addr.sin_addr.s_addr = inet_addr("192.168.1.103");
addr.sin_family = AF_INET;
addr.sin_port = htons(4444);
cout << "What is your chat name?" <<endl;
cin.getline(chatname, 100);
cout << "Do you want to connect to the server? [Y/N]" <<endl;
cin >> bevestiging;
if (bevestiging == "N")
{
exit(1);
}
else
{
if(bevestiging == "Y")
{
connect(sConnect, (SOCKADDR*)&addr, sizeof(addr));
antwoord = recv(sConnect, bericht, sizeof(bericht), NULL);
strbericht = bericht;
cout << strbericht << chatname <<endl;
while(true)
{
if(antwoord > 1)
{
cin.clear();
cin.sync();
cout << chatname << " :" <<endl;
cin.getline(sbericht, 250);
antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL);
antwoord = send(sConnect, chatname, sizeof(chatname), NULL);
while(antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL) && (antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL)))
{
antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL);
antwoord = recv(sConnect, chatname, sizeof(chatname), NULL);
cout << chatname << ":" <<endl;
cout << sbericht <<endl;
cin.getline(sbericht, 250);
}
}
else
{
cout << "The connection to the server has been lost... \n" << "please exit the client." <<endl;
}
}
}
}
}
You would probably have to open another socket. The client would have to act as a server as well.
First of all: putting a 20mb zip file in to the web for about 4 interesting source files is not a good option. Your object files and debug output is of no interest to us, since we want to help with your source code. Try uploading a zip file containing only the source files the next time.
Secondly: If others want to understand your source code and are not familiar with your native language, they have to guess. Try using english as source code language for this and a variety of other reasons.
Now to answer your question:
The answer is already in your code. Currently, the server is looping until a maximum number of connects, receives input and sends back an answer. So actually you have already implemented it. I guess if you want to send initiated messages in both ways you have to alter your software architecture a bit.
Your code has a few fundamental problems:
The server can only handle one client at a time. If your server will ever have more than a single user on it (as a chat server invariably will), you need to be able to listen for more than one connection at once. select, or WSAEventSelect and WaitForMultipleObjects, would help a lot here.
You assume that a whole fixed-size message will appear at a time. TCP can not guarantee that (as the "stream" concept considers the data as just a potentially infinite sequence of individual bytes), and a half-sent message could freeze up your server while it waits for the rest. Not a big deal if this is all on your LAN, but if you expose this service to the internet, you're asking for random lockups. In order to prevent that, get the data and put it in a buffer as it comes, processing it only when you have a whole message.
The conversation is done in lock-step. That is, the client sends a message, and waits for a response, and then (and only then) expects console input. With this design, there will always be one message received per message sent. In order to get around this, i'll often have a thread for the data going in each direction -- one that gets the console input and sends it to the server, while the other listens to the server and prints the message received. (Note, this means messages could be received while you're typing. That's kinda the point. But it makes console input a bit annoying.) Threading is a semi-advanced topic -- once you start creating new threads, you often have to worry about synchronization and such. But it's generally cleaner than the alternatives in this case.
Sample threaded code (very roughly, since i don't have a C++ compiler handy):
const int MessageLength = 250;
const int NameLength = 250;
char myname[NameLength];
bool sendFully(SOCKET s, char* buffer, size_t buffer_len, int flags)
{
char *end = buffer + buffer_len;
while (buffer != buffer_len)
{
int sent = send(s, buffer, end - buffer, flags);
if (sent == 0) return false;
buffer += sent;
}
return true;
}
DWORD WINAPI watchConsoleInput(void*)
{
char input[MessageLength];
while (true)
{
std::cin.getline(input, MessageLength);
if (!sendFully(sConnect, input, sizeof(input), 0))
break;
if (!sendFully(sConnect, myname, sizeof(myname), 0))
break;
}
return 0;
}
int main()
{
char chatname[NameLength];
char sbericht[MessageLength];
... get our name in myname ...
... do the connect stuff ...
HANDLE watcher = CreateThread(NULL, 0, watchConsoleInput, NULL, 0, NULL);
while (true)
{
// Added MSG_WAITALL to work around the whole-message-at-a-time thing
if (recv(sConnect, sbericht, sizeof(sbericht), MSG_WAITALL) != sizeof(sbericht))
break;
if (recv(sConnect, chatname, sizeof(chatname), MSG_WAITALL) != sizeof(sbericht))
break;
}
// Don't care about errors; we're just being polite
shutdown(sConnect, SD_BOTH);
closesocket(sConnect);
cout << "Connection lost\n";
// ExitProcess rather than just 'return', so we know the watcher thread dies
ExitProcess(0);
}
I implented a socket client to communicate to an ip camera with RTSP over HTTP to get teh video from the camera.
To stablished the communication with the camera, first i have to set an HTTP-GET tunnel, then send the RTSP commands. When the camera loses the connection, the program has to close the tunnel handler, finish the thread and when the process return to the main function, it begins the communication (start the treads, and so on).
On the reconnection: the http-get tunnel is set ok, i mean, the socket connects and receives a "HTTP OK", so the program sends a RTSP "DESCRIBE" but the recv always return an EAGAIN error. I check with wireshar that the DESCRIBE OK response is sent from the camera, but the recv never gets it.
Here is the code:
struct sockaddr_in aServer;
// string myData;
char *myData=new char [256];
connection *c=(connection*)vargp;
memset(&aServer, 0, sizeof aServer);
aServer.sin_family = AF_INET;
aServer.sin_addr.s_addr = inet_addr(c->theServer.c_str());
if (aServer.sin_addr.s_addr == INADDR_NONE)
{
struct hostent *hp;
hp = gethostbyname(c->theServer.c_str());
if (hp != NULL)
{
memcpy(&aServer.sin_addr, hp->h_addr, hp->h_length);
aServer.sin_family = hp->h_addrtype; //Protocol family
}
else
cout << "Failed to resolve " << c->theServer.c_str() << ": " << hstrerror(h_errno) << endl;
}
aServer.sin_port = htons(c->thePort);
c->fd_get = socket(AF_INET, SOCK_STREAM, 0);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(c->fd_get, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
if (c->fd_get < 0){
cout << "fd_get < 0" << endl;
c->bFin=true;
c->WakeUP();
}
if (connect(c->fd_get, (struct sockaddr *) &aServer, sizeof aServer) < 0){
cout << "connect fd_get < 0" << endl;
c->bFin=true;
c->WakeUP();
}
if(!c->bFin){
sprintf(myData, "GET %s HTTP/1.1\r\n", c->theURI.c_str());
sprintf(myData, "%sx-sessioncookie: %s\r\nAccept: application/x-rtsp-tunnelled\r\nAuthorization: %s\r\n\r\n", myData,c->theHTTPSessionId.c_str(), c->addAuthorization(c->aGlobalUsername, c->aGlobalPassword).c_str() );
cout << myData << endl;
write(c->fd_get, myData, strlen(myData));
//LISTENING...
int theLen=1500; //3000;
int ret=0;
unsigned char datosRecibidos[3000];
int flags =fctl(c->fd_get, F_GETFD;
if((flags & O_NONBLOCK) == O_NONBLOCK){
fprint(stderr, "yup, its nonblocking");
}
else{
fprint(stderr, "nope, its blocking");
}
while (c->bFin==false){
ret = read(c->fd_get, ReceivedData, theLen);
// ret= recvfrom(c->fd_get, ReceivedData, theLen, 0, (struct sockaddr *) 0, (socklen_t*)0);
if (ret == 0)
{
cout << "Server closed connection: 0" << endl;
}
else
if (ret == -1){
fprintf (stderr, "\n[%d]: %s %d\n", __LINE__, strerror (errno), errno);
if(errno==107 ||errno==EAGAIN){
cout << "errno" << endl;
c->bFin=true;
c->WakeUP();
cout << "vuelta wakeUP" << endl;
break;// empezar de nuevo
}else{
cout << "errno" << endl;
}
}
else{
//cout << (string)ReceivedData[0]<< endl;
c->ProcessReceivedData(ReceivedData, ret);
usleep(10);
}
}
close(c->fd_get);
c->fd_get = -1;
}
Could it be a timeout problem? or a stack problem? How can i solve it?
Thanks in advance for your help. Best regards.
cristina
EAGAIN means there is no data available for reading on a non-blocking socket. So, you should run the recv call again.
You haven't actually posted enough code to suggest there is a programming fault, although can I ask if when you detect the connection is closed that you also close down your end as well before re-establishing everything?
Is your socket open in O_NONBLOCK mode? You can check like this:
int flags = fcntl(fd, F_GETFD);
if ((flags & O_NONBLOCK) == O_NONBLOCK) {
fprintf(stderr, "Yup, it's nonblocking");
}
else {
fprintf(stderr, "Nope, it's blocking.");
}
In nonblocking mode, recv will return immediately with errno set to EAGAIN if there is nothing to receive yet.
I would always use poll() or select() before going for each socket read operation.
Also to test for NON-Blocking:
int flags = fcntl(fd, F_GETFL, 0);
if (flags & O_NONBLOCK) {
fprintf(stderr, "Yup, it's nonblocking");
} else {
fprintf(stderr, "Nope, it's blocking.");
}