|
|
【程序】uip多端口TCP多次发送数据 |
一派护法 十九级 |
void uip_appcall(void) { uint8_t send = 0; uint16_t a, b;
if (ntohs(uip_conn->lport) == 8000) { if (uip_connected()) { uip_conn->appstate.step = 0; send = 1;
sprintf(uip_conn->appstate.buf, "Connected! id=%d, port=%d\n", uip_conn - uip_conns, ntohs(uip_conn->lport)); // 显示所用的conn连接数组下标号, 以及端口号 }
if (uip_rexmit()) send = 1;
if (uip_newdata()) { uip_conn->appstate.step++; send = 1;
if (uip_conn->appstate.step == 1) { ((char *)uip_appdata)[uip_len] = '\0'; sscanf(uip_appdata, "%d, %d", &a, &b); sprintf(uip_conn->appstate.buf, "sum=%d\n", a + b); } }
if (send) { switch (uip_conn->appstate.step) { case 0: case 1: uip_send(uip_conn->appstate.buf, strlen(uip_conn->appstate.buf)); break; case 2: uip_send_P(PSTR("Ignored!\n")); break; } } } else if (ntohs(uip_conn->lport) == 3000) { if (uip_connected()) { uip_conn->appstate.step = 0; send = 1; }
if (uip_rexmit()) send = 1;
if (uip_acked()) { send = 1; uip_conn->appstate.step++; }
if (send) { switch (uip_conn->appstate.step) { case 0: uip_send_P(PSTR("The closesocket function closes a socket. Use it to release the socket descriptor passed in the s parameter. Note that the socket descriptor passed in the s parameter may immediately be reused by the system as soon as closesocket function is issued. ")); break; case 1: uip_send_P(PSTR("As a result, it is not reliable to expect further references to the socket descriptor passed in the s parameter to fail with the error WSAENOTSOCK. A Winsock client must never issue closesocket on s concurrently with another Winsock function call.\n")); break; case 2: uip_send_P(PSTR("Any pending overlapped send and receive operations ( WSASend/ WSASendTo/ WSARecv/ WSARecvFrom with an overlapped socket) issued by any thread in this process are also canceled. ")); break; case 3: uip_send_P(PSTR("Any event, completion routine, or completion port action specified for these overlapped operations is performed. The pending overlapped operations fail with the error status WSA_OPERATION_ABORTED.\n")); break; case 4: uip_send_P(PSTR("An application should not assume that any outstanding I/O operations on a socket will all be guaranteed to completed when closesocket returns. ")); break; case 5: uip_send_P(PSTR("The closesocket function will initiate cancellation on the outstanding I/O operations, but that does not mean that an application will receive I/O completion for these I/O operations by the time the closesocket function returns. ")); break; case 6: uip_send_P(PSTR("Thus, an application should not cleanup any resources (WSAOVERLAPPED structures, for example) referenced by the outstanding I/O requests until the I/O requests are indeed completed.\n")); break; case 7: uip_send_P(PSTR("An application should always have a matching call to closesocket for each successful call to socket to return any socket resources to the system.\n")); break; case 8: uip_send_P(PSTR("The linger structure maintains information about a specific socket that specifies how that socket should behave when data is queued to be sent and the closesocket function is called on the socket.\n")); break; case 9: uip_send_P(PSTR("The l_onoff member of the linger structure determines whether a socket should remain open for a specified amount of time after a closesocket function call to enable queued data to be sent. This member can be modified in two ways.\n")); break; } } } }
void uip_udp_appcall(void) { }
void myapp_init(void) { uip_listen(HTONS(3000)); uip_listen(HTONS(8000)); }
|
一派护法 十九级 |
状态结构体的定义: typedef struct { uint8_t step; char buf[40]; } uip_tcp_appstate_t;
|
一派护法 十九级 |
AVR存储空间的使用情况: Program Memory Usage : 13974 bytes 85.3 % Full Data Memory Usage : 997 bytes 97.4 % Full
|
一派护法 十九级 |
【PC端程序1】 #include <stdio.h> #include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
void display(SOCKET sock, char *buf, int len) { len = recv(sock, buf, len, (int)NULL); buf[len] = '\0'; printf("%s", buf); }
int main(void) { char buf[100]; int a, b; SOCKADDR_IN addr; SOCKET sock; WSADATA wsadata; WSAStartup(MAKEWORD(2, 2), &wsadata); addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.50"); addr.sin_family = AF_INET; addr.sin_port = htons(8000); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connect(sock, (SOCKADDR *)&addr, sizeof(SOCKADDR)) == 0) { display(sock, buf, sizeof(buf));
printf("Please input two numbers: "); scanf_s("%d%d", &a, &b); sprintf_s(buf, sizeof(buf), "%d, %d", a, b); send(sock, buf, strlen(buf), (int)NULL); display(sock, buf, sizeof(buf)); send(sock, "sth", 3, (int)NULL); display(sock, buf, sizeof(buf));
closesocket(sock); } else printf("Connection failed!\n"); WSACleanup(); return 0; }
|
一派护法 十九级 |
【PC端程序2】 #include <stdio.h> #include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
void display(SOCKET sock) { char buf[500]; int len = recv(sock, buf, sizeof(buf), (int)NULL); buf[len] = '\0'; printf("%s", buf); }
int main(void) { int i; SOCKADDR_IN addr; SOCKET sock; WSADATA wsadata; WSAStartup(MAKEWORD(2, 2), &wsadata); addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.50"); addr.sin_family = AF_INET; addr.sin_port = htons(3000); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connect(sock, (SOCKADDR *)&addr, sizeof(SOCKADDR)) == 0) { for (i = 0; i <= 9; i++) display(sock);
closesocket(sock); } else printf("Connection failed!\n"); WSACleanup(); return 0; }
|
一派护法 十九级 |
在AVR Studio中,为了节约SRAM,sprintf和scanf最好改成sprintf_P和scanf_P,从Flash中读取格式字符串。 例如: strcpy_P(uip_appdata, PSTR("This is a string.")); sscanf_P(uip_appdata, PSTR("%d, %d"), &a, &b); sprintf_P(uip_conn->appstate.buf, PSTR("sum=%d\n"), a + b);
|
|
|
|