الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

هل هناك خطأ في هذا الكود

مغلق
بدأه Unlimated destroye في 7 أغسطس 2006 · 1 رد · 552 مشاهدة · في لغة Ms Visual Basic 6 وما قبلها من إصدارات
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Form

Private Sub Form_Load()

Dim lRetVal As Long

Dim ErrorMsg As String

Dim WSAData As WSADataType

Dim Bind_The_first_socket As Long

Dim sockaddr_in As sockaddr_in

'

'start up winsock service

lRetVal = WSAStartup(&H202, WSAData)

If lRetVal = 0 Then MsgBox "Lib loaded and ready to use"

If lRetVal <> 0 Then

'

'

Select Case lRetVal

Case WSASYSNOTREADYs

ErrorMsg = "The underlying network subsystem is not " & _

"ready for network communication."

Case WSAVERNOTSUPPORTED

ErrorMsg = "The version of Windows Sockets API support " & _

"requested is not provided by this particular " & _

"Windows Sockets implementation."

Case WSAEINVAL

ErrorMsg = "The Windows Sockets version specified by the " & _

"application is not supported by this DLL."

Case lRetVal = 0

ErrorMsg = "loaded and ready to use"

End Select

'

MsgBox ErrorMsg

'

End If

If lRetVal = 0 Then

The_first_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

End If

If The_first_socket = -1 Then MsgBox "faild to setup The_first_socket" Else MsgBox "sucses seting up The_first_socket" & The_first_socket

'bind the The_first_socket

Bind_The_first_socket = bind(The_first_socket, sockaddr_in, Len(sockaddr_in))

sockaddr_in.sin_family = AF_INET

sockaddr_in.sin_port = htons(CLng(2000))

sockaddr_in.sin_addr = inet_addr("127.0.0.1")

If Bind_The_first_socket = -1 Then MsgBox "faild to bind The_first_socket" Else MsgBox "Succses Binding The_first_socket"

End Sub

Private Sub Form_Unload(Cancel As Integer)

Call WSACleanup 'unload the lib

Call closesocket(The_first_socket)

End Sub

______________________________________________

Module

'load the liberatery

Public Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVR As Long, lpWSAD As WSADataType) As Long

Public Const WSA_DescriptionSize = 257

Public Const WSA_SysStatusSize = 129

Public Type WSADataType 'info when lib is load

wVersion As Integer

wHighVersion As Integer

szDescription As String * WSA_DescriptionSize

szSystemStatus As String * WSA_SysStatusSize

iMaxSockets As Integer

iMaxUdpDg As Integer

lpVendorInfo As Long

End Type

'_________________________________________________________________

'unload the lib

Public Declare Function WSACleanup Lib "ws2_32.dll" () As Long

'___________________________________________________________

'Setup soket

Public Declare Function socket Lib "ws2_32.dll" (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long

Public Const AF_INET = 2 'UDP, TCP

Public Const IPPROTO_TCP = 6

Public Const SOCK_STREAM = 1

Public The_first_socket As Long

'_____________________________________________________________

'close soket

Public Declare Function closesocket Lib "ws2_32.dll" (ByVal S As Long) As Long

'____________________________________________________________

'Bind the Socket

Public Declare Function bind Lib "ws2_32.dll" (ByVal S As Long, ByRef Name As sockaddr_in, ByRef namelen As Long) As Long

Public Const INADDR_ANY = &H0

Public Type sockaddr_in

sin_family As Integer

sin_port As Integer

sin_addr As Long

sin_zero As String * 8

End Type

Public Declare Function htons Lib "ws2_32.dll" (ByVal hostshort As Long) As Integer

Public Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long

I believe that there is no god but Allah and Mohamed is Allah Messenger

#2

هل قمت باللعب في الكود ؟؟ .. أم أنك حصلت عليه هكذا ..

علي العموم رسالة

faild to bind The_first_socket

لابد وأن تظهر كل مرة تحاول فيها عمل Bind للسوكت..

وهذا لأنك إذا نظرت لهذه الشفرة في نهاية حدث Form_Load:

Bind_The_first_socket = bind(The_first_socket, sockaddr_in, Len(sockaddr_in))
sockaddr_in.sin_family = AF_INET
sockaddr_in.sin_port = htons(CLng(2000))
sockaddr_in.sin_addr = inet_addr("127.0.0.1")

أنت هنا تقوم بعمل bind للسوكت قبل ملء البنية sockaddr_in التي علي أساسها سيكون ال Socket Binding.

بالتالي كل ما ستفعله هو تأخير عملية ال Bind للسوكت إلي ما بعد ملء البنية sockaddr_in هكذا :

sockaddr_in.sin_family = AF_INET
sockaddr_in.sin_port = htons(CLng(2000))
sockaddr_in.sin_addr = inet_addr("127.0.0.1")

Bind_The_first_socket = bind(The_first_socket, sockaddr_in, Len(sockaddr_in))

هذا الموضوع مغلق.

مواضيع مشابهة