sockatmark() — Determine whether a socket is at the out-of-band mark

Standards

Standards / Extensions C or C++ Dependencies

Single UNIX Specification, Version 3

both z/OS® V1R9

Format

SUSV3:
#define _POSIX_C_SOURCE 200112L
#include <sys/socket.h>

int sockatmark(int s);

General description

The sockatmark() function determines whether the socket specified by the descriptor s is at the out-of-band data mark. If the protocol for the socket supports out-of-band data by marking the stream with an out-of-band data mark, the sockatmark() function returns 1 when all data preceding the mark has been read and the out-of-band data mark is the first element in the receive queue. The sockatmark() function does not remove the mark from the stream.

Argument Description
s the descriptor used to determine if the socket is at the out-of-band data mark

Returned value

Upon successful completion, the sockatmark() function returns a value indicating whether the socket is at an out-of-band data mark. If the protocol has marked the data stream and all data preceding the mark has been read, the return value is 1; if there is no mark, or if data precedes the mark in the receive queue, the sockatmark() function returns 0. Otherwise, it returns a value of -1 and set errno to indicate the error.

Error Code
Description
EBADF
The s argument is not a valid file descriptor.
ENOTTY
The s argument does not specify a descriptor for a socket.

Example

CELEBS74

⁄* CELEBS74

   This example demonstrates the use of the sockatmark() function.

   Expected output:
   C: Sending regular data
   C: Sending OOB data
   S: Received "123a"
   S: At the mark
   S: Received "b"

*⁄

#define _POSIX_C_SOURCE 200112L
#include <sys⁄socket.h>
#include <netinet⁄in.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
   struct sockaddr_in saddr;
   socklen_t addr_len = sizeof(saddr);
   int port = 12121, n, ld, connfd, servfd;
   char buffer[25];
   pid_t pid;

   if((ld = socket(AF_INET,SOCK_STREAM,0)) == -1){
      printf("socket error\n");
      return 0;
   }

   saddr.sin_family = AF_INET;
   saddr.sin_port    = 12121;

   if(bind(ld,(struct sockaddr *)&saddr,addr_len) == -1){
      printf("bind error\n");
      return 0;
   }

   if(listen(ld,5) == -1){
      printf("listen error\n");
      return 0;
   }

   pid = fork();
   if(pid==0){
      if((connfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
         printf("socket error\n");
         exit(0);
      }

      if(connect(connfd,(struct sockaddr *)&saddr,addr_len) == -1){
         printf("connect error\n");
         exit(0);
      }

      printf("C: Sending regular data\n");
      send(connfd,"123",3,0);
      printf("C: Sending OOB data\n");
      send(connfd,"ab",2,MSG_OOB);

      close(connfd);
      exit(0);
   }
   else {
      servfd = accept(ld,(struct sockaddr *)&saddr,&addr_len);
      if(servfd == -1) {
         printf("accept error\n");
         exit(0);
      }

      sleep(5);
      memset(buffer,0,sizeof(buffer));
      recv(servfd,&buffer,sizeof(buffer),0);
      printf("S: Received \"%s\"\n",buffer);

      memset(buffer,0,sizeof(buffer));

      n = sockatmark(servfd);
      if(n == 1) printf("S: At the mark\n");
      recv(servfd,&buffer,sizeof(buffer),MSG_OOB);
      printf("S: Received \"%s\"\n",buffer);

      close(servfd);
      close(ld);
   }
}

Related information