Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Winsock problem

Winsock problem

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpquestion
21 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Moak

    If the TCP handshake is successful, you are actually sending packages from the microcontroller to the PC. This lets me wonder if the error you see is on application level, perhaps your socket code is not working properly, could be both client or server side. In the code snippet you provided there is no connect or bind/listen call, so I would not expect the socket to change status. 1) Have you tried connecting to your PC socket application from another PC (or via loopback)? 2) What error code do you get when connecting from microcontroller to PC? Timeout or something else? 3) Have you checked with Wireshark that TCP handshake is fine and packages are properly ACKed? 4) Which TCP stack are you using on the micocontroller (ARM SDK)? Could the problem not be Winsock at all?

    Chat in Europe :java: Now with 24% more Twitter

    modified on Wednesday, July 7, 2010 11:05 AM

    M Offline
    M Offline
    masnu
    wrote on last edited by
    #9

    Sorry Moak, I was cutting and pasting and forgot the most important part. Here's the actual code to open and connect:

    int CHPCtrl::Connect()
    {

    struct sockaddr\_in	rmtAddr;
    

    // Create data socket
    m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if ( m\_Socket == INVALID\_SOCKET )
    	return 1;
    

    // Set client properties
    rmtAddr.sin_family = AF_INET;
    rmtAddr.sin_addr.s_addr = inet_addr( remoteIP );
    rmtAddr.sin_port = htons( m_nDevicePort );

    // Connect to remote
    if ( connect( m_Socket, (struct sockaddr *)&rmtAddr, sizeof(rmtAddr) ) == SOCKET_ERROR )
    return 1;

    // Open rx monitor thread
    m_hRxMonitor = (HANDLE)_beginthreadex( 0, 0, RxMonitor, this, CREATE_SUSPENDED, 0 );

    if ( m\_hRxMonitor )
    {
    	
    	m\_bConnected = true;
    	ResumeThread( m\_hRxMonitor );
    
    } //\_\_if ( m\_pReadThread )\_\_
    
    return 0;
    

    }

    And in a separate thread I wait for incoming data:

    UINT CHPCtrl::RxMonitor(void *pThis)
    {

    CHPCtrl		\*pCtrl = (CHPCtrl\*)pThis;
    int		bytes\_recevied = 0;
    
    fd\_set		sckt;
    timeval		timeout;
    

    // Set two second timeout
    timeout.tv_sec = 2;
    timeout.tv_usec = 0;

    // Monitor all rx traffic
    while ( pCtrl->m_bConnected )
    {

    	FD\_ZERO( &sckt );
    	FD\_SET( pCtrl->m\_Socket, &sckt );
    
    	int ret = select( 0, &sckt, 0, 0, &timeout );
    
    	switch ( ret )
    	{
    
    		case  SOCKET\_ERROR:
    			//Handle error 
    			break;
    
    		case 0:	//Timeout
    			//Handle time out
    			break;
    
    		default:
    			pCtrl->ReadFromSocket( pCtrl->m\_Socket );
    
    	} //\_\_switch ( ret )\_\_
    
    } //\_\_while ( m\_bRunRx )\_\_
    

    // Shutdown rx comm
    shutdown( pCtrl->m_Socket, SD_RECEIVE );

    return 0;
    

    }

    m_Socket is a class variable. I did this so I can send and receive on the same socket without blocking. Please let me know if you see anything wrong with this.

    M 1 Reply Last reply
    0
    • M masnu

      Sorry Moak, I was cutting and pasting and forgot the most important part. Here's the actual code to open and connect:

      int CHPCtrl::Connect()
      {

      struct sockaddr\_in	rmtAddr;
      

      // Create data socket
      m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

      if ( m\_Socket == INVALID\_SOCKET )
      	return 1;
      

      // Set client properties
      rmtAddr.sin_family = AF_INET;
      rmtAddr.sin_addr.s_addr = inet_addr( remoteIP );
      rmtAddr.sin_port = htons( m_nDevicePort );

      // Connect to remote
      if ( connect( m_Socket, (struct sockaddr *)&rmtAddr, sizeof(rmtAddr) ) == SOCKET_ERROR )
      return 1;

      // Open rx monitor thread
      m_hRxMonitor = (HANDLE)_beginthreadex( 0, 0, RxMonitor, this, CREATE_SUSPENDED, 0 );

      if ( m\_hRxMonitor )
      {
      	
      	m\_bConnected = true;
      	ResumeThread( m\_hRxMonitor );
      
      } //\_\_if ( m\_pReadThread )\_\_
      
      return 0;
      

      }

      And in a separate thread I wait for incoming data:

      UINT CHPCtrl::RxMonitor(void *pThis)
      {

      CHPCtrl		\*pCtrl = (CHPCtrl\*)pThis;
      int		bytes\_recevied = 0;
      
      fd\_set		sckt;
      timeval		timeout;
      

      // Set two second timeout
      timeout.tv_sec = 2;
      timeout.tv_usec = 0;

      // Monitor all rx traffic
      while ( pCtrl->m_bConnected )
      {

      	FD\_ZERO( &sckt );
      	FD\_SET( pCtrl->m\_Socket, &sckt );
      
      	int ret = select( 0, &sckt, 0, 0, &timeout );
      
      	switch ( ret )
      	{
      
      		case  SOCKET\_ERROR:
      			//Handle error 
      			break;
      
      		case 0:	//Timeout
      			//Handle time out
      			break;
      
      		default:
      			pCtrl->ReadFromSocket( pCtrl->m\_Socket );
      
      	} //\_\_switch ( ret )\_\_
      
      } //\_\_while ( m\_bRunRx )\_\_
      

      // Shutdown rx comm
      shutdown( pCtrl->m_Socket, SD_RECEIVE );

      return 0;
      

      }

      m_Socket is a class variable. I did this so I can send and receive on the same socket without blocking. Please let me know if you see anything wrong with this.

      M Offline
      M Offline
      Moak
      wrote on last edited by
      #10

      Looks good, but I have never used select() on Windows. Perhaps have a look at the questions I had, they might give you some ideas. :)

      Chat in Europe :java: Now with 24% more Twitter

      1 Reply Last reply
      0
      • M masnu

        Please elaborate as to which part of the post wasn't in "international" English and I will be happy to clarify.

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #11

        masnu wrote:

        Please elaborate as to which part of the post wasn't in "international" English and I will be happy to clarify.

        "r" "u" Mind you, once translated from text messaging, it was a helpful question / answer. Iain,

        I am one of "those foreigners coming over here and stealing our jobs". Yay me!

        1 Reply Last reply
        0
        • M masnu

          Hi, I'm trying to enable my ARM development board to communicate with a PC. I can establish a connection and send data from the PC to the micro without any issues. When I try to send data from the micro to the PC, however, my socket doesn't respond to the data. A network analyzer shows that the packet was sent and formated properly. I have tried both TCP and UDP with the same results. Has anyone come across this before? Thanks.

          M Offline
          M Offline
          masnu
          wrote on last edited by
          #12

          For those of you who are interested I finally figured out what the problem was. When I implemented the TCP/IP stack on the micro I made a mistake in the routine that calculates the IP header checksum. With an invalid checksum Winsock just disregarded the message. Once the correct checksum was sent everything worked fine. Thanks to all of you for your help. I appreciate it. Paul

          M J 2 Replies Last reply
          0
          • M masnu

            For those of you who are interested I finally figured out what the problem was. When I implemented the TCP/IP stack on the micro I made a mistake in the routine that calculates the IP header checksum. With an invalid checksum Winsock just disregarded the message. Once the correct checksum was sent everything worked fine. Thanks to all of you for your help. I appreciate it. Paul

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #13

            So it was not a Winsock problem at all. ;)

            Chat in Europe :java: Now with 24% more Twitter

            M 1 Reply Last reply
            0
            • M Moak

              So it was not a Winsock problem at all. ;)

              Chat in Europe :java: Now with 24% more Twitter

              M Offline
              M Offline
              masnu
              wrote on last edited by
              #14

              Nope! It was a me problem!! :-) Thanks Moak!

              1 Reply Last reply
              0
              • M masnu

                For those of you who are interested I finally figured out what the problem was. When I implemented the TCP/IP stack on the micro I made a mistake in the routine that calculates the IP header checksum. With an invalid checksum Winsock just disregarded the message. Once the correct checksum was sent everything worked fine. Thanks to all of you for your help. I appreciate it. Paul

                J Offline
                J Offline
                jeron1
                wrote on last edited by
                #15

                If you don't mind me asking, which network analyser did you use that didn't display an incorrect checksum?

                M 1 Reply Last reply
                0
                • J jeron1

                  If you don't mind me asking, which network analyser did you use that didn't display an incorrect checksum?

                  M Offline
                  M Offline
                  masnu
                  wrote on last edited by
                  #16

                  I was using Wireshark, but it DID display an incorrect checksum. I just got busy doing other things and completely overlooked it. It wasn't until I focused on the problem that I paid attention to what the analyzer was telling me.

                  J 1 Reply Last reply
                  0
                  • M masnu

                    I was using Wireshark, but it DID display an incorrect checksum. I just got busy doing other things and completely overlooked it. It wasn't until I focused on the problem that I paid attention to what the analyzer was telling me.

                    J Offline
                    J Offline
                    jeron1
                    wrote on last edited by
                    #17

                    Cool, the reason I asked is we have a similar project coming up, and a long time ago I ran into a goofy problem with a fairly early version of Ethereal where it didn't flag some field as having an invalid value, I lost a lot of hair over that one! :)

                    M 1 Reply Last reply
                    0
                    • J jeron1

                      Cool, the reason I asked is we have a similar project coming up, and a long time ago I ran into a goofy problem with a fairly early version of Ethereal where it didn't flag some field as having an invalid value, I lost a lot of hair over that one! :)

                      M Offline
                      M Offline
                      masnu
                      wrote on last edited by
                      #18

                      I can see how that would happen. Thankfully this one didn't take me that much time. Try WireShark http://www.wireshark.org/[^] it's a useful tool.

                      M 1 Reply Last reply
                      0
                      • M masnu

                        I can see how that would happen. Thankfully this one didn't take me that much time. Try WireShark http://www.wireshark.org/[^] it's a useful tool.

                        M Offline
                        M Offline
                        Moak
                        wrote on last edited by
                        #19

                        What wonders me now... that you actually have seen the corrupt IP packages in Wireshark. I was assuming you ran Wireshark on Windows PC and your hosts are connected via a switch, shouldn't the switch throw away the IP packages from the embedded board with wrong header checksum instead of forwarding them?

                        Chat in Europe :java: Now with 24% more Twitter

                        M M 2 Replies Last reply
                        0
                        • M Moak

                          What wonders me now... that you actually have seen the corrupt IP packages in Wireshark. I was assuming you ran Wireshark on Windows PC and your hosts are connected via a switch, shouldn't the switch throw away the IP packages from the embedded board with wrong header checksum instead of forwarding them?

                          Chat in Europe :java: Now with 24% more Twitter

                          M Offline
                          M Offline
                          Moak
                          wrote on last edited by
                          #20

                          Oops I was thinking wrong. IP packages are OSI Layer 3 and network switches operate on Layer 2... so they couldn't care less about IP header checksums. Sorry for the confusion. Happy coding! :)

                          Chat in Europe :java: Now with 24% more Twitter

                          1 Reply Last reply
                          0
                          • M Moak

                            What wonders me now... that you actually have seen the corrupt IP packages in Wireshark. I was assuming you ran Wireshark on Windows PC and your hosts are connected via a switch, shouldn't the switch throw away the IP packages from the embedded board with wrong header checksum instead of forwarding them?

                            Chat in Europe :java: Now with 24% more Twitter

                            M Offline
                            M Offline
                            masnu
                            wrote on last edited by
                            #21

                            No.. I had my embedded board connected to the PC with a cross-over cable.. no switch.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups