Winsock2 WSAGetLastError 10061

      Winsock2 WSAGetLastError 10061

      Dear support,

      I have a problem with winsock2, i write a simple server and client application, the server sock runs without problems on the NETDCUA5.
      And the client socket runs on a windows 10 machine. The communication between server and client socktet runs only in debug session (conmanclient3, cmaccpet3).

      But start the server socket not in the debug mode, can`t connect the client. The WSAGetLastError return allways 10061, connection refused.

      What is wrong ?

      thank you

      Daniel
      Hm very strange,
      you are really sure that you use the same port for "bind" the "listen" on server side as the client tries to connect to?
      Server is really waiting in "accept" for the connection from client?
      Any code details?
      F&S Elektronik Systeme GmbH
      As this is an international forum, please try to post in English.
      Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

      Post was edited 1 time, last by “fs-support_ZU” ().

      Hi,

      yes, its very strange :(
      im really sure, both sockets runs on the same port (4444), and the server socket is waiting in "accept" for a client connection.
      here is a code snippet from server:

      Source Code

      1. // service pin
      2. swprintf(LogEntry_Service,L"Service PIN=%d",nPin);
      3. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      4. // initialize winsock
      5. swprintf(LogEntry_Service,L"Initializing winsock...");
      6. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      7. if(WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
      8. swprintf(LogEntry_Service,L"Initializing, failed (err=%d)",WSAGetLastError());
      9. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      10. return false;
      11. }
      12. // create master socket
      13. swprintf(LogEntry_Service,L"Initialized winsock, successfully.");
      14. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      15. if((ma=socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET) {
      16. swprintf(LogEntry_Service,L"Create master socket, failed (err=%d)",WSAGetLastError());
      17. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      18. return false;
      19. }
      20. // prepare sockaddr
      21. srv.sin_family = AF_INET;
      22. srv.sin_addr.s_addr = ADDR_ANY;
      23. srv.sin_port = htons(4444);
      24. // bind master socket
      25. if(bind(ma,(sockaddr*)&srv,sizeof(srv))==SOCKET_ERROR) {
      26. swprintf(LogEntry_Service,L"Bind master socket, failed (err=%d)",WSAGetLastError());
      27. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      28. return false;
      29. }
      30. // listen to incoming connections
      31. swprintf(LogEntry_Service,L"Bind master socket, done");
      32. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      33. listen(ma,SERVICE_CLIENT_MAX);
      34. // start service thread
      35. addrlen = sizeof(sockaddr);
      36. hService = CreateThread(NULL,0,ServiceThreadProc,(LPVOID)nPin,0,NULL);
      37. if(hService == INVALID_HANDLE_VALUE){
      38. swprintf(LogEntry_Service,L"Create service thread, failed\n");
      39. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      40. return false;
      41. }
      42. SetThreadPriority(hService,THREAD_PRIORITY_HIGHEST);
      43. srvrun = true;
      44. servicerun= true;
      45. swprintf(LogEntry_Service,L"Service Thread started",WSAGetLastError());
      46. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());
      47. swprintf(LogEntry_Service,L"Waiting for incoming connections...");
      48. Log_Service->m_LOG_Write(LogEntry_Service,GetLastError());


      and here is the client connection function from .NET 4.5

      Source Code

      1. ​private void bn_connect_Click(object sender, EventArgs e) {
      2. try {
      3. if((RMHostSocket == null)||(!RMHostSocket.Connected)) {
      4. RMHost = new TcpClient();
      5. RMHost.Connect("192.168.0.3",4444);
      6. if (RMHost.Connected) {
      7. RMHostSocket = RMHost.Client;
      8. Receive = new Thread(new ThreadStart(m_ReceiveData));
      9. Receive.Priority = ThreadPriority.Highest;;
      10. Receive.Start();
      11. }
      12. }else {
      13. if(Receive.ThreadState == ThreadState.Running) {
      14. Receive.Abort();
      15. }
      16. RMHost.Close();
      17. RMHostSocket.Close();
      18. lb_connect.Text = "Connected: No";
      19. lb_authorize.Text = "Authorized: No";
      20. bn_connect.Image = JMT_160901_CLTDLG.Properties.Resources.disconnected_26;
      21. tc_options.Visible = false;
      22. }
      23. }catch(Exception ex) {
      24. lb_connect.Text = "Connected: No";
      25. lb_authorize.Text = "Authorized: No";
      26. bn_connect.Image = JMT_160901_CLTDLG.Properties.Resources.disconnected_26;
      27. tc_options.Visible = false;
      28. MessageBox.Show(ex.Message,"Connection Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
      29. }
      30. }


      the firewall is disabled on client side. Its really really strange ..... starts the server socket from the IDE with "conmanlient3 and cmaccept3" runs the communication between server and client socket with no problems.

      greetings

      Daniels
      Hm, no idea.
      But Telnet Server works so i assume the problem it is related to the port 4444, did you try a free port < 1024? E.g. 700.
      F&S Elektronik Systeme GmbH
      As this is an international forum, please try to post in English.
      Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.
      Hi,

      I have found the failure, the problem is the client :D

      that works on client side, but its c++ not .NET :D

      Source Code

      1. // Setup our socket address structure
      2. SOCKADDR_IN SockAddr;
      3. SockAddr.sin_port = htons(4444);
      4. SockAddr.sin_family = AF_INET;
      5. SockAddr.sin_addr.s_addr = inet_addr("192.168.0.3");


      ok, thank you for your help

      greetings

      Daniel