| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 680 人关注过本帖
标题:winsock套字节问题
只看楼主 加入收藏
yukunhu
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-11-14
收藏
 问题点数:0 回复次数:0 
winsock套字节问题
我本来想搜下,但是发现搜索引擎不能用.我写的东西如下.每次在我套字节初始化的时候返 回值都不是0,是10014,后面的错误代码是10039.我的ws2_32.dll的版本信息是5.1.2600.2180 (xpsp_sp2_rtm.040803-2158)Windows Socket 2.0 32-Bit DLL,搞了两天查了无数东西,都没有答 案.快疯了.我写这个只是测试下用winsock2.0写用ipx的通讯程序.怎样才能在在vb.net下初始化套字节成功?跪谢了,没次在这都没得到答 案,但是我还是怀一个热切心企盼热心的大哥帮忙.我用的visual studio 2003
Module Module1
Declare Function WSAStartup Lib "ws2_32.dll" _
(ByVal wVersionRequired As Long, ByVal lpWSAData As WSADATA) As Long
Declare Function WSACleanup Lib "ws2_32.dll" () As Long
Declare Function WSAGetLastError Lib "ws2_32.dll" () As Integer
Declare Function bind Lib "ws2_32" (ByVal sck As Long, ByVal name As sockaddr_ipx, ByVal namelen As Long) As Long
Declare Function socket Lib "ws2_32" (ByVal AddressFamily As Long, ByVal Type As Long, ByVal Protocol As Long) As Long
Declare Function WSARecvFrom Lib "ws2_32" (ByVal hSocket As Long, ByVal lpBuffers As Long, ByVal dwBufferCount As Long, ByVal lpNumberOfBytesRecvd As Long, ByVal lpFlags As Long, ByVal lpFrom As SOCKADDR, ByVal lpFromlen As Integer, ByVal lpOverlapped As WSAOVERLAPPED, ByVal lpCompletionROUTINE As Long) As Integer
Declare Function WSASendTo Lib "ws2_32" (ByVal hSocket As Long, ByVal lpBuffers As Long, ByVal dwBufferCount As Long, ByVal lpNumberOfBytesSent As Long, ByVal dwFlags As Long, ByVal lpTo As SOCKADDR, ByVal iToLen As Integer, ByVal lpOverlapped As WSAOVERLAPPED, ByVal lpCompletionROUTINE As Long) As Integer
Declare Function GetSockName Lib "ws2_32" Alias "GetSockNameA" (ByVal sck As Long, ByVal name As Long, ByVal namelen As Long) As Long
Declare Function listen Lib "ws2_32" (ByVal s As Long, ByVal backlog As Integer) As Integer
Declare Function sendto Lib "ws2_32" (ByVal s As Long, ByVal buf As String, ByVal BufLen As Integer, ByVal flags As Integer, ByVal sckto As sockaddr_ipx, ByVal tolen As Integer) As Integer
Declare Function recv Lib "ws2_32" (ByVal s As Long, ByVal buf As String, ByVal BufLen As Integer, ByVal flags As Integer) As Integer
Structure WSADATA
Public wversion As Integer
Public wHighVersion As Integer
Public szDescription As String '* WSA_DescriptionSize 'Description of winsock implementation
Public szSystemStatus As String '* WSA_SysStatusSize 'status or configuration information
Public iMaxSockets As Integer
Public iMaxUdpDg As Integer
Public lpszVendorInfo As Long
End Structure
Structure SOCKADDR
Public sa_family As Integer
Public sa_data As String
End Structure
Structure WSAOVERLAPPED
Public Internal As Long
Public InternalHigh As Long
Public Offset As Long
Public OffsetHigh As Long
Public hEvent As Long
End Structure
Structure sockaddr_ipx
Public sa_family As Long
Public sa_netnum As String
Public sa_nodenum As String
Public sa_socket As Long
End Structure
'adressefamiliy
Public Enum AddressFamily
AF_UNSPEC = 0 '/* unspecified */
'/*
' * Although AF_UNSPEC is defined for backwards compatibility , using
' * AF_UNSPEC for the "af" parameter when creating a socket is STRONGLY
' * DISCOURAGED. The interpretation of the "protocol" parameter
' * depends on the actual address family chosen. As environments grow
' * to include more and more address families that use overlapping
' * protocol values there is more and more chance of choosing an
' * undesired address family when AF_UNSPEC is used.
' */
AF_UNIX = 1 '/* local to host (pipes, portals) */
AF_INET = 2 '/* internetwork: UDP, TCP, etc. */
AF_IMPLINK = 3 '/* arpanet imp addresses */
AF_PUP = 4 '/* pup protocols: e.g. BSP */
AF_CHAOS = 5 '/* mit CHAOS protocols */
AF_NS = 6 '/* XEROX NS protocols */
AF_IPX = AF_NS '/* IPX protocols: IPX, SPX, etc. */
AF_ISO = 7 '/* ISO protocols */
AF_OSI = AF_ISO '/* OSI is ISO */
AF_ECMA = 8 '/* european computer manufacturers */
AF_DATAKIT = 9 '/* datakit protocols */
AF_CCITT = 10 '/* CCITT protocols, X.25 etc */
AF_SNA = 11 '/* IBM SNA */
AF_DECnet = 12 '/* DECnet */
AF_DLI = 13 '/* Direct data link interface */
AF_LAT = 14 '/* LAT */
AF_HYLINK = 15 '/* NSC Hyperchannel */
AF_APPLETALK = 16 '/* AppleTalk */
AF_NETBIOS = 17 '/* NetBios-style addresses */
AF_VOICEVIEW = 18 '/* VoiceView */
AF_FIREFOX = 19 '/* Protocols from Firefox */
AF_UNKNOWN1 = 20 '/* Somebody is using this! */
AF_BAN = 21 '/* Banyan */
AF_ATM = 22 '/* Native ATM Services */
AF_INET6 = 23 '/* Internetwork Version 6 */
AF_CLUSTER = 24 '/* Microsoft Wolfpack */
AF_12844 = 25 '/* IEEE 1284.4 WG AF */
AF_MAX = 26
End Enum
Public Enum SocketType
SOCK_STREAM = 1 ' /* stream socket */
SOCK_DGRAM = 2 ' /* datagram socket */
SOCK_RAW = 3 ' /* raw-protocol interface */
SOCK_RDM = 4 ' /* reliably-delivered message */
SOCK_SEQPACKET = 5 ' /* sequenced packet stream */
End Enum
Public Enum SocketProtocol
IPPROTO_IP = 0 '/* dummy for IP */
IPPROTO_ICMP = 1 '/* control message protocol */
IPPROTO_IGMP = 2 '/* internet group management protocol */
IPPROTO_GGP = 3 '/* gateway^2 (deprecated) */
IPPROTO_TCP = 6 '/* tcp */
IPPROTO_PUP = 12 '/* pup */
IPPROTO_UDP = 17 '/* user datagram protocol */
IPPROTO_IDP = 22 '/* xns idp */
IPPROTO_ND = 77 '/* UNOFFICIAL net disk proto */
IPPROTO_RAW = 255 '/* raw IP packet */
IPPROTO_MAX = 256
NSPROTO_IPX = 1000
NSPROTO_SPX = 1256
NSPROTO_SPXII = 1257
End Enum
Public Const SOCKET_ERROR = -1
Public Const WS_VERSION_REQD = &H101
Public Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Public Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD = 1
Public Const WSADescription_Len = 256
Public Const WSASYS_Status_Len = 128
Public Const FD_SETSIZE = 64
Public Function HIBYTE(ByVal wParam As Integer) As Integer
HIBYTE = (System.Math.Abs(wParam) \ &H100) And &HFF&
End Function

