• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: SO_NOSIGPIPE option.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SO_NOSIGPIPE option.


  • Subject: Re: SO_NOSIGPIPE option.
  • From: Robert Bell <email@hidden>
  • Date: Wed, 07 Oct 2009 13:53:44 -0700


The server first (client in next email):

#include <iostream>
#include <stdio.h>

//Network includes.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

//Other standard lib stuff.
#include <errno.h>
#include <signal.h>

// Threading.
#include <pthread.h>

#define PORT 9500

static void signalCatch(int signo){
if(signo == SIGPIPE){
std::cout << "SIGPIPE received" << std::endl;
}
}

void *writeThread(void *arg){


FILE *fout = static_cast<FILE *>(arg);


int sizeOfData = 1024;
size_t nObj = 1;
int *outData = new int[sizeOfData];
for(int i = 0; i < sizeOfData; i++){
outData[i] = 26;
}


bool connectionError = false;


int count = 0;
while(true){
outData[0] = count;


if((::fwrite(outData, sizeOfData * sizeof(int), nObj, fout) == 1) && (fflush(fout) == 0)){
std::cout << "outData count: " << count << std::endl;
}
else{
connectionError = true;
std::cout << "server: error: write fail ... terminating write attempts" << std::endl;
break;
}


sleep(1);


count++;
}



return NULL;
}

bool written(int fd, void *ptr, size_t nbytes);

ssize_t written_internal(int fd, const void *vptr, size_t n);

int main(int argc, char **args){


if(signal(SIGPIPE, signalCatch) == SIG_ERR)
std::cerr << "### Error: can't catch SIGPIPE ###" << std::endl;

 


// Kill SIGPIPE
//sigset_t new_set, old_set;
//sigemptyset(&new_set);
//sigaddset(&new_set,SIGPIPE);
//sigprocmask(SIG_BLOCK,&new_set,&old_set);


// Server initilization.
int socket;
socklen_t cliaddrlen;
struct sockaddr_in servaddr, cliaddr;

    

socket = ::socket(AF_INET, SOCK_STREAM, 0);

    

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);


// This is a tcp server, and as such, SO_REUSEADDR needs to be set correctly.
int optVal = 1;
int len = sizeof(optVal);
setsockopt(socket,SOL_SOCKET,SO_REUSEADDR,&optVal,len);


// Try and block sigpipe on the socket.
setsockopt(socket,SOL_SOCKET,SO_NOSIGPIPE,&optVal,len);


bind(socket, (const struct sockaddr *)&servaddr, sizeof(servaddr));
listen(socket, 1024);


for(;;){
cliaddrlen = sizeof(cliaddr);


int connection = accept(socket, (struct sockaddr *)&cliaddr, &cliaddrlen);


FILE *fin = fdopen(connection, "r");
FILE *fout = fdopen(connection, "w");


bool connectionError = false;


int ret;
pthread_t tid;
if(ret = pthread_create(&tid, NULL, writeThread, fout))
std::cerr << "thread failed: " << ret << " -"<< std::endl;
else
std::cout << "thread spawned" << std::endl;


while(true){


char inData[1024];
char *rVal = ::fgets(inData, 1024, fin);
if(rVal != NULL){
std::cout << "inData: " << inData; 
}
else{
connectionError = true;
std::cout << "server: error: read fail ... terminating read attempts" << std::endl;
break;
}
}


while(true);
}


return 0;
}

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: SO_NOSIGPIPE option.
      • From: Robert Bell <email@hidden>
References: 
 >SO_NOSIGPIPE option. (From: Robert Bell <email@hidden>)
 >Re: SO_NOSIGPIPE option. (From: Vincent Lubet <email@hidden>)
 >Re: SO_NOSIGPIPE option. (From: Robert Bell <email@hidden>)
 >Re: SO_NOSIGPIPE option. (From: Shawn Erickson <email@hidden>)

  • Prev by Date: Re: Inappropriate NSError from -[NSURLConnection sendSynchronousRequest:::]
  • Next by Date: Re: SO_NOSIGPIPE option.
  • Previous by thread: Re: SO_NOSIGPIPE option.
  • Next by thread: Re: SO_NOSIGPIPE option.
  • Index(es):
    • Date
    • Thread