472,791 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

Help needed in a simple UDP socket program

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);
}
}

May 15 '06 #1
2 5092
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.

May 15 '06 #2
santosh wrote:
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.


Hello!
Thanks, i will try it there!
May 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
2
by: Timothy Stark | last post by:
Hello folks, I am working on my program and was trying to implement 'dynamic' classes by using virtual function calls but it did not work. I want to convert from 'class test1' to 'class test2'...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
6
by: Astroman | last post by:
Hi guys and girls. This is my first time posting here so go easy :) . I was wondering if someone could please interpret how this csum() function works in the following C code. I know that the...
2
by: lucifer | last post by:
hi, i am creating an simple http server ie it serves only static pages . u can compile the code then use ur browser if it is IE then the it shows the page but the http header is also shown...
0
by: mhetfield | last post by:
Hi, I'm writing a client-server socket program. the client will be an instance of the well-known telnet application. i want to implement a simple authentication between the server and the client. ...
3
by: Har | last post by:
I have created a small client application with loopback address (127.0.0.1) so that same machine act as server and client. but i want to know whether we have to create seperate server application and...
1
by: orehian | last post by:
Construct a one-time password system. · Write a server code and a client code. The server code takes as input a username and a one-time password from the client and then sends a message...
2
by: fredszky | last post by:
Hello I am very new to perl, however i managed to make this server/client work with udp, now i would like to do the same thing but with TCP/IP, what must i do? Server: #!perl -w # Server...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.