Alright , Please accept my sincere appologies, here is the complete
code of my server side, I didn't want to post the whole code thinking
that it would be too much for people to read
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <string.h>
#include <netdb.h>
#define MYPORT 53491 /* the port users will be connecting to */
#define MSGSIZE 128
#define NAMESIZE 2
static int read_from_client (int fd, struct sockaddr_in *address);
/**
Accepts a string on the command line, sends that string to any
client that connects.
*/
int main (int argc, char *argv[])
{
char *message = "Waiting for another request";
int sockfd; /* datagram socket (2-way) */
struct sockaddr_in my_addr; /* my address */
int addr_size;
int enable = 1;
struct servent *h;
int port;
struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;
/* --- Check args and set up the message --- */
if (argc > 2) { /* usage message */
fprintf(stderr, "Usage: myserver port\n");
exit(1);
}
if (argc == 2) { /* take message from command line */
message = argv[1];
}
/* --- Create the socket --- */
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Can't create socket");
exit(1);
}
/* --- Bind the socket, making its address reusable --- */
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
/* Note the use of INADDR_ANY to get the OS to use whatever local
interfaces are found. */
if (setsockopt(sockfd,
SOL_SOCKET, SO_REUSEADDR,
&enable, sizeof(int)) == -1) {
perror("Can't set socket option");
exit(1);
}
if (bind(sockfd,
(struct sockaddr *)&my_addr,
sizeof(my_addr)) == -1) {
perror("Could not bind");
exit(1);
}
/* --- No listen or accept code here - just wait for clients to make
requests --- */
/* --- Accept datagrams and serve our message --- */
while (1) {
struct sockaddr client_addr; /* client's address */
socklen_t addr_size = sizeof(struct sockaddr);
char client_buffer[MSGSIZE];
int readsz = 0;
char *name;
char *message1;
char *message2;
char *port_s;
char *protocol;
char *protocol1;
char **aliases;
//char *alias1;
int x = 1;
int i = 0;
int cnt;
char *SeqNumber;
/* Read from the client, get the client address filled in. Then we
can respond back to the same client. */
// memset(&client_addr, '\0', sizeof(struct sockaddr));
if ((readsz = recvfrom(sockfd, client_buffer, MSGSIZE, 0,&client_addr,
&addr_size)) > 0)
{
/* cast the client address to internet format for convenience */
struct sockaddr_in *in_addr = (struct sockaddr_in *)&client_addr;
client_buffer[readsz] = '\0';
/* take apart the address and see what we have, then print the
message */
fprintf(stderr, "Client: family=%d, port=%d, addr=%s,
length=%d\n",client_addr.sa_family,ntohs(in_addr->sin_port),inet_ntoa(in_addr->sin_addr),addr_size);
SeqNumber = strtok (client_buffer, ",");
name = strtok (NULL,",");
hostsev.sequence = atoi(SeqNumber);
printf(" there %i \n", hostsev.sequence);
while (1)
{
/* extract protocol from string sequence */
protocol = strtok(NULL,"\n");
fprintf(stderr,"Your request is the following: SeqNumber: %s, Name Of
Service: %s, Protocol: %s \n",SeqNumber,name,protocol);
//h = getservbyname(name,protocol);
if ((h = getservbyname(name,protocol)) == NULL)
{
printf("%s: unknown host %s \n",name,protocol);
exit(0);
}
else
{
printf("The port Number for your request is %d \n",ntohs(h
->s_port));
hostsev.port = h -> s_port;
printf("here %i \n" , ntohs(hostsev.port));
//port_s = atoi(itoa(ntohs(h -> s_port));
//exit(0);
i = 0;
hostsev.names[NAMESIZE] = 0 ;
while (h -> s_aliases != NULL)
{
//printf("Alias %d : \t%s\n",i, h->s_aliases);
for(cnt = 0; h->s_aliases != '\0'; ++cnt)
hostsev.names[cnt] = *h->s_aliases;
//hostsev.names += *h->s_aliases;
//printf("the content of the structure %s \n", hostsev.names);
i++;
} // loop thru aliases
break;
}
}
printf("the content of the structure %s \n", hostsev.names[0]);
/* Next, we write our message and close the socket. */
if (sendto(sockfd, &hostsev, sizeof(hostsev), 0,&client_addr,
addr_size) == -1)
{
perror("Failed to write to client");
}
else
{
fprintf(stderr, "%s\n", message);
}
}
else {
printf("Failed to read from client\n");
}
}
return 0;
}
Thank you