This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

More questions about TCP communication

As my previous post shows I can send ACK from the CC3000 to the AP, now I am trying to get the Server part up and running but the system refuses to Bind! I am sure it is some small stupid mistake but I would be grateful if someone could point it out for me then.

void server_communication(void)
{
	// The family
	addrServerTCP.sa_family = AF_INET;

	// The Server port: 51000
	addrServerTCP.sa_data[0] = Port[0];
	addrServerTCP.sa_data[1] = Port[1];

	// The Server IP address: 192.168.0.100
	addrServerTCP.sa_data[2] = IP_Server[0];
	addrServerTCP.sa_data[3] = IP_Server[1];
	addrServerTCP.sa_data[4] = IP_Server[2];
	addrServerTCP.sa_data[5] = IP_Server[3];

	// SET SOCKET PARAMETERS
	SOCKETServerTCP = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	memset (&addrServerTCP.sa_data[2], 0, 4);	//Why should the mem be zeroed?
	// BIND
	BIND_TCP = bind(SOCKETServerTCP, &addrServerTCP, sizeof(sockaddr));
	while(BIND_TCP < 0)
		BIND_TCP = bind(SOCKETServerTCP, &addrServerTCP, sizeof(sockaddr));

	// LISTEN FOR CONNECTIONS ON THE SERVER
	LISTEN_TCP = listen(SOCKETServerTCP, 1);

	// SET SOCKET OPTIONS
	short  nonBlocking = 0;
	unsigned long  recvtimeout = 1000;
	setsockopt(SOCKETServerTCP, SOL_SOCKET, SOCKOPT_NONBLOCK     , &nonBlocking, sizeof(nonBlocking));
	setsockopt(SOCKETServerTCP, SOL_SOCKET, SOCKOPT_RECV_TIMEOUT , &recvtimeout, sizeof(recvtimeout));

	socklen_t size;
	size = sizeof(sockaddr);
	// ACCEPT CONNECTION WITH THE CLIENT
	SOCKETClientTCP = accept( SOCKETServerTCP, &addrServerTCP, &size );
	UARTprintf("SOCKETClientTCP: %i \n", SOCKETClientTCP);
	if(SOCKETClientTCP == 0)
						UARTprintf("Connection Set\n");
	while(SOCKETClientTCP < 0)
	{
		if(SOCKETClientTCP == -2)
			UARTprintf("Connection pending... \n");
		SOCKETClientTCP = accept( SOCKETServerTCP, &addrServerTCP, &size );
		if(SOCKETClientTCP == 0)
					UARTprintf("Connection Set\n");
	}

	fd_set readfd;
	struct timeval timeout;
	FD_ZERO( &readfd );
	FD_SET( SOCKETClientTCP, &readfd);
	timeout.tv_sec = 5;
	timeout.tv_usec = 0;
	// DETERMEN READINESS FOR R/W OPERATIONS ON THE SOCKET
	SELECT_TCP = select(SOCKETClientTCP, &readfd, NULL, NULL, &timeout );

	// RECEIVE CONNECTION VERYFICATION FROM CLIENT
	RECV_TCP = recv(SOCKETClientTCP, ucRx_Buffer, sizeof(ucRx_Buffer), 0);

   	// SEND DATA TO CLIENT
   	SEND_TCP = send(SOCKETClientTCP, (const char*)pcData16, ulDataLength, 0);
    closesocket(SOCKETServerTCP);
    closesocket(SOCKETClientTCP);

    }

  • Hi Martin,

    1) Can you just check if the socket() was created successfully? And assign the 'addrServerTCP' (port, IP etc) after the socket creation is successful.

    2) I'm not sure about the arguments passed. Just assign port 51000 explicitly as below: 

    // The Server port: 51000
    addrServerTCP.sa_data[0] = 0xc7;
    addrServerTCP.sa_data[1] = 0x38;

    3) Address should be zeroed, as only the remote device needs to know the IP address. And your device will acquire the IP address soon afer Wifi Connection.

    4) Just to complete, typecast the 2nd argument to bind with (sockaddr const*).

    Thanks & Regards,

    Raghavendra

  • Thank you Raghavendra for your time!

    1. Yes I receive 0 from Socket() and the assignment is done after the socket now also.

    2. Done.

    3. Okey thanks for the explanation.

    4. Do you mean like this: BIND_TCP = bind(SOCKETServerTCP, (sockaddr const*)&addrServerTCP, sizeof(sockaddr));

    No change, I am afraid. I am still stuck in the while(BIND_TCP < 0).

    Any other ideas? Is there any complete example code in this forum that I could take a look at that is not from the example code supplied by TI? I have been looking thought several hundred of posts but you might know a good post that I should take a look at!

  • Maybe troubleshoot the logistics of the while loop with the bind function and see if you can't get it to work?

    Are you getting Compiler/Linker errors or just that it's not working?

  • I still haven´t made any head way with the Bind function.

    I got no Complier/Linker errors, the Bind function seams to work since it activates the following functions in the correct order and is run several times over and over beacuse of the while function.

    Bind() then SimpleLinkWaitEvent() and then hci_event_handler().

    The socket funtion gives me 0 as it should and the Bind function still gives me -1.

    Wireshark shows this information when I activate the matlab script that starts the connect on the client part:

    192.168.0.101 192.168.0.100 TCP 62 [TCP Port Numbers reused] 51000 > 51000 [SYN] seq=0 win=8192 Len=0 MSS=1460 WS=4 SACK_PERM=1

    192.168.0.101 192.168.0.100 TCP 62 [TCP Retransmission] 51000 > 51000 [SYN] seq=0 win=8192 Len=0 MSS=1460 WS=4 SACK_PERM=1

    Am I missing something here? Below I have write the code as it looks now, what could I be missing....

    	while ((ulCC3000DHCP == 0) || (ulCC3000Connected == 0))
    	{
    		hci_unsolicited_event_handler();
    		SysCtlDelay(1000);
    	}
    
    	SOCKETServerTCP = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	UARTprintf("SOCKETServerTCP: %i \n", SOCKETServerTCP);
    
    	// The family
    	addrServerTCP.sa_family = AF_INET;
    
    	// The Server port: 51000
    	addrServerTCP.sa_data[0] = 0xc7;
    	addrServerTCP.sa_data[1] = 0x38;
    
    	// The Server IP address: 192.168.0.100
    	addrServerTCP.sa_data[2] = 0xC0;
    	addrServerTCP.sa_data[3] = 0xA8;
    	addrServerTCP.sa_data[4] = 0x0;
    	addrServerTCP.sa_data[5] = 0x64;
    
    	// ZERO ALL IP ADDRESSES
    	memset(&addrServerUDP.sa_data[2], 0, 4);
    
    	// BIND
    	BIND_TCP = bind(SOCKETServerTCP, (sockaddr const*)&addrServerTCP, sizeof(addrServerTCP));
    	while(BIND_TCP < 0)
    	{
    		SysCtlDelay(1000000);
    		UARTprintf("BIND_TCP: %i \n", BIND_TCP);
    		BIND_TCP = bind(SOCKETServerTCP, (sockaddr const*)&addrServerTCP, sizeof(addrServerTCP));
    	}
    	UARTprintf("BIND_TCP: %i \n", BIND_TCP);
    
    	// LISTEN FOR CONNECTIONS ON THE SERVER
    	LISTEN_TCP = listen(SOCKETServerTCP, 1);
    	UARTprintf("LISTEN_TCP: %i \n", LISTEN_TCP);
  • Remove this (useless when following memset is correct)

    // The Server IP address: 192.168.0.100
    addrServerTCP.sa_data[2] = 0xC0;
    addrServerTCP.sa_data[3] = 0xA8;
    addrServerTCP.sa_data[4] = 0x0;
    addrServerTCP.sa_data[5] = 0x64;
    Change this
     
    // ZERO ALL IP ADDRESSES
    memset(&addrServerUDP.sa_data[2], 0, 4);
    to this
    // ZERO ALL IP ADDRESSES
    memset(&addrServerTCP.sa_data[2], 0, 4);
  • GOD DAME IT!!!! Thank you Martin I will change that right away and see it that stupid mistake is the cause! Thanks once again!

  • Thank you so much!