Thread: Help for Socket Programming C++

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Help for Socket Programming C++

    Im still new to socket programming in C++. Currently i am doing a client-server communication. Not very understand how the code meant. Can someone help me to teach line-by-line the codes meaning.

    The code as follows:

    Code:
    void CSampleClientDlg::OnButtonSend() 
    {
    	SOCKET TempSoc;
    	TempSoc = (SOCKET)m_hComm;
    	TCHAR szBuf[255]={0};
    	m_bDataToSend = TRUE;
    	CString strText = "";
    	m_Send.GetWindowText(strText);
    	_tcscpy(szBuf,strText);
    	int nRet = send(TempSoc,szBuf,strText.GetLength(),0);
    	if(nRet == SOCKET_ERROR)
    		AfxMessageBox("Error in Sending");
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Using easy words, the winsock work on that piece of code can be understand as:

    Code:
    SOCKET TempSoc;
    TempSoc = (SOCKET)m_hComm;
    Here you assign a global variable 'm_hComm' to a local var (to simplify the work).

    Code:
    int nRet = send(TempSoc,szBuf,strText.GetLength(),0);
    'send' is a winsock function that sends the text 'szBuf' (wich length is 'strText.GetLength()') to the socket 'TempSock'; that function returns the length of the text that have been received by the other side or an error code like 'SOCKET_ERROR'. In that case, if 'nRet==strText.GetLength()' (after the send) means that you have send all the data, else means that the other side only have received the first 'nRet' characters of 'strText'.

    Hope that will help; also you can take a look to the 'Useful links and books' thread on that networking board.

    Niara
    Last edited by Niara; 04-02-2007 at 04:48 AM.

Popular pages Recent additions subscribe to a feed