• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Check Socket Connection State

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a client applicattion sockets, but i want to check the state of a connection. i don't know how i can do that. I need this control over the connection because when the server closed the connection i can do a send() without errors but the second send() return a 'fail'. i want to control this situation. Sorry but my example was developed in C. Please help me!!!
(in *** CHECK CONNECTION *** please insert the code that you recommend)
The Code used is:
Example: A connection-oriented client
The following example shows how to connect a socket client program to a connection-oriented server.
/***********************************************/
/* Header files needed to use the sockets API. */
/***********************************************/
/* in qcle/h file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* in qsysinc library */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
/* BufferLength is 250bytes */
#define BufferLength 250
/* Host name of server system */
#define SERVER "SERVERHOSTNAME"
/* Server�s port number */
#define SERVPORT 3005
/* Pass in 1 parameter which is either the */
/* address or host name of the server, or */
/* set the server name in the #define */
/* SERVER. */
void main(int argc, char *argv[])
{
/****************************************/
/* Variable and structure definitions. */
/****************************************/
int sd, rc, length = sizeof(int);
struct sockaddr_in serveraddr;
char buffer[BufferLength];
char server[255];
char temp;
int totalcnt = 0;
struct hostent *hostp;
/* will send 250bytes of �a� */
memset(buffer, �a�, 250);
/******************************************/
/* The socket() function returns a socket */
/* descriptor representing an endpoint. */
/* The statement also identifies that the */
/* INET (Internet Protocol) address family */
/* with the TCP transport (SOCK_STREAM) */
/* will be used for this socket. */
/******************************************/
/* get a socket descriptor */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket() failed");
exit(-1);
}
if (argc > 1)
strcpy(server, argv[1]);
else
strcpy(server, SERVER);
memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
if ((serveraddr.sin_addr.s_addr = inet_addr(server))
== (unsigned long)INADDR_NONE)
{
/*************************************************/
/* When passing the host name of the server as a */
/* parameter to this program, use the gethostbyname() */
/* function to retrieve the address of the host server. */
/***************************************************/
/* get host address */
hostp = gethostbyname(server);
if (hostp == (struct hostent *)NULL)
{
printf("HOST NOT FOUND --> ");
/* h_errno is usually defined */
/* in netdb.h */
printf("h_errno = %d\n",h_errno);
close(sd);
exit(-1);
}
memcpy(&serveraddr.sin_addr,hostp->h_addr,sizeof(serveraddr.sin_addr));
}
/***********************************************/
/* After the socket descriptor is received, the */
/* connect() function is used to establish a */
/* connection to the server. */
/***********************************************/
/* Connect() to server. */
rc = connect(sd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
if (rc< 0)
{
perror("connect() failed");
close(sd);
exit(-1);
}
/*********************************************/
/* Send 250bytes of a�s to the server using */
/* the write() function. */
/*********************************************/
/* Write() 250�a�s to the server. */
*** CHECK CONNECTION ***
rc = write(sd, buffer, sizeof(buffer));
if (rc < 0)
{
perror("write() failed");
rc = getsockopt(sd, SOL_SOCKET, SO_ERROR,
&temp, &length);
if (rc == 0)
{
/* Print out the asynchronously */
/* received error. */
errno = temp;
perror("SO_ERROR was");
}
close(sd);
exit(-1);
}
totalcnt = 0;
while(totalcnt < BufferLength)
{
/***************************************/
/* Wait for the server to echo the 250*/
/* bytes of a�s back by using the */
/* read() function. */
/***************************************/
/* Read data from the server. */
*** CHECK CONNECTION ***
rc = read(sd, &buffer[totalcnt],
BufferLength-totalcnt);
if (rc < 0)
{
perror("read() failed");
close(sd);
exit(-1);
}
else if (rc == 0)
{
printf("partner program has issued a close()");
close(sd);
exit(-1);
}
else
totalcnt += rc;
}
/****************************************/
/* When the data has been read, close() */
/* the socket descriptor. */
/****************************************/
/* Close socket descriptor from client side. */
close(sd);
exit(0);
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic