Recv函数是一个常用的C语言库函数,用于从网络连接或文件描述符读取数据到提供的缓冲区中。
Recv函数的语法通常如下所示:
int recv (SOCKET s, char *buf, int len, int flags);
其中,s是网络连接的集成描述符,buf是要接受数据存放位置,len指定可接受数据字节数,flags则指定接受操作中的其他参数,如是否阻塞等。
使用Recv函数时,可以将其用于实现 服务器 端和客户端之间的数据传送功能,以完成用户与服务器之间的通信。例如,假设客户端在服务器端注册时需要将某些特定数据发送给服务器端,此时可以在服务器端的注册请求处理函数中使用Recv函数来接受客户端发送的特定数据。
除了上面提到的服务器端与客户端之间的通信,Recv函数还可以用于访问文件描述符,如管道、FIFO等,实现进程之间的数据通信,管道和FIFO是一种特殊结构,能够让一个进程的数据,在不同的进程之间传输,从而完成客户端与服务器端的数据传输工作。
此外,Recv函数可以把数据从网络中收回。例如,聊天室程序在接受客户端发回的信息时,可以使用Recv函数收取数据。
以上就是Recv函数的基本用途,使用方式也很简单,作为一种常用的编程函数,Recv函数可以起到简化网络编程过程,为程序设计师提供了极大的便利。
香港服务器首选树叶云,2H2G首月10元开通。树叶云(shuyeidc.com)提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。

TCP连接下,使用API中的Send函数发完数据后,是否只有服务器收到数据或出错后,才有返回值?
对应于阻隔式socket连接, send方法必须等到服务端接收到后或发生异常时才返回.
如何使用Delphi设计强大的服务器程序
vara,b:Pbyte;i :integer;ResultBuffer :array [] of byte;beginfor i := 1 to Sizeof(Buffer) div 8 dobegin a := @Buffer[(i-1)*8+1]; b := @ResultBuffer[(i-1)*8+1] Des(a^,b^,true); //这里使用DES加解密处理end;end多使用WSASend,WSARecv等WinSocket 2函数,不要使用Send,Recv函数这个主要看你的服务器运行在什么系统中了,如果运行在WIN系统里,最好使用WSA系统的函数,因为Microsoft毕竟将它们都优化了高效的服务器一定要使用线程池技术,使用多少线程合理,需要线程处理什么样的数据。 我个人认为如果要使用线程池的技术,一定要处理那些最费时的操作,如数据库的查询操作。 如果服务器使用了“池”的概念,这就又出现了一个问题,如何高效的分配池呢?我在程序中大量的使用池,如线程池,数据池等。 当数据到达的时候,如何分配池呢?这里就不告诉大家了,以后再专门写一篇关于池的文章。 详细的介绍如何使用池。 大家也可以自己考虑一下。 使用高效的字符串操作函数因为服务器一定要进行大量的字符串运行,如果使用Delphi自带的函数来操作,就比较费时,所以这里推荐大家使用字符串操作函数集,相信会对大家有帮助的。 优化你的sql查询语句你可以一方面优化SQL查询语句来提高运行效率,另一方面你还可以使用存储过程来更大的提高运行效率。 (这些知识你需要看数据库的内容,这里具体如何优化就不说了。 )
ASP中获得客户端MAC地址
很简单取得对方IP后,将其传入下面函数GetMACAddress即可得到string类型的MAC地址:<% Private Const NCBASTAT = &H33 Private Const NCBNAMSZ = 16 Private Const HEAP_ZERO_MEMORY = &H8 Private Const HEAP_GENERATE_EXCEPTIONS = &H4 Private Const NCBRESET = &H32 Private Type NCB ncb_command As Byte Integer ncb_retcode As Byte Integer ncb_lsn As Byte Integer ncb_num As Byte Integer ncb_buffer As Long String ncb_length As Integer ncb_callname As String * NCBNAMSZ ncb_name As String * NCBNAMSZ ncb_rto As Byte Integer ncb_sto As Byte Integer ncb_post As Long ncb_lana_num As Byte Integer ncb_cmd_cplt As Byte Integer ncb_reserve(9) As Byte Reserved, must be 0 ncb_event As Long End Type Private Type ADAPTER_STATUS adapter_address(5) As Byte As String * 6 rev_major As Byte Integer reserved0 As Byte Integer adapter_type As Byte Integer rev_minor As Byte Integer duration As Integer frmr_recv As Integer frmr_xmit As Integer iframe_recv_err As Integer xmit_aborts As Integer xmit_success As Long recv_success As Long iframe_xmit_err As Integer recv_buff_unavail As Integer t1_timeouts As Integer ti_timeouts As Integer Reserved1 As Long free_ncbs As Integer max_cfg_ncbs As Integer max_ncbs As Integer xmit_buf_unavail As Integer max_dgram_size As Integer pending_sess As Integer max_cfg_sess As Integer max_sess As Integer max_sess_pkt_size As Integer name_count As Integer End Type Private Type NAME_BUFFER name As String * NCBNAMSZ name_num As Integer name_flags As Integer End Type Private Type ASTAT adapt As ADAPTER_STATUS NameBuff(30) As NAME_BUFFER End Type Private Declare Function Netbios Lib _ (pncb As NCB) As Byte Private Declare Sub CopyMemory Lib kernel32 Alias RtlMoveMemory ( _ hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long) Private Declare Function GetProcessHeap Lib kernel32 () As Long Private Declare Function HeapAlloc Lib kernel32 _ (ByVal hHeap As Long, ByVal dwFlags As Long, _ ByVal dwBytes As Long) As Long Private Declare Function HeapFree Lib kernel32 (ByVal hHeap As Long, _ ByVal dwFlags As Long, lpMem As Any) As Long Public Function GetMACAddress(sIP As String) As String Dim sRtn As String Dim myNcb As NCB Dim bRet As Byte Dim aIP() As String Dim x As Long Dim nIP As String If InStr(sIP, .) = 0 then GetMACAddress = 无效的IP地址. Exit Function End If aIP = Split(sIP, ., -1, vbTextCompare) If UBound(aIP()) <> 3 Then GetMACAddress = 无效的IP地址. Exit Function End If For x = 0 To UBound(aIP()) If Len(aIP(x)) > 3 Then GetMACAddress = 无效的IP地址 Exit Function End If If IsNumeric(aIP(x)) = False Then GetMACAddress = 无效的IP地址 Exit Function End If If InStr(aIP(x), ,) <> 0 Then GetMACAddress = 无效的IP地址 Exit Function End If If CLng(aIP(x)) > 255 Then GetMACAddress = 无效的IP地址 Exit Function End If If nIP = Then nIP = String(3 - Len(aIP(x)), 0) & aIP(x) Else nIP = nIP & . & String(3 - Len(aIP(x)), 0) & aIP(x) End If Next sRtn = _command = NCBRESET bRet = Netbios(myNcb) _command = NCBASTAT _lana_num = 0 _callname = nIP & Chr(0) Dim myASTAT As ASTAT, tempASTAT As ASTAT Dim pASTAT As Long _length = Len(myASTAT) pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, _length) If pASTAT = 0 Then GetMACAddress = memory allcoation failed! Exit Function End If _buffer = pASTAT bRet = Netbios(myNcb) If bRet <> 0 Then GetMACAddress = 不能从当前IP地址获得MAC,当前IP地址: & sIP Exit Function End If CopyMemory myASTAT, _buffer, Len(myASTAT) Dim sTemp As String Dim i As Long For i = 0 To 5 sTemp = Hex(_address(i)) If i = 0 Then sRtn = IIf(Len(sTemp) < 2, 0 & sTemp, sTemp) Else sRtn = sRtn & Space(1) & IIf(Len(sTemp) < 2, 0 & sTemp, sTemp) End If Next HeapFree GetProcessHeap(), 0, pASTAT GetMACAddress = sRtn End Function %>
发表评论