1 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
15 int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload)
17 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
18 server_data_t *globals = (server_data_t *) gras_userdata_get();
20 fprintf(stderr, "Argh, killed by %s:%d! Bye folks...\n",
21 gras_socket_peer_name(client), gras_socket_peer_port(client));
26 } /* end_of_kill_callback */
28 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload)
30 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
32 fprintf(stderr, "Cool, we received the message from %s:%d.\n",
33 gras_socket_peer_name(client), gras_socket_peer_port(client));
36 } /* end_of_hello_callback */
38 int server(int argc, char *argv[])
40 gras_socket_t mysock; /* socket on which I listen */
41 server_data_t *globals;
43 gras_init(&argc, argv);
45 globals = gras_userdata_new(server_data_t);
48 gras_msgtype_declare("hello", NULL);
49 gras_msgtype_declare("kill", NULL);
50 mysock = gras_socket_server(atoi(argv[1]));
52 gras_cb_register("hello", &server_hello_cb);
53 gras_cb_register("kill", &server_kill_cb);
55 while (!globals->killed) {
56 gras_msg_handle(-1); /* blocking */
63 int client(int argc, char *argv[])
65 gras_socket_t mysock; /* socket on which I listen */
66 gras_socket_t toserver; /* socket used to write to the server */
68 gras_init(&argc, argv);
70 gras_msgtype_declare("hello", NULL);
71 gras_msgtype_declare("kill", NULL);
72 mysock = gras_socket_server_range(1024, 10000, 0, 0);
74 fprintf(stderr, "Client ready; listening on %d\n",
75 gras_socket_my_port(mysock));
77 gras_os_sleep(1.5); /* sleep 1 second and half */
78 toserver = gras_socket_client(argv[1], atoi(argv[2]));
80 gras_msg_send(toserver, "hello", NULL);
82 "we sent the data to the server on %s. Let's do it again for fun\n",
83 gras_socket_peer_name(toserver));
84 gras_msg_send(toserver, "hello", NULL);
86 fprintf(stderr, "Ok. Enough. Have a rest, and then kill the server\n");
87 gras_os_sleep(5); /* sleep 1 second and half */
88 gras_msg_send(toserver, "kill", NULL);