Welcome to the forums!
We don't bind() a client socket. We let the operating system auto-bind() to an available port when we call connect(). This is for the reason you are experiencing. Only one socket can be bound to a port and subsequent attempts to bind() to the same port will fail. Also, we need to closesocket() our sockets when we are finished with them. Finally, you should not call WSACleanup on every button press. WSAStartup() should be called only once at program startup and WSACleanup() should be called only once when the program is finishing.
[edit]
We also need to deal with the fact that input from the network may not be null terminated. Therefore, we have to do it explicitly.
This is typically done something like:
Code:
char buf[100];
int ret = recv(s,buf,sizeof(buf) - 1,0);
if (ret != SOCKET_ERROR)
{
buf[ret] = '\0';
// use buffer safely
}
I'll leave it up to you to add that to your code.
[/edit]
So, delete the red code and add the green:
Code:
void CS3Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
SOCKET s=socket(AF_INET,SOCK_STREAM,0);
sockaddr_in a1, a2;
a1.sin_family=AF_INET;
a1.sin_port=htons(5001);
a1.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(s,(LPSOCKADDR) &a1, sizeof(a1))==SOCKET_ERROR)
{
WSACleanup();
return;
}
a2.sin_family=AF_INET;
a2.sin_port=htons(13);
a2.sin_addr.s_addr=inet_addr("192.168.120.100");
if(connect(s,(LPSOCKADDR) &a2,sizeof(a2))==SOCKET_ERROR)
{
WSACleanup();
closesocket(s);
return;
}
char buf[80];
recv(s,buf,sizeof(buf),0);
closesocket(s);
m_s.Format(_T("%s"), buf);
UpdateData(FALSE);
WSACleanup();
}