thordk
asked on
help: how to make a proper send and close on a socket - got problems
Hello,
Im having a few problems towards a printer where it seems that a socket connection is not closed ? this is my code, can you see if this is correct ? that closure are done accordingly ?
What i need is a stub that can:
1. connect
2. send data
3. close socket (and make sure its closed + data is sent)
Its important it makes sure its closed because its a printer works like a printqueue, it will print when the socket closes, and then it will continue through next active connection if any.
But something is not quite right always, so wondering about this code, really hope you can help. running xp & 2003.
Im having a few problems towards a printer where it seems that a socket connection is not closed ? this is my code, can you see if this is correct ? that closure are done accordingly ?
What i need is a stub that can:
1. connect
2. send data
3. close socket (and make sure its closed + data is sent)
Its important it makes sure its closed because its a printer works like a printqueue, it will print when the socket closes, and then it will continue through next active connection if any.
But something is not quite right always, so wondering about this code, really hope you can help. running xp & 2003.
int sock_send (bool fastconnect, char *ip, int port, char *databuffer1, int datalen1, char *databuffer2, int datalen2)
{
char *p;
int bytessent=0;
int bufindex=0;
int actualsent=0;
SOCKET ConnectSocket;
struct sockaddr_in clientService;
int recvbuflen = 4096;
char recvbuf[4096] = "";
int iResult = 0;
char szTemp[2000];
char szPipe[2000];
szPipe[0]=0x00;
if (fastconnect)
{
iResult = fast_connect (ip, port, 1000, &ConnectSocket, &clientService);
}
else
{
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( ip );
clientService.sin_port = htons( port );
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
}
if (iResult == 0)
{
bytessent = 0;
while (bytessent < datalen1)
{
actualsent = send (ConnectSocket,&databuffer1[bufindex],(datalen1-bytessent),0);
bytessent += actualsent;
if (actualsent <= 0)
break;
if (bytessent<datalen1)
{
bufindex+=actualsent;
}
}
Sleep(10);
bytessent = 0;
while (bytessent < datalen2)
{
actualsent = send (ConnectSocket,&databuffer2[bufindex],(datalen2-bytessent),0);
bytessent += actualsent;
if (actualsent <= 0)
break;
if (bytessent<datalen2)
{
bufindex+=actualsent;
}
}
iResult = shutdown(ConnectSocket, SD_SEND);
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
closesocket(ConnectSocket);
}
else
{
closesocket(ConnectSocket);
}
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.