[Anet-checkins] CVS: ANet/ANet_Daemon/Linux/Samples AsyncTCPServer.c,NONE,1.1 AsyncUDPServer.c,NONE,
Status: Abandoned
Brought to you by:
benad
|
From: Prasanna G. <gpr...@us...> - 2002-01-29 22:21:38
|
Update of /cvsroot/anet/ANet/ANet_Daemon/Linux/Samples
In directory usw-pr-cvs1:/tmp/cvs-serv31581
Added Files:
AsyncTCPServer.c AsyncUDPServer.c SyncTCPServer.c
SyncUDPServer.c TCPClient.c UDPClient.c
Log Message:
""
--- NEW FILE: AsyncTCPServer.c ---
#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() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define MAXPENDING 5 /* Maximum outstanding connection requests */
#define RCVBUFSIZE 11 /* Size of receive buffer */
void DieWithError(char *errorMessage) { /* Error handling function */
perror(errorMessage);
exit(1);
}
int main(int argc, char *argv[]) {
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in servAddr; /* Local address */
struct sockaddr_in clntAddr; /* Client address */
fd_set rset,wset; /* File Descriptor for read and write */
unsigned short servPort; /* Server port */
unsigned int clntLen; /* Length of client address data structure */
struct timeval tv = {0,0}; /* Timeout value for select */
char sendBuffer[RCVBUFSIZE]; /* Buffer for send string */
int recvMsgSize; /* Size of received message */
int noOfPacktoSend; /* No of Packet to send */
char recvBuffer[RCVBUFSIZE]; /* Buffer for receving string */
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}
servPort = 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(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
servAddr.sin_port = htons(servPort); /* Local port */
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
DieWithError("bind() failed");
/* Mark the socket so it will listen for incoming connections */
if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed");
/* Set the size of the in-out parameter */
clntLen = sizeof(clntAddr);
/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &clntAddr,
&clntLen)) < 0)
DieWithError("accept() failed");
/* clntSock is connected to a client! */
/* Receive message from client */
if ((recvMsgSize = recv(clntSock, recvBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed");
noOfPacktoSend = (int) atoi(recvBuffer);
printf("No of Packets to send = %d\n",noOfPacktoSend);
/* Send received string and receive again until end of transmission */
while(1) {
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_SET(clntSock,&rset);
FD_SET(clntSock,&wset);
select(clntSock+1,&rset,&wset,NULL,&tv);
if (FD_ISSET(clntSock,&rset)) {
/* See if there is more data to receive */
if ((recvMsgSize = recv(clntSock, recvBuffer, RCVBUFSIZE,0)) < 0)
DieWithError("recv() failed");
recvBuffer[recvMsgSize] ='\0';
sleep(5);
printf("receiving : %s\n",recvBuffer);
if (!strcmp(recvBuffer,"Closed") || recvMsgSize > 3)
break;
}
if (FD_ISSET(clntSock,&wset)) {
if (noOfPacktoSend >= 0) {
memset(sendBuffer,0,RCVBUFSIZE);
if (!noOfPacktoSend) {
strcpy(sendBuffer,"Disconnect");
} else {
sprintf(sendBuffer,"%10d",rand() % 1000);
}
if (send(clntSock, sendBuffer, strlen(sendBuffer), 0) != strlen(sendBuffer))
DieWithError("send() failed");
noOfPacktoSend--;
printf("Sending : %s\n",sendBuffer);
}
}
}
close(clntSock);
}
--- NEW FILE: AsyncUDPServer.c ---
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define STRMAX 10 /* Longest string to send or receive */
void DieWithError(char *errorMessage) /* External error handling function */
{
perror(errorMessage);
exit(1);
}
int main(int argc, char *argv[])
{
int sock; /* Socket */
struct sockaddr_in servAddr; /* Local address */
struct sockaddr_in clntAddr; /* Client address */
fd_set rset,wset; /* File Descriptor for read and write */
unsigned int cliAddrLen; /* Length of incoming message */
char sendBuffer[STRMAX]; /* Buffer for send and receiving string */
unsigned short servPort; /* Server port */
int recvMsgSize; /* Size of received message */
int noOfPacktoSend; /* No of Pack to be send */
struct timeval tv = {0,0}; /* Timeout value for select */
if (argc != 2) /* Test for correct number of parameters */
{
fprintf(stderr,"Usage: %s <UDP SERVER PORT>\n", argv[0]);
exit(1);
}
servPort = atoi(argv[1]); /* First arg: local port */
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Construct local address structure */
memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
servAddr.sin_port = htons(servPort); /* Local port */
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
DieWithError("bind() failed");
/* Set the size of the in-out parameter */
cliAddrLen = sizeof(clntAddr);
/* Block until receive message from a client */
if ((recvMsgSize = recvfrom(sock, sendBuffer, STRMAX, 0,
(struct sockaddr *) &clntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed");
printf("Handling client %s\n", inet_ntoa(clntAddr.sin_addr));
noOfPacktoSend = (int) atoi(sendBuffer);
printf("No of UDP Packets to send = %d\n",noOfPacktoSend);
while(1) {
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_SET(sock,&rset);
FD_SET(sock,&wset);
select(sock+1,&rset,&wset,NULL,&tv);
if (FD_ISSET(sock,&wset)) {
if (noOfPacktoSend >= 0) {
if (!noOfPacktoSend) {
strcpy(sendBuffer,"Disconnect");
} else {
sprintf(sendBuffer,"%9d",rand() % 1000);
}
/* Send received datagram back to the client */
if (sendto(sock, sendBuffer, strlen(sendBuffer), 0,
(struct sockaddr *) &clntAddr, sizeof(clntAddr)) != strlen(sendBuffer))
DieWithError("sendto() sent a different number of bytes than expected");
printf("Sending : %s\n",sendBuffer);
noOfPacktoSend--;
}
}
if (FD_ISSET(sock,&rset)) {
if ((recvMsgSize = recvfrom(sock, sendBuffer, STRMAX, 0,
(struct sockaddr *) &clntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed");
sendBuffer[recvMsgSize] = '\0';
printf("Receiving : %s\n",sendBuffer);
if (!strcmp(sendBuffer,"Closed"))
break;
}
}
close(sock);
/* NOT REACHED */
}
--- NEW FILE: SyncTCPServer.c ---
#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() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define MAXPENDING 5 /* Maximum outstanding connection requests */
#define RCVBUFSIZE 11 /* Size of receive buffer */
void DieWithError(char *errorMessage) { /* Error handling function */
perror(errorMessage);
exit(1);
}
int HandleTCPClient(int clntSocket) { /* TCP client handling function */
char sendBuffer[RCVBUFSIZE]; /* Buffer for send string */
int recvMsgSize; /* Size of received message */
int noOfPacktoSend; /* No of Packet to send */
char recvBuffer[RCVBUFSIZE]; /* Buffer for receving string */
/* Receive message from client */
if ((recvMsgSize = recv(clntSocket, recvBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed");
noOfPacktoSend = (int) atoi(recvBuffer);
printf("No of Packets to send = %d\n",noOfPacktoSend);
/* Send received string and receive again until end of transmission */
while (1) /* zero indicates end of transmission */
{
memset(sendBuffer,0,RCVBUFSIZE);
if (!noOfPacktoSend) {
strcpy(sendBuffer,"Disconnect");
} else {
sprintf(sendBuffer,"%5d",rand() % 1000 );
}
if (send(clntSocket, sendBuffer, strlen(sendBuffer), 0) != strlen(sendBuffer))
DieWithError("send() failed");
/* See if there is more data to receive */
if ((recvMsgSize = recv(clntSocket, recvBuffer, RCVBUFSIZE,0)) < 0)
DieWithError("recv() failed");
recvBuffer[recvMsgSize] ='\0';
printf("Sending : %s\tReceiving : %s\n",sendBuffer,recvBuffer);
if (!strcmp(recvBuffer,"Closed"))
break;
noOfPacktoSend--;
}
close(clntSocket); /* Close client socket */
return 1;
}
int main(int argc, char *argv[]) {
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in servAddr; /* Local address */
struct sockaddr_in clntAddr; /* Client address */
unsigned short servPort; /* 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);
}
servPort = 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(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
servAddr.sin_port = htons(servPort); /* Local port */
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 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(clntAddr);
/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &clntAddr,
&clntLen)) < 0)
DieWithError("accept() failed");
/* clntSock is connected to a client! */
printf("Handling client %s\n", inet_ntoa(clntAddr.sin_addr));
if (HandleTCPClient(clntSock))
break;
}
/* NOT REACHED */
}
--- NEW FILE: SyncUDPServer.c ---
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define STRMAX 255 /* Longest string to send or receive */
void DieWithError(char *errorMessage) /* External error handling function */
{
perror(errorMessage);
exit(1);
}
int main(int argc, char *argv[])
{
int sock; /* Socket */
struct sockaddr_in servAddr; /* Local address */
struct sockaddr_in clntAddr; /* Client address */
unsigned int cliAddrLen; /* Length of incoming message */
char sendBuffer[STRMAX]; /* Buffer for send and receiving string */
unsigned short servPort; /* Server port */
int recvMsgSize; /* Size of received message */
int noOfPacktoSend; /* No of Pack to be send */
if (argc != 2) /* Test for correct number of parameters */
{
fprintf(stderr,"Usage: %s <UDP SERVER PORT>\n", argv[0]);
exit(1);
}
servPort = atoi(argv[1]); /* First arg: local port */
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Construct local address structure */
memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
servAddr.sin_port = htons(servPort); /* Local port */
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
DieWithError("bind() failed");
for (;;) /* Run forever */
{
/* Set the size of the in-out parameter */
cliAddrLen = sizeof(clntAddr);
/* Block until receive message from a client */
if ((recvMsgSize = recvfrom(sock, sendBuffer, STRMAX, 0,
(struct sockaddr *) &clntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed");
printf("Handling client %s\n", inet_ntoa(clntAddr.sin_addr));
noOfPacktoSend = (int) atoi(sendBuffer);
printf("No of UDP Packets to send = %d\n",noOfPacktoSend);
while(1) {
if (!noOfPacktoSend) {
strcpy(sendBuffer,"Disconnect");
if (sendto(sock, sendBuffer, strlen(sendBuffer), 0,
(struct sockaddr *) &clntAddr, sizeof(clntAddr)) != strlen(sendBuffer))
DieWithError("sendto() sent a different number of bytes than expected");
} else {
sprintf(sendBuffer,"%d",rand());
/* Send received datagram back to the client */
if (sendto(sock, sendBuffer, strlen(sendBuffer), 0,
(struct sockaddr *) &clntAddr, sizeof(clntAddr)) != strlen(sendBuffer))
DieWithError("sendto() sent a different number of bytes than expected");
printf("Sending : %s\t",sendBuffer);
noOfPacktoSend--;
}
if ((recvMsgSize = recvfrom(sock, sendBuffer, STRMAX, 0,
(struct sockaddr *) &clntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed");
sendBuffer[recvMsgSize] = '\0';
printf("Receiving : %s\n",sendBuffer);
if (!strcmp(sendBuffer,"Closed"))
exit(0);
}
}
/* NOT REACHED */
}
--- NEW FILE: TCPClient.c ---
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define RCVBUFSIZE 11 /* Size of receive buffer */
void DieWithError(char *errorMessage) { /* Error handling function */
perror(errorMessage);
exit(1);
}
int main(int argc, char *argv[]) {
int sock; /* Socket descriptor */
struct sockaddr_in servAddr; /* Server address */
unsigned short servPort; /* Server port */
char *servIP; /* Server IP address (dotted quad) */
char *sendString; /* String to send to server */
char recvString[RCVBUFSIZE]; /* Buffer for recv string */
unsigned int sendStringLen; /* Length of send string */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
and total bytes read */
if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server IP> <No Packet to Send> [<Echo Port>]\n",
argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
sendString = argv[2]; /* Second arg: Number of Packet to send and receive */
if (argc == 4)
servPort = atoi(argv[3]); /* Use given port, if any */
else
servPort = 7; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
servAddr.sin_port = htons(servPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
DieWithError("connect() failed");
// strcpy(sendString,"5");
sendStringLen = strlen(sendString); /* Determine input length */
/* Send the string to the server */
if (send(sock, sendString, sendStringLen, 0) != sendStringLen)
DieWithError("send() sent a different number of bytes than expected");
/* Receive the same string back from the server */
totalBytesRcvd = 0;
strcpy(sendString,"Ok");
sendStringLen = strlen(sendString); /* Determine input length */
while (1)
{
memset(recvString,0,RCVBUFSIZE); /* Set the Buffer with 0 */
/* Receive up to the buffer size (minus 1 to leave space for
a null terminator) bytes from the sender */
if ((bytesRcvd = recv(sock, recvString, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
if (!strcmp(recvString,"Disconnect")) {
strcpy(sendString,"Closed");
sendStringLen = strlen(sendString); /* Determine input length */
if (send(sock, sendString, sendStringLen, 0) != sendStringLen)
DieWithError("send() sent a different number of bytes than expected");
printf("Reveiving: %s\tSending: %s\n",recvString,sendString);
break;
}
sleep(5);
if (send(sock, sendString, sendStringLen, 0) != sendStringLen)
DieWithError("send() sent a different number of bytes than expected");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
recvString[bytesRcvd] = '\0'; /* Terminate the string! */
printf("Reveiving: %s\tSending: %s\n",recvString,sendString);
}
close(sock);
exit(0);
}
--- NEW FILE: UDPClient.c ---
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define STRMAX 255 /* Longest string to handled */
void DieWithError(char *errorMessage)
{
perror(errorMessage);
exit(1);
}
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in servAddr; /* Server address */
struct sockaddr_in fromAddr; /* Source address of echo */
unsigned short servPort; /* server port */
unsigned int fromSize; /* In-out of address size for recvfrom() */
char *servIP; /* IP address of server */
char *sendString; /* String to send to server */
char recvBuffer[STRMAX+1]; /* Buffer for receiving string */
int sendStringLen; /* Length of send string*/
int respStringLen; /* Length of received response */
if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */
{
fprintf(stderr,"Usage: %s <Server IP> <Num packet Recv> [<Echo Port>]\n", argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
sendString = argv[2]; /* Second arg: No of Packets to Receive */
if ((sendStringLen = strlen(sendString)) > STRMAX) /* Check input length */
DieWithError("Echo word too long");
if (argc == 4)
servPort = atoi(argv[3]); /* Use given port, if any */
else
servPort = 7; /* 7 is the well-known port for the echo service */
/* Create a datagram/UDP socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet addr family */
servAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
servAddr.sin_port = htons(servPort); /* Server port */
// strcpy(sendString,"5");
sendStringLen = strlen(sendString); /* Determine input length */
/* Send the string to the server */
if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *)
&servAddr, sizeof(servAddr)) != sendStringLen)
DieWithError("sendto() sent a different number of bytes than expected");
strcpy(sendString,"Ok");
sendStringLen = strlen(sendString); /* Determine input length */
while (1) {
memset(recvBuffer,0,STRMAX); /* Set the Buffer with 0 */
/* Recv a response */
fromSize = sizeof(fromAddr);
if ((respStringLen = recvfrom(sock, recvBuffer, STRMAX, 0,
(struct sockaddr *) &fromAddr, &fromSize)) != strlen(recvBuffer)) {
printf("Recived %s\n",recvBuffer);
DieWithError("recvfrom() failed");
}
if (!strcmp(recvBuffer,"Disconnect")) {
strcpy(sendString,"Closed");
sendStringLen = strlen(sendString); /* Determine input length */
if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *)
&servAddr, sizeof(servAddr)) != sendStringLen)
DieWithError("sendto() sent a different number of bytes than expected");
break;
}
if (servAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
{
fprintf(stderr,"Error: received a packet from unknown source.\n");
exit(1);
}
sleep(5);
/* Send the string to the server */
if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *)
&servAddr, sizeof(servAddr)) != sendStringLen)
DieWithError("sendto() sent a different number of bytes than expected");
/* null-terminate the received data */
recvBuffer[respStringLen] = '\0';
printf("Received: %s\tSending : %s\n", recvBuffer,sendString); /* Print the echoed arg */
}
close(sock);
exit(0);
}
|