static EEL_xno n2_tcp_close(EEL_vm *vm) { EEL_value *args = vm->heap + vm->argv; EB_socket *ebs; if(EEL_TYPE(args) != md.net2_socket_cid) return EEL_XWRONGTYPE; ebs = o2EB_socket(args->objref.v); if(!ebs->rs) return EEL_XDEVICECLOSED; if(ebs->rs->n2socket < 0) return EEL_XDEVICECLOSED; if(ebs->rs->status) return ebs->rs->status; if(eb_sockets) eb_sockets[ebs->rs->n2socket] = NULL; if(ebs->rs->sender) { ebs->rs->closed = 1; ebs->rs->status = EEL_XDEVICECLOSED; } else { NET2_TCPClose(ebs->rs->n2socket); sfifo_close(&ebs->rs->fifo); free(ebs->rs); ebs->rs = NULL; } return 0; }
static EEL_xno n2s_destruct(EEL_object *eo) { EB_socket *ebs = o2EB_socket(eo); if(!ebs->rs) return 0; /* Detached! We're done here. */ if(ebs->rs->sender) { ebs->rs->closed = 1; SDL_WaitThread(ebs->rs->sender, NULL); sfifo_close(&ebs->rs->fifo); } NET2_TCPClose(ebs->rs->n2socket); if(ebs->rs->n2socket >= 0) if(eb_sockets) eb_sockets[ebs->rs->n2socket] = NULL; free(ebs->rs); return 0; }
/*---------------------------------------------------------- TCP calls ----------------------------------------------------------*/ static EEL_xno n2_tcp_accept_on(EEL_vm *vm) { EEL_xno x; EEL_value init; EEL_value *args = vm->heap + vm->argv; int s; if(EEL_TYPE(args) == md.ipaddress_cid) s = NET2_TCPAcceptOnIP(o2IPaddress(args->objref.v)); else s = NET2_TCPAcceptOn(eel_v2l(args)); if(s < 0) return EEL_XDEVICEOPEN; eel_l2v(&init, s); x = eel_o_construct(vm, md.net2_socket_cid, &init, 1, vm->heap + vm->resv); if(x) { NET2_TCPClose(s); return x; } return 0; }
int main(int argc, char **argv) { SDL_Event ev; char welcome[] = "Test de connexion PerudOnLan"; char buf[1024]; int len = 0; int count = 0; int s = -1; int next[0xffff]; int n = -1; IPaddress *monIP = NULL; memset(next, 0, sizeof(next)); mySDLInitOrQuit(SDL_INIT_EVENTTHREAD | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE); if((NET2_ResolveHost(monIP,"localhost",6666))!=0 || monIP==NULL) { fprintf(stderr,"errreur à la récupération de l'IP"); return EXIT_FAILURE; } NET2_TCPAcceptOn(monIP->port); while (FE_WaitEvent(&ev)) { switch (ev.type) { case SDL_USEREVENT: switch (NET2_GetEventType(&ev)) { case NET2_TCPACCEPTEVENT: count++; printf("accept(%d)\n", NET2_GetSocket(&ev)); s = NET2_GetSocket(&ev); if (-1 == NET2_TCPSend(s, welcome, sizeof(welcome) - 1)) { printf("WELCOME SEND FAILED\n"); } next[s] = 0; break; case NET2_TCPRECEIVEEVENT: s = NET2_GetSocket(&ev); while (0 != (len = NET2_TCPRead(s, buf, sizeof(buf)))) { int i; n = next[s]; for (i = 0; i < len; i++) { if (welcome[n] != buf[i]) { printf("\nout of sync %c != %c\n", welcome[n], buf[i]); exit(0); } n = (n + 1) % (sizeof(welcome) - 1); } next[s] = n; if (-1 == NET2_TCPSend(s, buf, len)) { printf("RECIEVE SEND FAILED\n"); } } break; case NET2_TCPCLOSEEVENT: printf("close(%d)\n", NET2_GetSocket(&ev)); NET2_TCPClose(NET2_GetSocket(&ev)); count--; /*if (0 >= count) { exit(0); } */ break; case NET2_ERROREVENT: printf("Error: %s(%d)\n", NET2_GetEventError(&ev), NET2_GetSocket(&ev)); exit(0); break; } break; case SDL_QUIT: mySDL_Quit(); exit(0); break; } } mySDL_Quit(); }
int main(int argc, char **argv) { SDL_Event ev; // an SDL event char buf[1024]; // a message buffer int len = 0; // some counters int count = 0; int socks = 0; // the number of // connected sockets, // which is also the // number of connected // clients. mySDLInitOrQuit(SDL_INIT_EVENTTHREAD | // initialize SDL. We need SDL_INIT_VIDEO | // events and it doesn't SDL_INIT_NOPARACHUTE); // work without video // This is where we tell NET2 to start accepting TCP/IP connections on // port 6666. This call does not wait for a connection to come in. It // tells NET2 to do the waiting and it returns immediately. After this // call returns your program will get an event any time a computer // trys to connect to the port given in this call. NET2_TCPAcceptOn(6666); while (FE_WaitEvent(&ev)) // wait for events { //printSDLEvent(&ev); switch (ev.type) { // all NE2 events are SDL_USEREVENTs but not all user events are NET2 // events. If you use user events in your code you need to be careful // to make sure you are not using the same event codes that NET2 uses. case SDL_USEREVENT: switch(NET2_GetEventType(&ev)) { // This next piece of code handles an accept event. This event tells // us that an connection has been accepted. Here, all we do is count // it. You would normally take some other action. case NET2_TCPACCEPTEVENT: printf("accept(%d)\n", NET2_GetSocket(&ev)); printNET2Event(&ev); socks++; break; // This next piece of code is for handling receive // events. This kind of event tells us we have input waiting // on a socket. You need to grab all of it. No that we get the // socket from the event. You can use the socket to tell you // which user sent the information to you. case NET2_TCPRECEIVEEVENT: while (0 != (len = NET2_TCPRead(NET2_GetSocket(&ev), buf, sizeof(buf)))) { count += len; } break; // If an error occurs on a socket or the other computer closes // the connection you will get a close event. When you get a // close event you must close the socket. Use the socket in // the event to tell you which connection went away. case NET2_TCPCLOSEEVENT: printf("close(%d)\n", NET2_GetSocket(&ev)); printNET2Event(&ev); NET2_TCPClose(NET2_GetSocket(&ev)); // close the socket printf("count=%d\n", count); fflush(NULL); socks--; if (0 >= socks) { //exit(0); } //count = 0; break; // Sometimes you will get errors. It is best to keep track of // them and try to figure out what is causing them. case NET2_ERROREVENT: printf("Error: %s(%d)\n", NET2_GetEventError(&ev), NET2_GetSocket(&ev)); printNET2Event(&ev); break; } break; case SDL_QUIT: // time to quit mySDL_Quit(); // clean up and exit exit(0); break; } } mySDL_Quit(); }