Aug 12th, 2005, 03:23 AM
#1
Thread Starter
Hyperactive Member
Socket Issue[Resolved]
VB Code:
Function IsServerOnline() As Long
Dim MySocket As Long
MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
Dim server As String
server = "66.66.66.66" ' example ip
[b]If (Connect(MySocket, server, Len(server))) = SOCKET_ERROR Then[/b]
MsgBox "Cannot Connect to Server", vbCritical, "Error"
Unload Me
End
IsServerOnline = 0
Else
MsgBox "Connected", vbInformation
End If
End Function
It errors "Type mismatch" on the bold line on "server"
What am I doing wrong? (yes I have the API's declared)
Last edited by Tantrum3k; Aug 16th, 2005 at 11:10 PM .
Aug 12th, 2005, 02:51 PM
#2
Thread Starter
Hyperactive Member
Aug 12th, 2005, 02:53 PM
#3
Re: Socket Issue
Post your declarations. That isn't the way to connect, unless I'm missing something.
Aug 12th, 2005, 02:56 PM
#4
Re: Socket Issue
Originally Posted by
dglienna
Post your declarations. That isn't the way to connect, unless I'm missing something.
He's using the API sockets... (but I have no clue how to use them)
Why not using the Winsock control ?
Aug 12th, 2005, 03:03 PM
#5
Thread Starter
Hyperactive Member
Re: Socket Issue
VB Code:
Public Declare Function Connect Lib "wsock32.dll" Alias "connect" (ByVal s As Long, addr As sockaddr, ByVal namelen As Long) As Long
I can't use the Winsock Control because my project is DLL. It errors "Out of Memory" in DLL. Thus the reason I use send/recv/connect/accept via the API's
Sorry it doesn't really error type mismatch but it errors "ByRef argument type mismatch".
Still haven't figured out the problem...
Last edited by Tantrum3k; Aug 12th, 2005 at 03:06 PM .
Aug 12th, 2005, 03:09 PM
#6
Re: Socket Issue
Right now you have:
VB Code:
Dim server As String
server = "66.66.66.66" ' example ip
server variable is a string, it has to be sockaddr type, as per your Connect declaration, that's why you get a "Type mismatch" error
Aug 12th, 2005, 03:12 PM
#7
Thread Starter
Hyperactive Member
Re: Socket Issue
VB Code:
Dim server As sockaddr
[b]server = "66.66.66.66"[/b]
Now it errors "Type mismatch" on the bold line.
Aug 12th, 2005, 03:13 PM
#8
Re: Socket Issue
Originally Posted by
Tantrum3k
I can't use the Winsock Control because my project is DLL. It errors "Out of Memory" in DLL. Thus the reason I use send/recv/connect/accept via the API's
I'm pretty sure it should work. You have to make an invisible form in your DLL project, and add the Winsock control on it. Then in the DLL, make a new instance of the form, and then you can use the Winsock control. I think I did it like this before, and it worked...
Aug 12th, 2005, 03:14 PM
#9
Re: Socket Issue
Originally Posted by
Tantrum3k
VB Code:
Dim server As sockaddr
[b]server = "66.66.66.66"[/b]
Now it errors "Type mismatch" on the bold line.
Well, of course sockaddr is not a string...
sockaddr is a Type, see what parameters sockaddr type takes... look in the MSDN how to use the sockaddr type...
Aug 12th, 2005, 03:23 PM
#10
Re: Socket Issue
Here, I did the research for you: MSDN - SOCKADDR
VB Code:
Public Type SOCKADDR
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero As String * 8
End Type
You have to convert your "66.66.66.66" address to a long, and pass it to sockaddr.sin_addr
I think there's an API function that converts the string address to long, but I don't remember which one.
But, since you know so little API programming, I REALLY suggest to you that you should find a way to get Winsock to work in your DLL, because you will have MUCH more head-ake (hmm... not sure how to spell that) if you continue on the API path.
Aug 12th, 2005, 03:33 PM
#11
Thread Starter
Hyperactive Member
Re: Socket Issue
I'll check that out.
Thanks.
I'll probably be back
Aug 12th, 2005, 03:41 PM
#12
Re: Socket Issue
Regarding to the post you edited
sockaddr.sin_addr = 66
sockaddr.sin_addr = 66 ' this just overides the previous 66, therefore you end up with 66 in the end, not "66.66.66.66"...
I did not test it, but I think this is the API that converts from string address to a Long
VB Code:
Public Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long
EDIT , Here's the MSDN help for it MSDN - inet_addr
You have to use it like:
VB Code:
Dim server As sockaddr
server.sin_addr = inet_addr("66.66.66.66")
If (Connect(MySocket, server, Len(server))) = SOCKET_ERROR Then
'... the rest of the code
Aug 12th, 2005, 04:02 PM
#13
Thread Starter
Hyperactive Member
Re: Socket Issue
Also, in regards to the comment you made about puting the winsock control on a form hidden in your dll. I just did that it works fine without errors but when I try calling it in a Module like:
VB Code:
Dim ip As String
ip = Form1.Winsock1.LocalIP
MsgBox ip
It just exits the DLL. Closes it immediately without reason.
So I take it it's not going to happen, eh?
EDIT:
VB Code:
Dim server As sockaddr
server.sin_addr = inet_addr("66.66.66.66")
If (Connect(MySocket, server, Len(server))) = SOCKET_ERROR Then
Do you know how I can somehow put a port in there too for it to connect to also?
Last edited by Tantrum3k; Aug 12th, 2005 at 04:08 PM .
Aug 12th, 2005, 04:17 PM
#14
Re: Socket Issue
Originally Posted by
Tantrum3k
VB Code:
Dim ip As String
ip = Form1.Winsock1.LocalIP
MsgBox ip
Try without displaying the ip in the message box... a DLL is not supposed to have an interface, than's why I told you to make the form.Visible = False
Originally Posted by
Tantrum3k
Do you know how I can somehow put a port in there too for it to connect to also?
VB Code:
Dim server As sockaddr
server.sin_port = 80 ' or some other port...
Aug 12th, 2005, 04:29 PM
#15
Re: Socket Issue
you guys are too fast for me
VB Code:
Option Explicit
Private Type in_addr
S_un_b As Long
S_un_w As Long
S_addr As Long
End Type
Private Type sockaddr_in
sin_family As Integer 'Internet address family.
sin_port As Integer 'port number associated of the server
sin_addr As in_addr 'remote IP address of the server
sin_zero As String * 8
End Type
Private Declare Function Connect Lib "wsock32.dll" Alias "connect" ( _
ByVal s As Long, _
addr As sockaddr_in, _
ByVal namelen As Long _
) As Long
Private Const AF_INET = 2
Private Const SOCKET_ERROR = -1&
Private Declare Function inet_addr Lib "wsock32.dll" (ByVal ipAddres As String) As Long
Private Sub Command1_Click()
Debug.Print inet_addr("127.0.0.1")
Dim clientService As sockaddr_in
Dim MySocket As Long
clientService.sin_family = AF_INET
clientService.sin_addr.S_addr = inet_addr("127.0.0.1")
clientService.sin_port = [hl]htons(4500)[/hl]
If Connect(MySocket, clientService, LenB(clientService)) = SOCKET_ERROR Then
MsgBox "Cannot Connect to Server", vbCritical, "Error"
Unload Me
Else
MsgBox "Connected", vbInformation
End If
End Sub
Last edited by moeur; Aug 14th, 2005 at 09:40 PM .
Aug 13th, 2005, 04:18 AM
#16
Thread Starter
Hyperactive Member
Re: Socket Issue
I get this error now:
VB Code:
Dim listensock As sockaddr_in
Dim rc As Long
rc = listen([b]listensock[/b], 1)
Errors "Type mismatch" on the bold.
Aug 13th, 2005, 09:53 AM
#17
Re: Socket Issue
show me how you have declared everything so I can see what you've done wrong.
The listen function takes two longs.
Last edited by moeur; Aug 13th, 2005 at 09:58 AM .
Aug 13th, 2005, 01:57 PM
#18
Thread Starter
Hyperactive Member
Re: Socket Issue
Attached Files
Aug 13th, 2005, 03:49 PM
#19
Re: Socket Issue
SInce you have properly declared the listen function as having two long parameters, why did you try to pass it a parameter of type sockaddr_in?
Try something more like this
VB Code:
Sub main()
'Initialize Winsock
Dim mywsaData As WSAData
iResult = WSAStartup(MAKEWORD(2, 2), mywsaData)
If iResult <> NO_ERROR Then
MsgBox "Error at WSAStartup()"
Exit Sub
End If
'Create a SOCKET for listening for
'incoming connection requests.
Dim ListenSocket As Long
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
If ListenSocket = INVALID_SOCKET Then
MsgBox "Error at socket(): " & CStr(WSAGetLastError())
WSACleanup
Exit Sub
End If
'The sockaddr_in structure specifies the address family,
'IP address, and port for the socket that is being bound.
Dim service As sockaddr_in
service.sin_family = AF_INET
service.sin_addr.s_addr = inet_addr("127.0.0.1")
service.sin_port = [hl]htons(4500)[/hl]
If bind(ListenSocket, service, LenB(service)) = SOCKET_ERROR Then
MsgBox "bind() failed."
Call closesocket(ListenSocket)
Exit Sub
End If
'Listen for incoming connection requests
'on the created socket
If listen(ListenSocket, 1) = SOCKET_ERROR Then
MsgBox "Error listening on socket."
Else
MsgBox "Listening on socket..."
End If
WSACleanup
End Sub
Last edited by moeur; Aug 14th, 2005 at 09:39 PM .
Aug 13th, 2005, 06:13 PM
#20
Thread Starter
Hyperactive Member
Re: Socket Issue
VB Code:
Dim service As sockaddr_in
service.sin_family = AF_INET
service.sin_addr.S_addr = inet_addr("66.66.66.66") 'example ip of the host computer
service.sin_port = 4500
If bind(ListenSocket, service, LenB(service)) = SOCKET_ERROR Then
MsgBox "bind() failed"
Call closesocket(ListenSocket)
Exit Sub
End If
It's erroring "bind() failed".
I used ShowErrorMessage(Err.LastDllError) and it says "Address already in use". I don't see how it can be since I have it set to my IP with port 4500.
Aug 13th, 2005, 06:21 PM
#21
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
Aug 13th, 2005, 06:44 PM
#22
Thread Starter
Hyperactive Member
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".
Aug 13th, 2005, 06:58 PM
#23
Re: Socket Issue [Help Needed]
what is ListenSocket equal to?
Aug 13th, 2005, 07:01 PM
#24
Thread Starter
Hyperactive Member
Re: Socket Issue [Help Needed]
I msgBox'd "ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)" and it said it's 148
Aug 13th, 2005, 07:22 PM
#25
Re: Socket Issue [Help Needed]
Try this project as is. Does it give an error?
Last edited by moeur; Aug 15th, 2005 at 01:36 AM .
Aug 13th, 2005, 09:46 PM
#26
Thread Starter
Hyperactive Member
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?
Aug 14th, 2005, 12:44 AM
#27
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
Aug 14th, 2005, 02:01 PM
#28
Thread Starter
Hyperactive Member
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
Last edited by Tantrum3k; Aug 14th, 2005 at 03:00 PM .
Aug 14th, 2005, 03:09 PM
#29
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
Attached Files
Aug 14th, 2005, 03:12 PM
#30
Thread Starter
Hyperactive Member
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
Anyone see how I could be doing this wrong?
Aug 14th, 2005, 03:36 PM
#31
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?
Aug 14th, 2005, 03:50 PM
#32
Thread Starter
Hyperactive Member
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.
Aug 14th, 2005, 03:53 PM
#33
Re: Socket Issue [Help Needed]
go back to the code I posted above
Aug 14th, 2005, 03:58 PM
#34
Thread Starter
Hyperactive Member
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?
Aug 14th, 2005, 04:15 PM
#35
Re: Socket Issue [Help Needed]
it is being looped; the accept socket loop
Aug 14th, 2005, 05:13 PM
#36
Thread Starter
Hyperactive Member
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?
Aug 14th, 2005, 06:02 PM
#37
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
Aug 14th, 2005, 06:35 PM
#38
Thread Starter
Hyperactive Member
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.
Aug 14th, 2005, 07:42 PM
#39
Re: Socket Issue [Help Needed]
Do you have permission to connect to the server? What server are you trying to connect to?
Aug 14th, 2005, 08:06 PM
#40
Thread Starter
Hyperactive Member
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
Last edited by Tantrum3k; Aug 14th, 2005 at 08:09 PM .
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width