Hi all, I have a problem with winsock in c++ while developing a client program:

- When I send the first, it is ok, appears in the Server, but the other "sends", in the server appear as a empty string!
the send command return is Ok, the number of bytes. And the SErver does not close the socket, so..

It is a strange think because if a use VBasic it works: all I send appears in the Server.
Why VB is ok an VC++ does not?
Is there an alternative to winsock?
Have somebody any idea?

Thanks and best regards

That is the connect function:

int conectar()
{
	   WSADATA wsa;
	   static SOCKET buzonete;   	  
	   struct sockaddr_in direc;
	   int conex;    	
	   WSAStartup(MAKEWORD(2,2),&wsa);	   
	   do
 	    {  buzonete = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 
		   printf("Intentando crear el Socket...\n");
	    }
	   while (buzonete==-1);	   
	   direc.sin_family=AF_INET;
	   direc.sin_port=htons(6001);//Var_PUERTO_ACS); 
	   direc.sin_addr.s_addr = inet_addr("127.0.0.1"); 
	   memset(direc.sin_zero,0,8);   
	   do
	    {   conex = connect(buzonete, (sockaddr *)&direc, sizeof(sockaddr));
	        Sleep(1000);   
	    }
	   while (conex==-1);
	    return buzonete;
}

In main I have

int cliente_m;
cliente_m=conectar();
char *p, sip[255]="the comand I will send goes here";
int err;
p = calc(sip);  //  checksum calc
p = p + strlen(p) - 4;     //point to the  last four
sprintf( sip, %s%s%s,sip, p, "\r" ); // build the command. SIP protocol requires a return character at the end.
err = send( cliente_m, sip, strlen(sip)+1, 0 );   // OK
err = send( cliente_m, sip, strlen(sip)+1, 0 );   // Send return Ok, but Server receive nothing

The second and other appears in Server as string with nothing. And err return is OK.
Very strange because I have used a freeware "IP TCP Test Server" program, and the client works fine: all sends appear.

But VB, previous version, worked fine too, this is the code of the send button:

Private Sub btnEchar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEchar.Click
        Dim sip As String
        sip = "the comand goes here"
        cliente.SendData(sip & Strings.Right(calc(sip), 4) & vbCr)

:confused:

commented: Code-tags on first try. Excellent! +12

> p = calc(sip); // checksum calc
So does calc return a pointer to some super-secret memory location?
If it's just a regular char array, then it as gone out of scope, and p is pointing at invalid memory.

> sprintf( sip, %s%s%s,sip, p, "\r" );
sip is BOTH source and destination in this sprintf call.
This is very bad code. There are very few C library functions which are safe when presented with overlapping memory.

Why are you basically writing C in C++ ?

Thank you for reply:
well the function calc works fine, you can see the code below. The command SIP (or any text because I have desavtivated the error detection) is built correctly (step by step I was looking the variables). But it is no important, I have done directly char sip[]="abcdefghi \r"; and try to send twice, but nothing..second fails.

char *calcula(char *message);
{
	static char ascii_checksum[5];
	unsigned int checksum = 0;
	int i = 0;
	printf("mensaje:%s\n",message);
	while (message[i] != '\0')
	{
	   checksum = checksum + (unsigned) message[i]; // Sumar cada caracter ascii
	   i++;
	}
       checksum = ~checksum+1;    
	sprintf (ascii_checksum, "%4.4X", checksum);
	return (ascii_checksum);
}

I have modified sprintf too:

sprintf( sipf, "%s%s%s",sip, p, "\r" ); // build the command

but still the same problem..
I have tried to use \r, \r\n, \n\r, at the end of the string with no success.
Any ideas?
Very strange...

I think it is correct now:
the server i'm using works correct with

err = send( cliente_m, sip, strlen(sip), 0 );

but not this

err = send( cliente_m, sip, strlen(sip) +1 , 0 );

I do not remenber why do I put this +1 there...
;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.