help needed with CSocket

hi im aswin,
i need help recording CSocket. i want to know the difference between csocket and CAsyncSocket and which is the best to use.

1) When i use CSocket if one application acts as a server and other as client if the client disconnect abruptly how do i know that.

2) How do i make sure client has received correct datat the server has sen.

3) how do i send file using CSocket which may be more then 1 MB.

4) how do i make sure that a port isnt used by any other application.

5) Consider i use create command with port 1102. but when i use getport method i see that it shows different port.

plz help me in this situation as this is my first day in a company after graduating this april.

Regards
Aswin.N.Paranji
India
[797 byte] By [Aswin1980] at [2007-11-18 13:41:11]
# 1 Re: help needed with CSocket
CSocket is derived from CAsyncSocket and is able to send and receive messages with the help of the Serialize methode of CObject. so you do not have to check wether all data are transmitted correctly.

what do you mean with
'4) how do i make sure that a port isnt used by any other application.' ?

do you mean an application using a port currently or an application that could use this port anytime?
pgrohs at 2007-11-11 2:09:21 >
# 2 Re: help needed with CSocket
Aswin...

you said you need to ... record CSocket? what does this mean? do you want to create a log file for incoming/outgoing data?

the difference between CSocket & CAyncSocket as described in msdn in short is:

This class derives from CAsynCSocket and inherits its encapsulation of the Windows Sockets API. A CSocket object represents a higher level of abstraction of the Windows Sockets API than that of a CAsynCSocket object. CSocket works with classes CSocketFile and CArchive to manage the sending and receiving of data.

1) When i use CSocket if one application acts as a server and other as client if the client disconnect abruptly how do i know that.

Ans: As the client connected to a server disconnects itself it sends a msg to the server. And server gets OnClose msg. You just need to overRide the function OnClose() of your CSocket class to know about a client left or not.

2) How do i make sure client has received correct datat the server has sen.

If you just need to check whether its sending/receiving any data or not... you may Check it through putting up AfxMessageBox'es. And you if may need to check the validity of the data for security purposes... then i guess... you need to implement a CRC or somthing which can do the redundancy check for you, to make sure the data is correct.

3) how do i send file using CSocket which may be more then 1 MB.

First, if you need to send a file you shud be starting an FTP session. Check out CFtpConnection class.

Second, Check CSocketFile class.

Third, if you still need to send the file using these UDP sockets.. then you might be coming up with a "workarounded" idea.. that... you shud send the file in chunks... and nodata shud be sent or received... while file is being... transfered to from server to client or vice versa.

4) how do i make sure that a port isnt used by any other application.

If a port which you are trying to use is already in use... then you wont be able to acquire it for the listening of your server. But if your application is listening on a certain port than no application will be able to use that port unless your server stops listening.

So use any port from 0 to 35515, but the ports from 0 to 1024 are mostly in use of Operating system(in case of Windows platform).

5) Consider i use create command with port 1102. but when i use getport method i see that it shows different port.

When a client gets connected to a server listening, for example on port 1102, the server redirects the client to a new port... to get it connected to it.

All you need to make list... of the ports of the connected clients... and use them when necessary.

For example on the server side:

BOOL CServerListenDlg::OnNewConnection()
{
//create a new socket
CServerSocket* pSocket= new CServerSocket(this);

//accept the incoming connection
m_pSockListen->Accept(*pSocket);

//add the connected client to a list. in this case its a CArray
m_caClients.Add(pSocket);

CString strPeerAddress;
UINT nPeerPort=0;
pSocket->GetPeerName(strPeerAddress,nPeerPort);
CString sPort;
sPort.Format("%u",nPeerPort);
AfxMessageBox(strPeerAddress +" - "+ sPort);
return TRUE;

}

plz help me in this situation as this is my first day in a company after graduating this april.

Dont worry if its your first day... there'll be more days to come in this company for you ;)

might you be needing any help do let me know.
jusstujoo at 2007-11-11 2:10:24 >
# 3 Re: help needed with CSocket
I received you private msg. Come here so that if can be of use to everyone.

i would like to know about what is isBlocking in CSocket.

A socket can be in "blocking mode" or "nonblocking mode." The functions of sockets in blocking (or synchronous) mode do not return until they can complete their action. This is called blocking because the socket whose function was called cannot do anything is blocked until the call returns. A call to the Receive member function, for example, might take an arbitrarily long time to complete as it waits for the sending application to send (this is if you are using CSocket, or using CAsyncSocket with blocking). If a CAsyncSocket object is in nonblocking mode (operating asynchronously), the call returns immediately and the current error code, retrievable with the GetLastError member function, is WSAEWOULDBLOCK, indicating that the call would have blocked had it not returned immediately because of the mode. (CSocket never returns WSAEWOULDBLOCK. The class manages blocking for you.)
jusstujoo at 2007-11-11 2:11:17 >