Help needed in a simple UDP socket program

V

Vitali Gontsharuk

Hi!

I have a problem programming a simple client-server game, which is
called pingpong ;-)
The final program will first be started as a server (nr. 2) and then as
a client. The client then sends the message "Ping" to the server, which
reads it and answers with a "Pong".
The game is really simple and the coding should be also very simple! But
for me it isn't.
By the way, the program uses datagram sockets (UDP). And, I'm using
Cygwin (don't think the problem occurs due to that).
What the program now does is, upon the client start, to send the "Ping"
message to the server, which in turn sends an answer to the client. The
"sendto" invoked in the start_server() function deliveres the length of
the used buffer "mesg" as a result - but the message can't be received
by the client, the execution blocks!
It must be an easy to solve problem, but I'm now at the end of my nerves
and can't see what the problem is.
If you could help me, I would really appreciate that!

Thanks in advance,
Vitali Gontsharuk

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

void error(char *msg);
void debug_s(char *);
void debug_i(int);
char *get_ip_for_hostname(char *hostname);

void server_start();
void client_start(char *server_ip_addr);

/*int main(int argc, char *argv[]);*/

#include "pingpong.h"

#define CLI_MOD 1
#define SERV_MOD 2
#define MAX_MSG_SIZE 512
char mesg[MAX_MSG_SIZE] = "";
char debug_buf[256]="";

const int cli_port = 2001;
const int serv_port = 2002;
const char *ping = "Ping";
const char *pong = "Pong";

void error(char *msg)
{
perror(msg);
exit(1);
}

void debug_s(char *msg)
{
printf("DEBUG: %s\n", msg);
}

void debug_i(int number)
{
printf("DEBUG: %d\n", number);
}

void print_serv(char *msg)
{
printf("SERVER: '%s'\n", msg);
}

void print_cli(char *msg)
{
printf("CLIENT: '%s'\n", msg);
}

char *get_ip_for_hostname(char *hostname)
{
struct hostent *my_hostent = gethostbyname(hostname);
return my_hostent->h_addr;
}

void server_start(char *client_hostname)
{
int temp = 0;
int sockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;
/*
* Make the initializations
*/
memset(mesg, 0, MAX_MSG_SIZE);
memset(&serv_addr, 0, sizeof(serv_addr));
memset(&cli_addr, 0, sizeof(cli_addr));

/*
* Create an unnamed socket
*/
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
/*
* Initialize the server address structure and bind it to the
* previously created socket
*/
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(serv_port);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <
0)
error("ERROR on binding");
/*
* Initialize the client address structure
*/
memset(&cli_addr, 0, sizeof(cli_addr));

cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr =
inet_addr(get_ip_for_hostname(client_hostname));
cli_addr.sin_port = htons(cli_port);
clilen = sizeof(cli_addr);

/*
* while(-1 < recv(sockfd,buffer,sizeof(buffer),0)) {
*/
while((temp=
recvfrom(sockfd, mesg, MAX_MSG_SIZE, 0,
(struct sockaddr *) &cli_addr,
(socklen_t *) & clilen))>-1) {
sprintf(debug_buf,"%d chars received from client",temp);
print_serv(debug_buf);
print_serv(mesg);
if (0 != strcmp(mesg, ping))
error(strcat("Got the wrong string from the client: ", mesg));
print_serv("Got Ping!");
memset(mesg, 0, MAX_MSG_SIZE);
strcpy(mesg, pong);
sprintf(debug_buf,"The following message will be sent to the client: %s",mesg);
print_serv(debug_buf);
print_serv("sending pong to the client...");
temp =
sendto(sockfd, mesg, MAX_MSG_SIZE, 0,
(struct sockaddr *) &cli_addr, clilen);
sprintf(debug_buf,"%d chars sent to client",temp);
print_serv(debug_buf);
if (-1 == temp)
error("Could not send Pong to the client!");
}
printf("Server ended!");
}

void client_start(char *server_hostname)
{
int temp = 0;
int sockfd, servlen, clilen;
struct sockaddr_in serv_addr, cli_addr;
struct hostent *server;
/*
* Make the initializations
*/
memset(mesg, 0, MAX_MSG_SIZE);
memset(&serv_addr, 0, sizeof(serv_addr));
memset(&cli_addr, 0, sizeof(cli_addr));
print_serv(mesg);

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
/*
* Initialize the client address structure
*/
/*
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = INADDR_ANY;
cli_addr.sin_port = htons(cli_port);
if (bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0)
error("ERROR on binding");
*/
/*
* Initialize the server address structure
*/
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr =
inet_addr(get_ip_for_hostname(server_hostname));
serv_addr.sin_port = htons(serv_port);
servlen = sizeof(serv_addr);
if ((connect
(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))) < 0)
error("ERROR connecting");
strcpy(mesg,ping);
while ((temp = write(sockfd, mesg, MAX_MSG_SIZE)) != -1) {
sprintf(debug_buf, "%d chars sent to server.", temp);
print_cli(debug_buf);
print_cli("Receiving pong from server...");
temp=recvfrom(sockfd, mesg, MAX_MSG_SIZE, 0,
(struct sockaddr *) &serv_addr, &servlen);
sprintf(debug_buf, "%d received from server",temp);
print_cli(debug_buf);
if(temp==-1)
error("Could not receive pong from server!");
print_cli("Got pong!");
if (strcmp(mesg, pong) != 0) {
error("Client: kein Pong als Antwort!");
}
memset(mesg, 0, MAX_MSG_SIZE);
strcpy(mesg,ping);
}
printf("Client ended!");
}

int main(int argc, char *argv[])
{
if (argc != 3) {
printf(
"Usage: %s <mode: 1(client)|2(server)> <hostname (of the other side)>\n",
argv[0]);
exit(1);
}
int mode = atoi(argv[1]);
char *hostname = argv[2];
if (mode == CLI_MOD) {
client_start(hostname);
} else if (mode == SERV_MOD) {
server_start(hostname);
}
}
 
S

santosh

Vitali Gontsharuk wrote:> Hi!
I have a problem programming a simple client-server game, which is
called pingpong ;-)
The final program will first be started as a server (nr. 2) and then as
a client. The client then sends the message "Ping" to the server, which
reads it and answers with a "Pong".
The game is really simple and the coding should be also very simple! But
for me it isn't.
By the way, the program uses datagram sockets (UDP). And, I'm using
.... snip ...

All this is well beyond the scope of this group which deals with ISO C.
Your question would be better answered in a group like
comp.unix.programming etc.
 
V

Vitali Gontsharuk

santosh said:
Vitali Gontsharuk wrote:> Hi!

... snip ...

All this is well beyond the scope of this group which deals with ISO C.
Your question would be better answered in a group like
comp.unix.programming etc.

Hello!
Thanks, i will try it there!
 

Ask a Question

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.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top