Home > Software engineering >  Why to save the socket to the array is invalid?
Why to save the socket to the array is invalid?

Time:09-20


I want to write a TCP one-to-many communication program, is the server side using multithreading technology (do not use the Select any model), the server is able to provide all the connected messaging client, I consulted some other program, found that the basic idea is to each thread in the accept () function returns the sockets are saved to a socket word group, socket address also save to an array, and then in the "mass" button in the message, using the for loop iterates through the socket set, loop through the send () function sends a message to all sockets in the socket set of words, I can use the send () function sends SOCKET_ERROR error, output error code is 10038, invalid socket, illustrate the socket didn't save to the socket set of words, so also preserve the socket in the list structure, not, would you please tell me why this is, my key code is as follows:
 BOOL CListWinDlg: : OnInitDialog () 
{
CDialog: : OnInitDialog ();

SetIcon (m_hIcon, TRUE);//Set the big icon
SetIcon (m_hIcon, FALSE);//Set the small icon

WSADATA WSADATA;
The WORD sockVersion=MAKEWORD (2, 2);

SockSer=socket (AF_INET SOCK_STREAM, 0).
AddrSer. Sin_family=AF_INET;
AddrSer. Sin_port=htons (5566);
AddrSer. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");
Int len=sizeof (SOCKADDR);
Bind (sockSer, (SOCKADDR *) & amp; AddrSer, len);
Listen (sockSer, 5);

RECVPARAM * pRecvParam=new RECVPARAM;
PRecvParam - & gt; The sock=sockSer;
PRecvParam - & gt; HWND=m_hWnd;
HANDLE hThread=CreateThread (NULL, 0, WaitProc, (LPVOID) pRecvParam, 0, NULL);
The CloseHandle (hThread);
return 0;
}

DWORD WINAPI CListWinDlg: : WaitProc (LPVOID lpParameter)//accept connection thread
{
CListWinDlg * pServer=new CListWinDlg;
The SOCKET sock=((RECVPARAM *) lpParameter) - & gt; The sock.
HWND HWND=((RECVPARAM *) lpParameter) - & gt; The HWND;
The SOCKET sockConn;
SOCKADDR_IN addrCli;
Int len=sizeof (SOCKADDR);
Int num=0;
While (1)
{
Sleep (10);//what's the use of the sentence?
SockConn=accept (the sock, (SOCKADDR *) & amp; AddrCli, & amp; Len);


If (INVALID_SOCKET==sockConn)
{
Cstrings strNotice="the accept () fails, try again... ";
: : AfxMessageBox (strNotice);
continue;
}
The else {AfxMessageBox (" a client has been successfully connected ");

//pServer - & gt; M_listConn. Push_back (addrCli);
//pServer - & gt; M_sockConn. Push_back (sockConn);
PServer - & gt; M_Clients/num=sockConn;//to save the socket to the array,
PServer - & gt; AddrFrom/num=addrCli;
num++;
}//to save the socket address to array
//int num2=sizeof (pServer - & gt; M_Clients)/sizeof (pServer - & gt; M_Clients [0]);
}

return TRUE;
}
Void CListWinDlg: : OnSend ()
{
Char buff [200].
Char * ct;
CTime time=CTime: : GetCurrentTime ();//get the current time
Cstrings t=time. The Format (" M H % : % : % S ");//set the time display format
Ct=(char *) t.G etBuffer (0);//cstrings char *
C_sendbuf. GetWindowText (buff, 200);
C_sendbuf. SetWindowText (NULL);
Cstrings Ser="server: & gt;";
Strcat (buff, ct);
Int iSendFalseCount=0;
CListWinDlg * pServer=new CListWinDlg;


//for (sockiterator=m_sockConn. The begin (); Sockiterator!=m_sockConn. End (); + + sockiterator)
//for (list : : iterator itr=pServer - & gt; M_sockConn. The begin (); Itr!=pServer - & gt; M_sockConn. End (); Itr++)
for(int i=0; i<3; I++)
{

Int vetcal=send (pServer - & gt; M_Clients [I], buff, strlen (buff) + 1, 0);
If (vetcal==SOCKET_ERROR)
ISendFalseCount++;
}
ISendFalseCount=WSAGetLastError ();
Char strtemp [5].
Itoa (iSendFalseCount, strtemp, 10);
AfxMessageBox (strtemp);


}

Header files are defined in the array is as follows:
 struct RECVPARAM 
{
The SOCKET sock;
The HWND HWND;
};
The class CListWinDlg: public CDialog
{
//Construction
Public:
PParent CListWinDlg (CWnd *=NULL);//standard constructor

The SOCKET sockSer sockConn;
The SOCKET m_Clients [10].//save the socket array
SOCKADDR_IN addrFrom [10].

List M_sockConn;
ListList : : iterator sockiterator;
SOCKADDR_IN addrSer;
List The test;

List : : iterator testiterator;
The static DWORD WINAPI WaitProc (LPVOID lpParameter);
The static DWORD WINAPI RecvProc (LPVOID lpParameter);

CodePudding user response:

With clients to connect to? What is the SOCKET parameters when the send? The TRACE log or set breakpoints view

CodePudding user response:

When you are in the preservation and use respectively
CListWinDlg * pServer=new CListWinDlg;

So you save and use is not the same object instance, the two objects of data is not Shared
You should use the same, or using a static array

CodePudding user response:

Client connection is successful, the server can receive any client sends the message, only the server can't send messages to the client,

What is the SOCKET parameters when the send? The TRACE log or set breakpoints view these I don't know how to do

CodePudding user response:

CListWinDlg * pServer=new CListWinDlg;
Is a new object that has nothing to do with the original dialog.

WaitProc (LPVOID lpParameter)//the dialog window

Dialog box to define
RECVPARAM RECVPARAM;

Or
RECVPARAM * pRecvParam=new RECVPARAM;
Put outside the class

CodePudding user response:

Do you have waiting for the client receives the message? If not then no,

CodePudding user response:

Know the problem, define the save socket array in chatDlg. CPP is ok, the original is to define in chatDlg. Not in h, heard that is to solve the problem of connection repeated definition, but why in the * DLG. CPP file will solve the problem of the repeated definition? Still don't understand,
//* * * * * * * in chatDlg originally defined h, now moved to here, to solve the problem of connection repeated definition 
The SOCKET * m_Clients [10];//save the socket array
SOCKADDR_IN addrCli [10].
int num;
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
nullnullnull
  • Related