Hello, new here and need some help please [Archive] - C Board

PDA

View Full Version : Hello, new here and need some help please


Dark1557
03-30-2008, 05:17 AM
Hi guys, am new here so nice to meet u all
i have a problem with a certain client/server program.
the client sends a request to the server and asks it to add 100 to 1557. the server receives the request, does the computation and sends back the results as a string message. can any one help me do this please
Client code

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* defines structure in_addr */
#include <netdb.h> /* defines structure hostent */
#include <stdio.h>
#include <string.h>
void main( int argc, char **argv)
{

#define MAXCHAR 80

int size_message, server_len, s; char client_message[MAXCHAR];

struct sockaddr_in client_addr, server_addr;

struct hostent *server_ptr;

/* create a connectionless socket with internet domain */
s = socket(PF_INET,SOCK_DGRAM,0);

/* fill in data structure of client address */
client_addr.sin_family = PF_INET;

/* accept connection on any internet interface */
client_addr.sin_addr.s_addr = htonl (INADDR_ANY);

/* assign arbitrary port number */
client_addr.sin_port= htons(0);

/* bind local address to the socket */
bind(s,(struct sockaddr *)&client_addr,sizeof(client_addr));



/* get address of server and fill in its data structure */
server_ptr = gethostbyname(argv[1]);
server_addr.sin_family = PF_INET;
server_addr.sin_addr= *(struct in_addr *)(server_ptr->h_addr);
server_addr.sin_port= htons(7785);
/* send server a message */
strcpy (client_message,"ADD 100 to my Id 1557");
size_message = strlen(client_message);
printf("The message sent is %s\n", client_message);
printf("The server internet address is %s\n",
inet_ntoa(*server_ptr->h_addr_list));
sendto(s,client_message,size_message,0,
(struct sockaddr *)&server_addr,sizeof(server_addr));
}




Server code

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* defines structure in_addr */
#include <netdb.h> /* defines structure hostent */
#include <stdio.h>
#include <string.h>
main()
{
#define MAXCHAR 80
int s;
struct sockaddr_in client_addr, server_addr;
char server_message[MAXCHAR];
int size_message, client_len;
/* create a connectionless socket with internet domain */
s = socket(PF_INET,SOCK_DGRAM,0);
/* fill in data structure of server address */
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr = htonl (INADDR_ANY);
server_addr.sin_port= htons(7785);
/* bind local address to the socket */
bind(s,(struct sockaddr *) &server_addr, sizeof(server_addr));
while(1) {

/* receive message from client */
/* recvfrom blocks until a message arrives */
/* it permits to retrieve the source address of message */
client_len = sizeof (client_addr);
recvfrom(s,server_message,MAXCHAR,0,
(struct sockaddr *)&client_addr,&client_len);
printf("client address: %s\n", inet_ntoa(client_addr.sin_addr));
printf("message received from client: %s\n",server_message);

}
}








so pleeeeease can any one help me?? i'll be very grateful

thanks

Salem
03-30-2008, 05:32 AM
So is this a
- communication problem (the message doesn't arrive)
- parsing problem (you don't know how to get "100" and "1557" out of the string)
- a math problem (how to do the addition)

Or something else?

Like for example
- does it compile?
- using ethereal / wireshark, can you see the message in transit at all?

Dark1557
03-30-2008, 05:37 AM
thanks 4 the reply
the communication is ok
but the computation is a problem
i dont know how to do these(
parsing problem (you don't know how to get "100" and "1557" out of the string)
- a math problem (how to do the addition)
)
also to reply to the client with the results
i hope u can help me and thank u very much

Salem
03-30-2008, 07:07 AM
Perhaps sscanf ?

Dark1557
03-30-2008, 07:41 AM
how can i use it with my program??
can u explain please
and thank u very much 4 your time

vart
03-30-2008, 07:48 AM
#include <stdio.h>
int main(void)
{
int a,b;
char buf[] = "ADD 100 to my Id 1557";
sscanf(buf,"ADD %d to my Id %d", &a,&b);
return 0;
}

Dark1557
03-30-2008, 08:34 AM
thank u very much

how should i include it within the program???

Salem
03-30-2008, 09:31 AM
Personally, I think you're missing a hell of a lot of basic information on C programming, and you've just launched yourself into the deep end with some network programming.

Unless you get this in place, then every small step towards whatever you want to do will be a struggle.

Dark1557
03-30-2008, 12:33 PM
actually, am doing this for a friend.
am not an expert in c programming (sorry!!)

however, thank you for your help, and vart too.

Salem
03-30-2008, 12:56 PM
Perhaps you should ask your friend to join the forum and ask the questions directly.

You might also consider that if you're doing your friends' homework what exactly you're both getting out of this, because learning by doing is far more effective than just reading about it.

Dark1557
03-31-2008, 03:43 AM
Help?!! Please???

matsp
03-31-2008, 03:49 AM
If you are trying to become a Formula One mechanic, you probably need to learn how to change the wheels on a regular car first. If you don't understand how to use sscanf() and the code-snippet posted, you probably should be studying the basics first. Network programming is not trivial.

--
Mats