Newbie: Implementing sockets with error handling

Hi! i implemented sockets in my project, i know how to connect to a server and recieve data etc..but i have no clue how to handle error messages. I am also new to writing classes.
Any help appreciated
[213 byte] By [underplay] at [2007-11-18 22:11:19]
# 1 Re: Newbie: Implementing sockets with error handling
Can u point out which socket class u r using or which socket model you r using .

if you r using CSocket or ASyncSocket class then MSDN is the best for reference

and if u r using socket models then most of the functions return SOCKET_ERROR when any error occor.
you can check the actual error by calling WSAGetLastError .

can u specify errors which u want to handle because there are lot of socket errors.
Naumaan at 2007-11-9 13:47:04 >
# 2 Re: Newbie: Implementing sockets with error handling
Im using ASyncSocket. I thought im suppost to have a seperate header and source file with that class that the wizard created. But i will use the function you gave me and mess around a bit, also the errors im trying to catch are just regular errors so my program will not crash/give errors such as timeout and address already in use. Most of the errors i have found in msdn.
underplay at 2007-11-9 13:48:06 >
# 3 Re: Newbie: Implementing sockets with error handling
There are few functions which you should take care

Create
Listen
Connect
Accept
Send
Receive

1= Create function if failed then it return 0 so you can check wether socket is successfully created of not.
( Create function mostly gives error when you did not called AfxSocketInit() in ur app before creating sockets )

2= Listen function if failed then it return 0 you can check the actual error by GetLastError function
( Listen function mostly gives error when another socket is already listening on the same port which u specify )

3= Connect function if failed then it return 0

( when host is unreachable or network is down then connect function mostly gives error )

4= Accept function also return 0 if failed
( if your listening and connecting sockets are successfully created and there is not network problem then accept function mostly successfull )

5= Send function if failed then it return SOCKET_ERROR otherwise return total number of sended characters.
( if you are using blocking sockets then send function may return WSAEWOULDBLOCK error )

6= Receive function also return SOCKET_ERROR otherwise it will return total number of bytes received.
( Receive function may return WSAEINPROGRESS or WSAEWOULDBLOCK error )

check for each function return value from msdn
these all functions if not complete successfully did not crash ur application you can take appropriate action according to your requirenment on these errors .
Naumaan at 2007-11-9 13:49:06 >
# 4 Re: Newbie: Implementing sockets with error handling
Thanks! that was very useful, i have 1 last question, will this code work for handling the messages? or do i use WSAGetLastError?

#DEFINE TimeOut 10060

int Error = W.Connect();
if(Error == TimeOut)
{
MessageBox("Timed Out");
}
underplay at 2007-11-9 13:50:11 >
# 5 Re: Newbie: Implementing sockets with error handling
No actually as connect function if failed then it will return 0 so if timeout occor then you will receive 0.
and the specific error code can be retrieved by calling GetLastError.

you can write this

#DEFINE TimeOut 10060
int Error;
if( W.Connect()==0)
{
Error=GetLastError();
if(Error == TimeOut)
{
MessageBox("Timed Out");
}
}
Naumaan at 2007-11-9 13:51:11 >
# 6 Re: Newbie: Implementing sockets with error handling
omg...everything just became so clear! thx for the help!!!
underplay at 2007-11-9 13:52:10 >
# 7 Re: Newbie: Implementing sockets with error handling
[Moved thread]
Andreas Masur at 2007-11-9 13:53:14 >