Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you connect more than one device?
I can connect to one device using SOCKETS (winsock2.h) and talk to it via TCP but when I try to connect to another device while the first device is connected, I get connected and talks but eventually will crash.
First connected source code:

void CIPtestDlg::OnConnectToSocket()
{
char MyIPaddress[16];// = {"192.168.2.50"};
char PortNumb[16], ClientPort[16];
int ClientportNumb;
memset(MyIPaddress,0,16);
memset(PortNumb,0,16);
memset(ClientPort,0,16);

m_ctlIPAddress.GetWindowText(MyIPaddress,16);
m_ctlPortNumber.GetWindowTextA(PortNumb,5);
m_ctlClientPort.GetWindowTextA(ClientPort,6);

if(strlen(ClientPort)==0 ||strlen(PortNumb)==0 || strlen(MyIPaddress) < 6)
{
AfxMessageBox("Please fill in Port Number or Client Port Number or IP Address");
return;
}

ClientportNumb = atoi(ClientPort);
int error = WSAStartup (0x0202, &w); // Fill in WSA info
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET)
{
AfxMessageBox( "Failed to connect.\n",MB_OK,0 );
WSACleanup();
return;
}
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( MyIPaddress );
clientService.sin_port = htons( ClientportNumb );
SenderAddrSize = sizeof(clientService);


if( connect( ConnectSocket, (SOCKADDR *)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
AfxMessageBox( "Failed to connect.\n",MB_OK,0 );
WSACleanup();
return;
}
AfxMessageBox("Connected to server.\n", MB_OK, 0);
m_ctlIsConnected.SetCheck(TRUE);

return;
}

The 2nd connect is just like the first except some variables have been changed:
void CIPtestDlg::OnBnClickedConnect2()
{
char MyIPaddress[16];
char PortNumb[16], ClientPort[16];
int ClientportNumb;
memset(MyIPaddress,0,16);
memset(PortNumb,0,16);
memset(ClientPort,0,16);

m_ctlIPAddress2.GetWindowText(MyIPaddress,16);
m_ctlPortNumber2.GetWindowTextA(PortNumb,5);
m_ctlClientPort.GetWindowTextA(ClientPort,6);

if(strlen(ClientPort)==0 ||strlen(PortNumb)==0 || strlen(MyIPaddress) < 6)
{
AfxMessageBox("Please fill in Port Number and/or IP Address");
return;
}
ClientportNumb = atoi(ClientPort);
int error = WSAStartup (0x0202, &w2); // Fill in WSA info
ConnectSocket2 = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (ConnectSocket2 == INVALID_SOCKET)
{
AfxMessageBox( "Failed to connect.\n",MB_OK,0 );
WSACleanup();
return;
}
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService2.sin_family = AF_INET;
clientService2.sin_addr.s_addr = inet_addr( MyIPaddress );
clientService2.sin_port = htons( ClientportNumb );
SenderAddrSize2 = sizeof(clientService2);


if( connect( ConnectSocket2, (SOCKADDR *)&clientService2, sizeof(clientService2)) == SOCKET_ERROR)
{
AfxMessageBox( "Failed to connect.\n",MB_OK,0 );
WSACleanup();
return;
}
AfxMessageBox("Connected to server.\n", MB_OK, 0);
m_ctlIsConnected2.SetCheck(TRUE);

return;
}

This is only a test program but one variable I could not cast or duplicate to a different name was "socket( AF_INET, SOCK_STREAM, IPPROTO_TCP)"

any clues out there?
Thanks for reading,
Craig C.

What I have tried:

I have tried to duplicate functions by casting and changing names as not to interfere with each other.
Posted
Updated 1-Dec-16 1:20am

Dear Craig,

Yes one can connect to multiple Server sockets as long as the client socket descriptors are independent of each other.
As far as your code posted above, there doesn't seem to be any issue that would cause your application to crash, however please check your control variables and or other variables used that may lead to memory leak resulting your app to crash.

More info about your app and the point or event of crash can help me have a better insight of your issue to resolve it at earliest. I hope while doing so you may find the cause all by yourself.
 
Share this answer
 
v2
Comments
inlandchris 1-Dec-16 6:03am    
Dear Kunal.tawde,
Thank you for the viewing. So I only need the Client socket to be different and all others is ok to be the same? that helps a lot.
Apparently, on the send part of the program I have a blocking recv and that was the problem of the crash; waiting forever.
kunal.tawde 1-Dec-16 6:35am    
Is there anything I can be of any help to you to resolve your issue. If yes, then I would be requring more details from your side regarding the below.

Apparently, on the send part of the program I have a blocking recv and that was the problem of the crash; waiting forever.

Also, just a suggestion that you may use "select" at your client end as well to make it a non-blocking recv. If you wish I can help you on this part.
The first thing to do is to use your debugger to find out where it crashes, and why. That should help to fix the code. Also, you seem to be duplicating all the code for connecting a socket, so it is more than possible that there is a problem somewhere in there. You should create a class that holds all the information and provides the various methods for managing the communication. If you have MFC then you should just use the CSocket Class[^] that provides these features in the first place.
 
Share this answer
 
Comments
inlandchris 1-Dec-16 8:26am    
Dear Richard M.
Yes, CSocket sounds good and its for MFC, I will try this next. Also, I have just bought 4 books on ACE so I will be trying that last. Then I will see what is best. This test program is just that, a test. I have a huge program in C++ that I need to upgrade from Serial RS422 to IP so these tests will help me decide.
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900