Skip to content

Instantly share code, notes, and snippets.

@yifu
Created September 21, 2012 15:06
Show Gist options
  • Select an option

  • Save yifu/3762042 to your computer and use it in GitHub Desktop.

Select an option

Save yifu/3762042 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define MAXPENDING 5 /* Maximum outstanding connection requests */
void DieWithError(char *errorMessage); /* Error handling function */
void HandleTCPClient(int clntSocket); /* TCP client handling function */
int main(int argc, char *argv[])
{
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned short echoServPort; /* Server port */
unsigned int clntLen; /* Length of client address data structure */
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}
echoServPort = atoi(argv[1]); /* First arg: local port */
/* Create socket for incoming connections */
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");
/* Mark the socket so it will listen for incoming connections */
if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed");
for (;;) /* Run forever */
{
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);
/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr,
&clntLen)) < 0)
DieWithError("accept() failed");
int resize = 1;
int status = setsockopt(clntSock, SOL_SOCKET, SO_RCVBUF, &resize, sizeof(int));
if (status < 0)
perror("setsockopt");
socklen_t size = sizeof(int);
status = getsockopt(clntSock, SOL_SOCKET, SO_RCVBUF, &resize, &size);
std::cout << "resize is [" << resize << "]." << std::endl;
/* clntSock is connected to a client! */
printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
HandleTCPClient(clntSock);
}
/* NOT REACHED */
}
void DieWithError(char *errorMessage)
{
perror(errorMessage);
exit(1);
}
#define RCVBUFSIZE 32 /* Size of receive buffer */
void DieWithError(char *errorMessage); /* Error handling function */
void HandleTCPClient(int clntSocket)
{
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
int recvMsgSize; /* Size of received message */
/* Receive message from client */
int i = 0;
while (true)
{
if ( i % 1000 == 0 )
{
std::cout << "Going to sleep (simulate a slow consumer)..." << std::endl;
sleep(10);
std::cout << "Awake!" << std::endl;
}
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed");
std::cout << ++i << ": recvMsgSize[" << recvMsgSize << "]" << std::endl;
}
close(clntSocket); /* Close client socket */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment