|
发表于 2018-6-26 11:20:09
|
显示全部楼层
你这种方式的话,参考下面的代码试试
- /*********************************************************************
- *
- * _ListenAtTcpAddr
- *
- * Starts listening at the given TCP port.
- */
- static int _ListenAtTcpAddr(U16 Port) {
- int sock;
- struct sockaddr_in addr = {0};
- sock = socket(AF_INET, SOCK_STREAM, 0);
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(Port);
- addr.sin_addr.s_addr = INADDR_ANY;
- bind(sock, (struct sockaddr *)&addr, sizeof(addr));
- listen(sock, 1);
- return sock;
- }
- /*********************************************************************
- *
- * _ServerTask
- *
- * Function description
- * This routine is the actual server task.
- * It executes some one-time init code, then runs in an ednless loop.
- * It therefor does not terminate.
- * In the endless loop it
- * - Waits for a conection from a client
- * - Runs the server code
- * - Closes the connection
- */
- __task void _ServerTask(void) {
- int s, Sock, AddrLen;
- U16 Port;
- //
- // Prepare socket (one time setup)
- //
- Port = 5900 + _Context.ServerIndex; // Default port for VNC is is 590x, where x is the 0-based layer index
- //
- // Loop until we get a socket into listening state
- //
- do {
- s = _ListenAtTcpAddr(Port);
- if (s != -1) {
- break;
- }
- os_dly_wait(100); // Try again
- } while (1);
- //
- // Loop once per client and create a thread for the actual server
- //
- while (1) {
- //
- // Wait for an incoming connection
- //
- AddrLen = sizeof(_Addr);
- if ((Sock = accept(s, (struct sockaddr*)&_Addr, &AddrLen)) < 0) {
- continue; // Error
- }
- //
- // Run the actual server
- //
- GUI_VNC_Process(&_Context, _Send, _Recv, (void *)Sock);
- //
- // Close the connection
- //
- closesocket(Sock);
- memset(&_Addr, 0, sizeof(struct sockaddr_in));
- }
- }
复制代码
|
|