Академический Документы
Профессиональный Документы
Культура Документы
#include
#include
#include
#include
#include
<stdio.h>
<sys/socket.h>
<arpa/inet.h>
<stdlib.h>
<string.h>
<unistd.h>
#define MAXPENDING 5
#define RCVBUFSIZE 32
/*
/*
/*
/*
/*
/*
for
for
for
for
for
for
/*
/*
/*
/*
/*
/*
{
printf("bind() failed\n");
exit(1);
}
/* Mark the socket so it will listen for incoming connections */
if (listen(servSock, MAXPENDING) < 0)
printf("listen() failed\n");
/* 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)
printf("accept() failed\n");
/* clntSock is connected to a client! */
printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
/* Receive message from client */
if ((recvMsgSize = recv(clntSock, echoBuffer, RCVBUFSIZE, 0)) < 0)
printf("recv() failed\n");
else {
echoBuffer[recvMsgSize] = '\0'; /* Terminate the string! */
printf("recvMsg %s\n",echoBuffer);
}
close(clntSock);
}