A
Andrew Poelstra
Richard,
Absolutely nothing else was forgotten
thanks
First rule of C programming: Heed _everything_ that Richard
Heathfield says.
Richard,
Absolutely nothing else was forgotten
thanks
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
As this thread bleeds on with non-Standard slop, I am quite confidentAndrew said:First rule of C programming: Heed _everything_ that Richard
Heathfield says.
Barry,
The only printf that isn't working and causing the segmentation fault
is the following
printf("the content of the structure %s \n", hostsev.names[0]);
Barry,
The only printf that isn't working and causing the segmentation fault
is the following
printf("the content of the structure %s \n", hostsev.names[0]);
Frank said:As this thread bleeds on with non-Standard slop, I am quite confident
that heeding everything that Mr. Heathfield says is an impossibly-high
standard for this young man, in particular when clc's most prodigious
contributor has already pointed out exactly where to start fixing
things. I, however, disagree with Mr. Heathfield. I think the original
mistake is not spelling 'servant' correctly, although this is not a
problem in C syntax, rather a grating error in communication generally.
frank
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
[etc]#include <sys/types.h>
#include <sys/socket.h>
int main (int argc, char *argv[])
{
[snippage]
struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;
struct servent *h; [snippage]
char **aliases;
//char *alias1;
i = 0;
hostsev.names[NAMESIZE] = 0 ;
while (h -> s_aliases != NULL) {
for (cnt = 0; h->s_aliases != '\0'; ++cnt)
hostsev.names[cnt] = *h->s_aliases;
i++;
}
printf("the content of the structure %s \n", hostsev.names[0]);
... I am going to highlight both tactical errors (mistakes
in individual steps of execution) and strategic errors (mistakes
in the overall design / plan of the program). There are quite a
few of the latter, but the former are actually bigger problems.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.