int dataLength;
int readLength = read(conn->getFd(), &dataLength, HEADER_SIZE);
printf("Header read %d \n", readLength);
if(readLength == 0){
removeClient(conn);
close(conn->getFd());
}
else {
/*
* Do not parse anything here.
* Pass the raw da开发者_如何学编程ta to the work handler.
*/
char *buf = new char[dataLength+1];
int dataReadLength = read(conn->getFd(), buf, dataLength);
printf("Data read %d \n", dataReadLength);
printf("ServerManager::eventAcceptLoop, A message has been received \n");
addWork(conn->getFd(), buf, dataReadLength);
I am working on a network programming and I have this code. It works fine in localhost (no error occur) but I am sure it is going to blow up if I try this on real world network. The problem is that I am not an expert of network programming and not sure which error handle I am supposed to add. It was also had to find a good example or tutorial including specific error handling.
I will be very appreciated if you guys can provide me a good example or tutorials that might help to get the error handling.
Thanks in advance.
You need to analyze the case where read()
returns -1
.
Check this tutorial/reference material: Beej's Guide to Network Programming It's my go-to reference material for network programming.
精彩评论