C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 04-02-2007, 01:41 AM   #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");

}
cplusplus.Jr is offline   Reply With Quote
Old 04-02-2007, 04:43 AM   #2
Registered User
 
Join Date: Mar 2005
Location: Juneda
Posts: 229
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.
Niara is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 04:00 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22