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) {
16 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
17 server_data_t *globals=(server_data_t*)gras_userdata_get();
19 fprintf(stderr,"Argh, killed by %s:%d! Bye folks...\n",
20 gras_socket_peer_name(client), gras_socket_peer_port(client));
25 } /* end_of_kill_callback */
27 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload) {
28 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
30 fprintf(stderr,"Cool, we received the message from %s:%d.\n",
31 gras_socket_peer_name(client), gras_socket_peer_port(client));
34 } /* end_of_hello_callback */
36 int server(int argc, char *argv[]) {
37 gras_socket_t mysock; /* socket on which I listen */
38 server_data_t *globals;
40 gras_init(&argc,argv);
42 globals=gras_userdata_new(server_data_t);
45 gras_msgtype_declare("hello", NULL);
46 gras_msgtype_declare("kill", NULL);
47 mysock = gras_socket_server(atoi(argv[1]));
49 gras_cb_register("hello",&server_hello_cb);
50 gras_cb_register("kill",&server_kill_cb);
52 while (!globals->killed) {
53 gras_msg_handle(-1); /* blocking */
60 int client(int argc, char *argv[]) {
61 gras_socket_t mysock; /* socket on which I listen */
62 gras_socket_t toserver; /* socket used to write to the server */
64 gras_init(&argc,argv);
66 gras_msgtype_declare("hello", NULL);
67 gras_msgtype_declare("kill", NULL);
68 mysock = gras_socket_server_range(1024, 10000, 0, 0);
70 fprintf(stderr,"Client ready; listening on %d\n", gras_socket_my_port(mysock));
72 gras_os_sleep(1.5); /* sleep 1 second and half */
73 toserver = gras_socket_client(argv[1], atoi(argv[2]));
75 gras_msg_send(toserver,"hello", NULL);
76 fprintf(stderr,"we sent the data to the server on %s. Let's do it again for fun\n", gras_socket_peer_name(toserver));
77 gras_msg_send(toserver,"hello", NULL);
79 fprintf(stderr,"Ok. Enough. Have a rest, and then kill the server\n");
80 gras_os_sleep(5); /* sleep 1 second and half */
81 gras_msg_send(toserver,"kill", NULL);