Demonstrates how to use MSG_OOB with send/recv. Use with BSD MSG_OOB usage server.
/*
* ANSI
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* BSD specific
*/
#include <netinet/in.h> // sockaddr_in,
#include <unistd.h> // close
#include <sys/socket.h> // most bsd calls
#include <arpa/inet.h> // inet_addr
#define INVALID_SOCKET (-1)
/*
* main
*/
void main(int argc, char **argv)
{
char *buff;
struct sockaddr_in serverAddr;
int serverSocket = INVALID_SOCKET;
buff = NULL;
if( argc != 3){
printf("Usage: %s IPaddr port", argv[0]);
goto END_ERR;
}
serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(atoi(argv[2]));
/*
* open a socket
*/
printf("\r\nCreating the Socket\r\n");
if( ( serverSocket = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){
perror("socket()");
goto END_ERR;
}
/*
* connect the socket
*/
printf("\r\nConnecting the Socket\r\n");
if( ( connect(serverSocket,
(struct sockaddr *)&serverAddr, sizeof(serverAddr))) == -1){
perror("connect()");
goto END_ERR;
}
printf("\r\nAllocating buffer\r\n");
if( (buff = (char *)malloc(100000)) == NULL){
printf("problem with malloc");
goto END_ERR;
}
memset(buff, '\0', sizeof(buff));
memset(buff, "abcdefghijklmnopqrstuvwxyz", (sizeof(buff) - 1));
printf("\r\nSending Buffer %s\r\n", buff);
if( (send(serverSocket, buff, strlen(buff), MSG_OOB)) == (-1)){
perror("write()");
goto END_ERR;
}
printf("\r\nRecieving Buffer\r\n");
if( (recv(serverSocket, buff, sizeof(buff)-1, MSG_OOB)) == (-1)){
perror("read()");
goto END_ERR;
}
printf(buff);
printf("\r\n\r\nworks so far\r\n");
END_ERR:
if (serverSocket != INVALID_SOCKET)
close(serverSocket);
if( buff != NULL)
free(buff);
}
© 2008 Novell, Inc. All Rights Reserved.