?
Solved

help: how to make a proper send and close on a socket - got problems

Posted on 2009-04-30
1
Medium Priority
?
233 Views
Last Modified: 2013-11-17
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.





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;
}

Open in new window

0
Comment
Question by:thordk
1 Comment
 
LVL 40

Accepted Solution

by:
evilrix earned 1500 total points
ID: 24286821
How do you know the connection isn't closed? It could be that the post on the printer is in a TIME-WAIT state.
http://www.comnet.technion.ac.il/netcourse/CIE/RFC/1323/24.htm
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question