int send_recv(int sock, const char *name) { int to = 100; assert(nn_setsockopt (sock, NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof (to)) >= 0); while(1) { recv_name(sock, name); sleep(1); send_name(sock, name); } }
/* Distribute keys */ int distribute_key (void) { int result; golle_num_t temp = golle_num_new (); if (!temp) { fprintf (stderr, "Memory error\n"); return 1; } result = h_to_bin (key.h); if (result != 0) { return result; } /* Send name to opponent */ result = send_name (opponent); if (result != 0) { return result; } /* Receive name from opponent */ result = recv_name (opponent, opponent_name); if (result != 0) { return result; } printf ("Opponent's name is '%s'\n", opponent_name); /* Send h to opponent */ result = send_h (opponent); if (result != 0) { return result; } /* Receive h from opponent */ result = recv_h (opponent, temp); if (result != 0) { return result; } printf ("Received h from %s\n", opponent_name); golle_error err = golle_key_accum_h (&key, temp); if (err != GOLLE_OK) { result = (int)err; fprintf (stderr, "H value accumulation failed.\n"); } golle_bin_clear (&hbin); golle_num_delete (temp); printf ("Key is distributed.\n"); return result; }
/* Connect and talk to the server */ int main() { struct sockaddr_in server; struct hostent *host; int s; /* Create an Internet family, stream socket */ s = socket(AF_INET, SOCK_STREAM, 0); /* Did that work? */ if (s < 0) { perror("socket()"); exit(EXIT_FAILURE); } /* We are running the server on localhost for the minute */ if ((host = gethostbyname("localhost")) == NULL) { perror("gethostbyname()"); exit(EXIT_FAILURE); } /* Fill in the socket address structure */ memset((char *)&server, '\0', sizeof (server)); server.sin_family = AF_INET; server.sin_port = htons(PORTNUM); memcpy((char *)&server.sin_addr, host->h_addr_list[0], host->h_length); /* Connect to the server */ if (connect(s, (struct sockaddr *)&server, sizeof (server)) < 0) { perror("connect()"); exit(EXIT_FAILURE); } /* Send our name first */ send_name(s); /* Now send a message */ send_message(s); /* Close the socket */ close(s); return (0); }
void ini_network() { SDL_Thread *thread_network; int numbytes; char buf[256]; struct hostent *he; struct sockaddr_in their_addr; // connectorâÂÂs address information //printf("servidor:"); //scanf("%s",buf); if ((he=gethostbyname(sserver)) == NULL) { // get the host info perror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // short, network byte order their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf, 256-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; printf("Received: %s",buf); sscanf(buf,"wellcome:%d",&(pj->id)); send_name(); thread_network = SDL_CreateThread (network, NULL); }
/* ip_addr is now in network byte order * * first we have to get hold of the portnumber to * the node through epmd at that host * */ int ei_xconnect_tmo(ei_cnode* ec, Erl_IpAddr adr, char *alivename, unsigned ms) { struct in_addr *ip_addr=(struct in_addr *) adr; int rport = 0; /*uint16 rport = 0;*/ int sockd; int one = 1; int dist = 0; ErlConnect her_name; unsigned her_flags, her_version; erl_errno = EIO; /* Default error code */ EI_TRACE_CONN1("ei_xconnect","-> CONNECT attempt to connect to %s", alivename); if ((rport = ei_epmd_port_tmo(ip_addr,alivename,&dist, ms)) < 0) { EI_TRACE_ERR0("ei_xconnect","-> CONNECT can't get remote port"); /* ei_epmd_port_tmo() has set erl_errno */ return ERL_NO_PORT; } /* we now have port number to enode, try to connect */ if((sockd = cnct((uint16)rport, ip_addr, sizeof(struct in_addr),ms)) < 0) { EI_TRACE_ERR0("ei_xconnect","-> CONNECT socket connect failed"); /* cnct() has set erl_errno */ return ERL_CONNECT_FAIL; } EI_TRACE_CONN0("ei_xconnect","-> CONNECT connected to remote"); /* FIXME why connect before checking 'dist' output from ei_epmd_port() ?! */ if (dist <= 4) { EI_TRACE_ERR0("ei_xconnect","-> CONNECT remote version not compatible"); goto error; } else { unsigned our_challenge, her_challenge; unsigned char our_digest[16]; if (send_name(sockd, ec->thisnodename, (unsigned) dist, ms)) goto error; if (recv_status(sockd, ms)) goto error; if (recv_challenge(sockd, &her_challenge, &her_version, &her_flags, &her_name, ms)) goto error; our_challenge = gen_challenge(); gen_digest(her_challenge, ec->ei_connect_cookie, our_digest); if (send_challenge_reply(sockd, our_digest, our_challenge, ms)) goto error; if (recv_challenge_ack(sockd, our_challenge, ec->ei_connect_cookie, ms)) goto error; put_ei_socket_info(sockd, dist, null_cookie, ec); /* FIXME check == 0 */ } setsockopt(sockd, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)); setsockopt(sockd, SOL_SOCKET, SO_KEEPALIVE, (char *)&one, sizeof(one)); EI_TRACE_CONN1("ei_xconnect","-> CONNECT (ok) remote = %s",alivename); erl_errno = 0; return sockd; error: EI_TRACE_ERR0("ei_xconnect","-> CONNECT failed"); closesocket(sockd); return ERL_ERROR; } /* ei_xconnect */
int main(int argc, char *argv[]) { const int num_args = 7; char name[NAME_LEN + 1], host[HOST_LEN + 1], port[PORT_LEN + 1]; int ch; if (argc != num_args) { return usage(); } while ((ch = getopt(argc, argv, "n:h:p:")) != -1) { switch (ch) { case 'n': { strncpy(name, optarg, NAME_LEN); break; } case 'h': { strncpy(host, optarg, HOST_LEN); break; } case 'p': { strncpy(port, optarg, PORT_LEN); break; } default: { return usage(); } } } if (optind != num_args) { return usage(); } name[NAME_LEN] = '\0'; host[HOST_LEN] = '\0'; port[PORT_LEN] = '\0'; signal(SIGINT, &quit_game); int sock = get_socket(host, port); if (sock == -1) { fprintf(stderr, "async: could not connect to `%s:%s`\n", host, port); return 1; } int sp = dup(sock); printf("%d\n", sp); pp = fdopen(sp, "r"); printf("%p\n", pp); FILE *io = fdopen(sock, "r+"); if (io == NULL) { fprintf(stderr, "async: could not open socket\n"); return 1; } game.io = io; game.name = malloc(sizeof(char) * (strlen(name) + 1)); strcpy(game.name, name); printf("async: joining server `%s:%s` with name `%s`\n", host, port, game.name); if (!send_name()) { fprintf(stderr, "async: could not send name to server\n"); return 1; } if (!receive_id()) { fprintf(stderr, "async: could not receive player id from server\n"); return 1; } if (!download_map()) { fprintf(stderr, "async: could not download map\n"); return 1; } else { printf("async: downloaded map, `%lu` bytes\n", strlen(game.map)); } game.player_loc.x = game.width / 2; game.player_loc.y = game.height / 2; game.player_dir = NORTH; pthread_create(&listener_thread, NULL, &game_listener, NULL); pthread_detach(listener_thread); run_game(); return 0; }
void Button::emit_name() { emit send_name(this->name); }