Ethernet Communication Error

Posted by SivaKumar on Stack Overflow See other posts from Stack Overflow or by SivaKumar
Published on 2010-03-24T11:50:09Z Indexed on 2010/03/24 11:53 UTC
Read the original article Hit count: 559

Filed under:

Hi,

I wrote a program to query the status of the Ethernet printer for that i created a TCP Stream Socket and i send the query command to the printer.In case of Error less condition it returns No error status but in error case its getting hang at recv command.Even i used Non blocking now the recv command returns nothing and error set as Resource temporarily unavailable.


code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <signal.h>
#include <termios.h>
#include <poll.h>
#include <netinet/tcp.h>
#include <stdarg.h>       

int main()
{
int ConnectSocket,ConnectSocket1,select_err,err,nRet,nBytesRead;
struct timeval waitTime = {10,30};
fd_set socket_set;
unsigned char * dataBuf = NULL;
unsigned char tempVar, tempVar1, tempVar2, tempVar3;
char reset[] = "\033E 2\r";
char print[] = "\033A 1\r";
char buf[1024]={0};

        ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 printf("The Socket ID is %d\n",ConnectSocket);

 if (ConnectSocket < 0) 
 {
        perror("socket()");
 return 0;
 }

 struct sockaddr_in clientService; 
 clientService.sin_family = AF_INET;
 clientService.sin_addr.s_addr = inet_addr("192.168.0.129"); //Printer IP
 clientService.sin_port = htons( 9100); // Printer Port

 if ( connect( ConnectSocket, (struct sockaddr*) &clientService, sizeof(clientService) ) == -1) {
   perror("connect()");
   close(ConnectSocket);
          return -1;
 }


/* if((nRet = ioctl(ConnectSocket , FIONREAD, &nBytesRead) == -1))
 {
  perror("ioctl()");
 }
 perror("ioctl()");
*/ 
 FD_ZERO(&socket_set);
 FD_SET(ConnectSocket, &socket_set);

        do
 {
 errno=0;
 select_err = select(ConnectSocket+1, NULL, &socket_set, NULL, &waitTime);
 }while(errno==EINPROGRESS);

 if (-1 == select_err || 0 == select_err)
 {
  int optVal = 0;
  int optLen = sizeof(optVal);

  if(select_err == -1)
  {
   perror("select() write-side");
  }
  else
  {
   //Timeout
   errno=0;
   err = getsockopt(ConnectSocket, 
     SOL_SOCKET, 
     SO_ERROR, 
     (char*)&optVal, 
     &optLen);
   printf("the return of the getsockopt is %d\n",err);
   printf("the opt val is %s\n",(char*)optVal);
   perror("getsockopt()");
   if(err == -1)
   {
    perror("getsockopt() write-side");
   }

  }
  printf("Select Failed during write - ConnectSocket: %d\n", ConnectSocket);
  //close(ConnectSocket);
  return -1;
 }


 err = send(ConnectSocket,print,sizeof(print)-1, 0);
 printf("\n No of Bytes Send is %d\n",err);
 if(err == -1 || err ==0)
 {
 perror("send()");
 //close(ConnectSocket);
 return -1;
 }


 FD_ZERO(&socket_set);
 FD_SET(ConnectSocket, &socket_set);

        do
 {
 errno=0;
 select_err = select(ConnectSocket+1, NULL, &socket_set, NULL, &waitTime);
 }while(errno==EINPROGRESS);

 if (-1 == select_err || 0 == select_err)
 {
  printf("Select Failed during write - ConnectSocket: %d\n", ConnectSocket);
  return -1;
 }


 err = send(ConnectSocket,reset,sizeof(reset)-1, 0);
 printf("\n No of Bytes Send is %d\n",err);
 if(err == -1 || err ==0)
 {
 perror("send()");
 //close(ConnectSocket);
 return -1;
 }


 FD_ZERO(&socket_set);
 FD_SET(ConnectSocket, &socket_set);
 printf("i am in reading \n");
 select_err = select(ConnectSocket+1, &socket_set, NULL, NULL, &waitTime);

 printf("the retun of the read side select is %d \n",select_err);
 perror("select()");

 if (-1 == select_err|| 0 == select_err)
 {  
  printf("Read timeout;  ConnectSocket: %d\n", ConnectSocket);
  close(ConnectSocket);
  perror("close()");
  return -1;
 }

 printf("Before Recv\n");
 nBytesRead = recv(ConnectSocket , buf, 1024, 0);
 printf("No of Bytes read is %d\n",nBytesRead);
 printf("%s\n",buf);
 if(nBytesRead == -1)
 {
  perror("recv()");
  close(ConnectSocket);
  perror("clode()");
  return -1;
 }

close(ConnectSocket);
return 1;
} 

© Stack Overflow or respective owner

Related posts about sockets