Re: Socket Issue [Help Needed]
use "127.0.0.1" as your local IP address
if you still get an error call WSAGetLastError to get the error
Re: Socket Issue [Help Needed]
I tried 127.0.0.1 too but it still errors.
I also tried WSAGetLastError and it just says "0".
Re: Socket Issue [Help Needed]
what is ListenSocket equal to?
Re: Socket Issue [Help Needed]
I msgBox'd "ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)" and it said it's 148
Re: Socket Issue [Help Needed]
Try this project as is. Does it give an error?
Re: Socket Issue [Help Needed]
That works. Hmm, weird.
Thanks.
Now to Accept connections... ;)
I tried this:
VB Code:
Dim lngRetValue As Long
Dim lngBufferSize As Long
lngBufferSize = LenB(service)
lngRetValue = accept(ListenSocket, service, lngBufferSize)
If lngRetValue = SOCKET_ERROR Then
MsgBox "Error accepting connection"
WSACleanup
Call closesocket(ListenSocket)
End If
But it puts the application in "Not responding" and i have to close it.
I have all this listen, accept, socket, winsock - stuff in a Timer set to 1 millisecond. Should I have it in a timer or? What would you suggest I do?
Re: Socket Issue [Help Needed]
Here is one way to do it in a loop. You could use timer, but set interval to something like 50 or 100
VB Code:
'Create a SOCKET for accepting incoming requests.
Dim AcceptSocket As Long
Debug.Print "Waiting for client to connect..."
'Accept the connection.
While True
AcceptSocket = SOCKET_ERROR
While AcceptSocket = SOCKET_ERROR
AcceptSocket = accept(ListenSocket, 0, 0)
DoEvents
Wend
MsgBox "Client connected."
ListenSocket = AcceptSocket
Wend
WSACleanup
Re: Socket Issue [Help Needed]
Looping it doesn't seem to work. It doesn't do anything. I'm most likely doing it wrong so here's the code:
VB Code:
Dim lResult As Long
Dim mywsaData As WSAData
Dim blah As String
While True
blah = lResult <> NO_ERROR
While lResult <> NO_ERROR
lResult = WSAStartup(&H202, mywsaData)
DoEvents
Wend
Wend
Dim ListenSocket As Long
While True
ListenSocket = SOCKET_ERROR
While ListenSocket = SOCKET_ERROR
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
DoEvents
Wend
Wend
Dim BindS As Long
Dim service As sockaddr_in
service.sin_family = AF_INET
service.sin_addr = inet_addr("127.0.0.1")
service.sin_port = 4500
While True
BindS = SOCKET_ERROR
While BindS = SOCKET_ERROR
BindS = bind(ListenSocket, service, LenB(service))
DoEvents
Wend
Wend
Dim ClientIp As sockaddr_in
Dim AcceptSocket As Long
Dim ClientIplen As Long
ClientIplen = Len(ClientIp)
While True
AcceptSocket = SOCKET_ERROR
While AcceptSocket = SOCKET_ERROR
AcceptSocket = accept(ListenSocket, ClientIp, ClientIplen)
DoEvents
Wend
ListenSocket = AcceptSocket
Wend
1 Attachment(s)
Re: Socket Issue [Help Needed]
Look at my code attached in the zip.
This is a DLL version of winsock. It's basically cSocket, with my own rapper around it.
It is used exactly like the winosck control is. Except you need to do:
VB Code:
Dim Woof As Socket
Set Woof = New Socket
'normal winsock code
Woka
Re: Socket Issue [Help Needed]
Wokawidget,
I tried using your Winsock dll code in my own DLL... it errored it saying "Failed to initialize 0x000000005" like that, someone told me that happened cuz winsock needs to create a window and it cant in a dll, or something, i woulda LOVED to use your winsock cuz its so easy and nice :D
Anyone see how I could be doing this wrong?
Re: Socket Issue [Help Needed]
first of all I wouldn't put the listen or bind functions in a loop
when you say it doesn't do anything, is it just waiting in the Accept loop?
Do you have a client trying to connect?
Re: Socket Issue [Help Needed]
I forgot to set it on Form_Load. But, now when I go to run the application it just hangs in processes and doesn't do anything. I even took bind and listen out of the loop but it still hangs and does nothing.
Re: Socket Issue [Help Needed]
go back to the code I posted above
Re: Socket Issue [Help Needed]
Your code works fine but I dont understand how the application will listen for incoming connections if it's not being looped. Ya know?
Re: Socket Issue [Help Needed]
it is being looped; the accept socket loop
Re: Socket Issue [Help Needed]
VB Code:
Dim MySocket As Long
Myocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
If MySocket = SOCKET_ERROR Then
MsgBox "error creating socket"
WSACleanup
End If
Dim clientService As sockaddr_in
clientService.sin_family = AF_INET
clientService.sin_addr.S_addr = inet_addr("66.66.66.66") 'connect to host ip
clientService.sin_port = 4500
If connect(MySocket, clientService, LenB(clientService)) = SOCKET_ERROR Then
MsgBox "Cannot Connect to Server", vbCritical, "Error"
LoginServer = 0
Else
MsgBox "Connected", vbInformation
End If
Even when the host is online it says "Cannot Connect to Server"....
.......
host listen function:
VB Code:
Dim ListenNow As Long
ListenNow = listen(ListenSocket, 1)
If ListenNow = SOCKET_ERROR Then
MsgBox "error in listen"
Call closesocket(ListenSocket)
WSACleanup
Else
MsgBox "listen done"
End If
accept function:
VB Code:
Dim CIp As sockaddr_in
Dim AcceptSocket As Long
Dim CIpLen As Long
CIpLen = Len(CIp)
While True
AcceptSocket = SOCKET_ERROR
While AcceptSocket = SOCKET_ERROR
AcceptSocket = accept(ListenSocket, CIp, CIpLen)
MsgBox "accept done"
DoEvents
Wend
ListenSocket = AcceptSocket
Wend
You see the problem?
Re: Socket Issue [Help Needed]
1. client. Are you sure you can connect to server? Did you do so using something like Winsock control as a test?
2. server: Don't put that msgbox in the accept loop
Re: Socket Issue [Help Needed]
I just tried connecting to the server via Winsock Control, still says Cannot connect even though the server is online.
Re: Socket Issue [Help Needed]
Do you have permission to connect to the server? What server are you trying to connect to?
Re: Socket Issue [Help Needed]
I'm trying to connect to my server made in vb and server uses winsock control to listen and accept .. but the client we're working on won't connect but another application that uses the winsock control to connect connects fine so it's something wrong with the code we're trying i think.
this is the connect code we talked about throughout this discussion:
VB Code:
Public Function LoginServer()
Dim GSSocket As Long
GSSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
If GSSocket = SOCKET_ERROR Then
MsgBox "error creating socket"
WSACleanup
End If
Dim server As sockaddr_in
server.sin_family = AF_INET
server.sin_addr.S_addr = inet_addr("66.66.66.66") ' servers ip
server.sin_port = 4500
If connect(GSSocket, server, LenB(server)) = SOCKET_ERROR Then
MsgBox "Cannot Connect to Server", vbCritical, "Error"
LoginServer = 0
Else
MsgBox "Connected", vbInformation
End If
End Function