Code:
#include<iostream>
#include<cstdlib>
class morse
{
public:
char lookup[28] = { 'a' ,'b' ,'c' ,'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'.' ,',' };
char morse[28][6] = {{ '.','-'}, //a The code tags seem to
{ '-','.','.','.'}, //b be screwing with my
{ '-','.','-','.'}, //c indentation....sorry
{ '-','.','.'}, //d guys!
{ '.'}, //e
{ '.','.','-','.'}, //f
{ '-','-','.'}, //g
{ '.','.','.','.'}, //h
{ '.','.'}, //i
{ '.','-','-','-'}, //j
{ '-','.','-'}, //k
{ '.','-','.','.'}, //l
{ '-','-'}, //m
{ '-','.'}, //n
{ '-','-','-'}, //o
{ '.','-','-','.'}, //p
{ '-','-','.','-'}, //q
{ '.','-','.'}, //r
{ '.','.','.'}, //s
{ '-'}, //t
{ '.','.','-'}, //u
{ '.','.','.','-'}, //v
{ '.','-','-'}, //w
{ '-','.','.','-'}, //x
{ '-','.','-','-'}, //y
{ '-','-','.','.'}, //z
{ '.','-','.','-','.','-'}, //.
{ '-','-','.','.','-','-'} //,
};
void ctob( char input[] )
{//open func
int input_len = strlen(input);
if ( input[0] == '\0' )
{
cout<<"No input given, exiting...";
exit(1);
}
for ( int i = 0; i < input_len; i++ )
{
for ( int j = 0; j < 28; j++ )
{
if ( input[i] == lookup[j] )
{
cout<< morse[j] <<"\n";
int k = 0;
while ( morse[j] [k] != '\0' )
{
if ( morse [j] [k] == '.' )
{
system("beep -f 700 -l 100");
system("sleep 0.17 s");
}
else if ( morse [j] [k] == '-' )
{
system("beep -f 700 -l 100");
system("sleep 0.17 s");
}
k++;
}
system("sleep 0.17 s");
break;
}
}
}
}//close func
void mtob(char input[])
{//open func
int l = 0;
while ( input[l] != '\0' )
{
if ( input[l] == '.' )
{
system("beep -f 700 -l 100");
system("sleep 0.17 s");
}
else if ( input[l] == '-' )
{
system("beep -f 700 -l 100");
system("sleep 0.17 s");
}
l++;
cout<< input[i];
}
system("sleep 0.17 s");
}//close func
char ctom(char input[])
{//open func
int ctom_len = strlen(input);
for ( int i = 0; i < ctom_len; i++ )
{
for ( int j = 0; j < 28; j++ )
{
if ( input[i] == lookup[j] )
{
char morse_code[] = morse[j];
}
}
}
}//close func
};//close class
I have included those header files as the compiler complained otherwise.
Code:
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<"morse.hpp">
#include<netinet/in.h>
#include<arpa/inet.h>
#define CLIENT_QUEUE_SIZE 2
#define MAX_DATA 1024
using namespace std;
int main(int argc, char *argv[])
{
if ( argc == 3 )
{
cout<<"USAGE: ./morse_list [PORT NUMBER]\n";
exit(-1);
}
int sock, cli_sock;
char *greeting = "Connected to the server.\n";
char input[MAX_DATA];
int len = strlen(greeting);
int recv_len;
struct sockaddr_in serv;
struct sockaddr_in client;
morse m;
serv.sin_family = AF_INET;
serv.sin_port = htons(argv[1]);
serv.sin_addr.s_addr = INADDR_ANY;
memset(serv.sin_zero, '\0', sizeof(serv.sin_zero));
if ((sock = socket( PF_INET, SOCK_STREAM, 0 )) == -1 )
{
cout<<"Could not open socket.\n";
exit(-1);
}
if ((bind( sock, ( struct sockaddr *)&serv, sizeof(serv))) == -1 )
{
cout<<"Could not bind socket.\n";
exit(-1);
}
if ((listen( sock, CLIENT_QUEUE_SIZE )) == -1 )
{
cout<<"Could not listenon the network.\n";
exit(-1);
}
cli_sock = accept( sock, (struct sockaddr *)&client, sizeof(client));
if ( cli_sock == -1 )
{
cout<<"Could not accept connection.\n";
exit(-1);
}
cout<<"Connection on port "<<ntohs(client.sin_port)<<"from "<<inet_ntoa(client.sin_addr.s_addr)<<"\n";
int bytes_sent = send( cli_sock, greeting, len, 0 );
if ( bytes_sent == -1 )
{
cout<<"FAIL: Could not send greeting\n";
}
while(1)
{
memset(input, '\0', MAX_DATA);
if ( (recv( cli_sock, input, MAX_DATA, 0 )) == -1 )
{
cout<<"Failed to receive data.\n";
exit(-1);
}
recv_len=strlen(input);
if ( input[0] == '\0' )
{
cout<<"Empty packet received, waiting for next useful packet...";
}
else
{
m.mtob(input[]);
}
}
close(cli_sock);
cout<<"Client connection terminated.\n";
close(sock);
}
Thanks in advance,