/***************************************************************************** * receive.c - partner to "send.c". Opens a port and reports the port number * to standard error. A port number can be specified which overrides the * automatic selection. Any data arriving on this port is then copied to * standard out and the process exits. * * Phil Gibbs - Trinem Consulting (pgibbs@trinem.co.uk) *****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include int EstablishIncomingConnection(int *base_port) { int sock; struct sockaddr_in server; int length; sock = socket(AF_INET, SOCK_STREAM, 0); assert(sock>0); server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(*base_port); assert(!bind(sock, (struct sockaddr *)&server, sizeof(server))); length = sizeof(server); assert(!getsockname(sock, (struct sockaddr *)&server, &length)); listen(sock, 5); *base_port=htons(server.sin_port); return sock; } void CopySocketToStandardOut(int msgsock) { char Buffer[2048]; int BytesRead; int res; while (BytesRead=read(msgsock,Buffer,sizeof(Buffer))) { res=write(1,Buffer,BytesRead); if (res1)?atol(argv[1]):0; Socket=EstablishIncomingConnection(&Port); fprintf(stderr,"Listening on port %d\n",Port); WaitForConnectionRequest(Socket); }