API Winsok 32

Iniciado por rembolso, 29 Abril 2012, 05:29 AM

0 Miembros y 1 Visitante están viendo este tema.

rembolso

Hola como estoy enfermo no puedo hacer nada estoy en la  pc jaja, me puse a boludear con winsock y vi como 3 ejemplos de winsock32 ,  y decidí aprender mas de las apis y me puse a jugar  tengo este codigo, pero tengo problema en dejarlo statico en memoria para que llame a la funciones correspondientes (depaso dejamos el surce si lo termino con exito)
* se conecta con exito, y envia datos con exito , el problema es  hacer la funcion de descarga,

Código (vb) [Seleccionar]
Private Declare Function htons Lib "wsock32.dll" (ByVal hostshort As Long) As Integer
Private Declare Function Connect Lib "wsock32.dll" Alias "connect" (ByVal s As Long, addr As SOCKADDR, ByVal namelen As Long) As Long
Private Declare Function Send Lib "wsock32.dll" Alias "send" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal Flags As Long) As Long
Private Declare Function Socket Lib "wsock32.dll" Alias "socket" (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long
Private Declare Function DestroyWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVR As Long, lpWSAD As WSADataType) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function recv Lib "wsock32.dll" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal Flags As Long) As Long
Private Const SOCK_STREAM As Long = 1
Private Const AF_INET As Long = 2
Private Const IPPROTO_TCP As Long = 6
Private Called As Long
Private Callad As Long
Public SocketHandle As Long
Public Len_Bytes_Enviados As Long
Public Status As Long
Public m_ObjectHost As Object

Private Type SOCKADDR
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero As String * 8
End Type
Private Type WSADataType
wVersion As Integer
wHighVersion As Integer
szDescription As String * 257
szSystemStatus As String * 129
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Const GWL_WNDPROC As Long = (-4)

Public Function Form_Gethw(ObjectHost As Object)

Dim StartupData As WSADataType


   Set m_ObjectHost = ObjectHost

 
       If Not WSAStartup(&H101, StartupData) Then
           bIsInit = True
           hWin = CreateWindowEx(0&, "STATIC", "SOCKET_WINDOW", 0&, 0&, 0&, 0&, 0&, 0&, 0&, App.hInstance, ByVal 0&)
           PrevProc = SetWindowLong(hWin, GWL_WNDPROC, AddressOf Socket_Evento)
       Else
           bIsInit = False
       End If
End Function


Public Function WsConnect(ByVal Host As String, ByVal Port As Long) As Long
Dim s As Long, Sockin As SOCKADDR

Sockin.sin_family = 2
Sockin.sin_port = htons(Port)
Sockin.sin_addr = inet_addr(Host$)
s = Socket(2, 1, 6)
If Connect(s, Sockin, 16) <> 0 Then
MsgBox "error al conectar"
Exit Function
End If


SocketHandle = s
'Call Enviar0("HOLA COMO ANDAS ANDAS LAURITA")
End Function

Public Function Enviar0(Data As String)
Dim Temp0$, Data0() As Byte
Select Case VarType(Data)
Case 8209
Data0 = Data
Case 8
Data0 = StrConv(Data, vbFromUnicode)
Case Else
Data0 = CStr(Data)
Data0 = StrConv(Data, vbFromUnicode)
End Select
Len_Bytes_Enviados = Send(SocketHandle, Data0(0), Len(Data), 0)
End Function

Private Function Socket_Evento(ByVal hWnd As Long, ByVal uMsg As Long, ByVal SocketHandle As Long, ByVal Evento As Long) As Long

Select Case Evento
Case &H10& 'conectado
Call m_ObjectHost.Scoket0_Conectado

Case &H20& 'cerrado
Call m_ObjectHost.Scoket0_Cerrado
Case &H1& 'descarga
Dim DataTemp As String, Ret As Long, rBuff As String

Do
rBuff = String(1024, 0)
Ret = recv(SocketHandle, ByVal rBuff, Len(rBuff), 0)
If Ret > 0 Then DataTemp = DataTemp + Left$(rBuff, Ret)
Loop Until Ret <= 0
If LenB(DataTemp) > 0 Then
Call m_ObjectHost.Scoket0_Descarga(DataTemp)
End If
End Select

End Function
Public Sub terminate()
DestroyWindow Called
Call WSACleanup
End Sub

el problema esta ak, en Socket_Evento , no recibe la llamada de SetWindowLong, me podrian echar una manito

BlackZeroX

te faltan muchas cositas...

* El buffer de recivo/envio de datos te lo determina el S.O. se otiene de esta manera:

Te lo pongo todo en C... sorry aun asu lo puedes traducir muy facil mente.


    int uiSize = sizeof(unsigned);

    //  Obtenemos los buffers del Sistema Operativo, tanto de Envio como de Recepción de datos.
    if (::getsockopt(this->mySock, SOL_SOCKET, SO_RCVBUF, (char*)&this->uiBuffRcv, &uiSize) == SOCKET_ERROR ||
            ::getsockopt(this->mySock, SOL_SOCKET, SO_SNDBUF, (char*)&this->uiBuffSnd, &uiSize) == SOCKET_ERROR)
    {
        closesocket(this->mySock);
        //Error...
    }


* No envies de golpe TODOS los datos con send... manda lX cantidad segun el buffer determinado...

Un poquito mas de codigo... igual esa clase que publique no esta terminada (si pero no la libere)...

http://foro.elhacker.net/programacion_cc/problemas_con_sockets-t347735.0.html;msg1696091#msg1696091

Dulces Lunas!¡.
The Dark Shadow is my passion.