Public Function LOBYTE(ByVal wParam As Integer) As Integer
LOBYTE = System.Math.Abs(wParam) And &HFF&
End Function
End Module
Public Class Form1
Inherits System.Windows.Forms.Form
Dim ipxadr As sockaddr_ipx
Dim ipxadr1 As sockaddr_ipx
Public s1 As Long
Public lis As Integer
Public b1 As Long
Public re As Integer
Public info As String
Public s2 As Long
Public b2 As Long

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sendinhalt As Char
Dim sen As Integer
SocketsInitialize()
s1 = socket(AddressFamily.AF_IPX, SocketType.SOCK_DGRAM, SocketProtocol.NSPROTO_IPX)
ipxadr.sa_family = AddressFamily.AF_IPX
ipxadr.sa_socket = 2000
ipxadr.sa_netnum = "12345678"
ipxadr.sa_nodenum = "AAAAAAAAAAAA"

b1 = bind(s1, ipxadr, Len(ipxadr))
s2 = socket(AddressFamily.AF_IPX, SocketType.SOCK_DGRAM, SocketProtocol.NSPROTO_IPX)
ipxadr1.sa_family = AddressFamily.AF_IPX
ipxadr1.sa_netnum = "AAAAAAAAAAAA"
ipxadr1.sa_nodenum = "12345678"
ipxadr1.sa_socket = 2001
b2 = bind(s2, ipxadr1, Len(ipxadr1))
sendinhalt = TextBox1.Text
sen = sendto(s1, sendinhalt, Len(sendinhalt), 0, ipxadr1, Len(ipxadr))
If sen = -1 Then
MsgBox("Error ist:" & (WSAGetLastError))
End If
lis = listen(s2, 2)

If sen = -1 Then
re = recv(s2, info, Len(info), 0)
MsgBox(info)
End If
WSACleanup()

End Sub
Public Sub SocketsInitialize()
Dim WSAD As WSADATA
Dim iReturn As Integer
Dim sLowByte As String, sHighByte As String, sMsg As String

iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
MsgBox(WSAGetLastError())

If iReturn <> 0 Then
Exit Sub
End If

If LoByte(WSAD.wversion) < WS_VERSION_MAJOR Or _
(LoByte(WSAD.wversion) = WS_VERSION_MAJOR And _
HiByte(WSAD.wversion) < WS_VERSION_MINOR) Then

Exit Sub
End If

If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
Exit Sub
End If

End Sub
End Class
搜索更多相关主题的帖子: winsock 字节 
2007-07-11 18:06
快速回复:winsock套字节问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069163 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved