📄 example.c
字号:
#include "example.h"#include "uip.h"#include <string.h>/* * Declaration of the protosocket function that handles the connection * (defined at the end of the code). */static int handle_connection(struct example_state *s);/*---------------------------------------------------------------------------*//* * The initialization function. We must explicitly call this function * from the system initialization code, some time after uip_init() is * called. */voidexample_init(void){ /* We start to listen for connections on TCP port 1000. */ uip_listen(HTONS(1000));}/*---------------------------------------------------------------------------*//* * In hello-world.h we have defined the UIP_APPCALL macro to * hello_world_appcall so that this funcion is uIP's application * function. This function is called whenever an uIP event occurs * (e.g. when a new connection is established, new data arrives, sent * data is acknowledged, data needs to be retransmitted, etc.). */voidexample_appcall(void){ /* * The uip_conn structure has a field called "appstate" that holds * the application state of the connection. We make a pointer to * this to access it easier. */ struct example_state *s = &(uip_conn->appstate); unsigned char testbuf[200]; int i; for(i=0;i<200;i++) testbuf[i]=0x55; /* * If a new connection was just established, we should initialize * the protosocket in our applications' state structure. */ if(uip_connected()) { // PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer)); uip_send("Welcome!\n", 9); } /* * Finally, we run the protosocket function that actually handles * the communication. We pass it a pointer to the application state * of the current connection. */ if(uip_acked() && s->state == WELCOME_SENT) { s->state = WELCOME_ACKED; } if(uip_newdata()) { //uip_send("ok\n", 3); uip_send("\nok\n", 4); } if(uip_rexmit()) { switch(s->state) { case WELCOME_SENT: uip_send("Welcome!\n", 9); break; case WELCOME_ACKED: uip_send("ok\n", 3); break; } } // handle_connection(s);}/*---------------------------------------------------------------------------*//* * This is the protosocket function that handles the communication. A * protosocket function must always return an int, but must never * explicitly return - all return statements are hidden in the PSOCK * macros. *//*static inthandle_connection(struct hello_world_state *s){ PSOCK_BEGIN(&s->p); PSOCK_SEND_STR(&s->p, "Hello. What is your name?\n"); PSOCK_READTO(&s->p, '\n'); strncpy(s->name, s->inputbuffer, sizeof(s->name)); PSOCK_SEND_STR(&s->p, "Hello "); PSOCK_SEND_STR(&s->p, s->name); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p);}*//*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -