I am trying to write a TCP Echo program that should open a TCP server socket, accept at least 1 connection, and echo back any data received. At minimum, this program should launch and run on a specified port (exe: port 4444". Ideally, it would ask for a port number (from command line, command line parameter, or config file), attempt to open on that port, and report if errors occurred.
The program is supposed to be tested using the telnet command from windows or Linux, or any terminal emulator in any OS. The use connects to the running program using Telnet or Hyperterminal. Anything that gets typed in should echo back immediately.
So far I have the following:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <errno.h>
#define MAXCONNECTING 1000 //Max connection requests
#define BUFFERSIZE 128 //limits data sent per loop
#define ECHOPORT 4444
//Error checking function
//Will be called multiple times to check for errors throughout the code
void ERR(char *ERROR) {
perror(ERROR);
exit(1);
}
/****************************************************************************
* Handles the connection. Receive/Send data from/to client
****************************************************************************/
void ClientHandle(int sock) {
char buffer[BUFFERSIZE];
int received = -1;
//receive the data
if ((received = recv(sock, buffer, BUFFERSIZE, 0)) < 0) {
ERR("Failed to receive message from client");
}
//send data and check for more incoming data
while (received > 0) {
if (send(sock, buffer, received, 0) != received) {
ERR("Failed to send data to client");
}
if ((received = recv(sock, buffer, BUFFERSIZE, 0)) < 0) {
ERR("Failed to receive additional data from client");
}
}
close(sock);
}
int main(int argc, char *argv[]) {
/****************************************************************************
* Get port number from command line and set to default port
****************************************************************************/
char *endptr;
short int port;
if (argc == 2) {
port = strtol(argv[1], &endptr, 0);
if (*endptr) {
fprintf(stderr, "EchoServer: Invalid Port Number.\n");
exit(EXIT_FAILURE);
}
} else if (argc < 2) {
port = ECHOPORT; //port 4444
} else {
fprintf(stderr, "EchoServer: Invalid arguments.\n");
exit(EXIT_FAILURE);
}
/****************************************************************************
* Server Configuration. Creating the socket
****************************************************************************/
int serversock, clientsock;
struct sockaddr_in echoserver, echoclient;
if (argc != 2) {
fprintf(stderr, "USING: echoserver Port: 4444\n");
exit(1);
}
//Creating the TCP socket
if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
ERR("Failed to create socket");
}
//Constructing the server sockaddr_in structure
memset(&echoserver, 0, sizeof(echoserver)); //clear struct
echoserver.sin_family = AF_INET; //internet ip
echoserver.sin_addr.s_addr = htonl(INADDR_ANY); //listen to any ip address
echoserver.sin_port = htons(atoi(argv[1])); //server port
/*****************************************************************************
* Bind and Listen
*****************************************************************************/
if (bind(serversock, (struct sockaddr *) &echoserver, sizeof (echoserver)) < 0) {
ERR("Failed to bind the server socket");
}
if (listen(serversock, MAXCONNECTING) < 0) {
ERR("Failed to listen on server socket");
}
/*****************************************************************************
* Accepting the transmission
*****************************************************************************/
while (1) {
unsigned int clientleng = sizeof (echoclient);
if ((clientsock = accept(serversock, (struct sockaddr *) &echoclient, &clientleng)) < 0) {
ERR("Failed to accept connection from client");
}
fprintf(stdout, "Client connected: %s\n", inet_ntoa(echoclient.sin_addr));
ClientHandle(clientsock);
}
return (0);
}
/*****************************************************************************************/
Can anyone see where I have went wrong? I can find the program in terminal using the command: g++ EchoServer.cpp -0 EchoServer and then followed by: ./EchoServer
The ouput only gives me: USING: echoserver Port: 4444
I am trying to telnet to it, but I am very new at this stuff. Please help!!