|
-
December 24th, 2009, 05:36 AM
#10
Re: invalid socket in accept()
1. taking your code and modifying it only slightly should work:
Code:
void CComputer::OnButtonStart()
{
...
//looping, dengan menggunakan thread
AfxBeginThread(&thread,this);
}
UINT thread(LPVOID p)
{
int iSize;
int s=1,msgcount,loop=1,flag=0;
CString data, data_send;
CComputer *dlg=reinterpret_cast<CComputer*>(p);
...
}
Please note that I replaced your cast instruction by the reinterpret_cast because that is more C++ like.
So, provided that your Dialog is still "alive", i. e. it is not destroyed, your thread should be able to access the variables. But if you close the dialog right after OnButtonStart exits, your thread might access invalid data.
2. Moving the code you already have would yield this:
Code:
void CComputer::OnButtonStart()
{
// TODO: Add your control notification handler code here
WSADATA wsd;
CString str, m_sError;
int iSize;
CString status;
CWaitCursor wait;
UpdateData(TRUE);
for (int i=0; i<40; i++ )
sClient[i] = NULL;
//cek port input
if(m_sPort.IsEmpty())
{
AfxMessageBox("Please Insert Port Number...!!");
m_cStartBtn.EnableWindow(TRUE);
return;
}
nClients=0;
// status.Format(" running");
m_sStatus.SetWindowText("running");
//refresh database
SetTimer(1,1000,NULL);
m_cStartBtn.EnableWindow(FALSE);
m_cStopBtn.EnableWindow(TRUE);
//looping, dengan menggunakan thread
AfxBeginThread(&thread,this);
}
UINT thread(LPVOID p)
{
int iSize;
int s=1,msgcount,loop=1,flag=0;
CString data, data_send;
// cek winsock2
if (WSAStartup(MAKEWORD(2,2), &wsd) !=0)
{
AfxMessageBox("Failed to Load Winsock....!!");
return;
}
// inisialisasi soket dan port
sServer=socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sServer ==SOCKET_ERROR)
{
m_sError.Format("socket() failed : %d ", WSAGetLastError());
AfxMessageBox(m_sError);
return;
}
//port
sadd_Server.sin_family = AF_INET;
sadd_Server.sin_port = htons(atoi(m_sPort));
sadd_Server.sin_addr.s_addr = htonl (INADDR_ANY);
iSize = sizeof(sadd_Server);
int opt,reuse;
reuse = setsockopt(sServer, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof (opt));
if (reuse == SOCKET_ERROR)
{
//close(); // close.
m_sError.Format("failed reuse socket : %d ", WSAGetLastError());
return;
}
// bind : mengikat antara aplikasi dengan port yang dibuat
if(bind(sServer, (struct sockaddr *)&sadd_Server, iSize) == SOCKET_ERROR)
{
m_sError.Format("bind() failed : %d " , WSAGetLastError());
AfxMessageBox(m_sError);
return;
}
nClients=0;
//listen port dengan backlog maksimum
listen(sServer,SOMAXCONN);
CComputer *dlg=reinterpret_cast<CComputer*>(p);
msgcount=dlg->nClients;
iSize=sizeof(dlg->sadd_Client);
dlg->sClient[msgcount]=accept(dlg->sServer,(struct sockaddr *)&(dlg->sadd_Client),&iSize);
if (dlg->sClient[msgcount]==INVALID_SOCKET)
{
AfxMessageBox("Error : invalid socket");
dlg->m_cStartBtn.EnableWindow(TRUE);
return 0;
}else
{
AfxMessageBox(" socket is ready");
dlg->m_cStartBtn.EnableWindow(TRUE);
}
return 0;
}
This might not compile at once, but you should get an idea of how it works.
You should also get into the habit to declare variables not on the top of the function but at the moment you need it. And don't forget to initialize the variables. In your call to setsockopt "opt" is not initialized and the result of the function call is unpredictable.
The declaration of int iSize could be moved to
Code:
int iSize = sizeof(sadd_Server);
I'm off for Christmas now, I wish you a good time playing around with this code. Make sure the CComputer dialog exists until the thread ends.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|