Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: ntaryl on March 20, 2007, 02:05 PM

Title: Winsock api
Post by: ntaryl on March 20, 2007, 02:05 PM
Hi and Good evening 
Iam here again  and  need some Help
code a client server aplication and i try to use the socket api .
not want  the cSocketclass .
Please small example client server with (connect,close,listen,send.recv  )functions.
thanks for the time 

Title: Re: Winsock api
Post by: Mystical on March 20, 2007, 05:00 PM
uhm, so your asking for source,

well www.pscode.com is your friend in that case.
Title: Re: Winsock api
Post by: brew on March 20, 2007, 06:52 PM
Well, ntaryl, I don't know of any good winsock object tutorials, but you can just use this:

WS.State returns the state of the winsock's current connection. There are 10 possible states:

WinSock.State = sckClosed = 0
WinSock.State = sckOpen = 1
WinSock.State = sckListening = 2
WinSock.State = sckConnectionPending = 3
WinSock.State = sckResolvingHost = 4
WinSock.State = sckHostResolved = 5
WinSock.State = sckConnecting = 6
WinSock.State = sckConnected = 7
WinSock.State = sckClosing = 8
WinSock.State = sckError = 9

Self explanitory.

What you want to do to create an outbound tcp connection is....

WinSock.Close 'to rid it of any previous connection
WinSock.Connect (STRING) ip address, (INT) porthere

Private Sub WinSock_Connect()
WinSock.SendData "meow mix r00lz" '(in string format)
End Sub

Private Sub WinSock_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
WinSock.GetData Data
MsgBox Data

for a client side connection of some sort.
for a server.... it's like this:

Private Sub Form_Load()
WinSock.Close
WinSock.Listen
End Sub

Private Sub Winsock_ConnectionRequest(ByVal RequestID As Long)
Winsock.Close
Winsock.Accept
End Sub...... so on like that
Title: Re: Winsock api
Post by: Hell-Lord on March 20, 2007, 11:32 PM
Private Sub Winsock_ConnectionRequest(ByVal RequestID As Long)
Winsock.Close
Winsock.Accept
End Sub...... so on like that


You mean...

winsock.Accept RequestID

and

Private Sub WinSock_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
WinSock.GetData Data
MsgBox Data


do.....
Dim Data as String: Data = Empty
Title: Re: Winsock api
Post by: brew on March 21, 2007, 09:16 AM
Quote from: Hell-Lord on March 20, 2007, 11:32 PM
Private Sub Winsock_ConnectionRequest(ByVal RequestID As Long)
Winsock.Close
Winsock.Accept
End Sub...... so on like that


You mean...

winsock.Accept RequestID

and

Private Sub WinSock_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
WinSock.GetData Data
MsgBox Data


do.....
Dim Data as String: Data = Empty
Oops @ the winsock.accept
But why would I make my "Data = Empty"? That doesn't make sense, in vb6 all variables = 0 or otherwise null until given a set value. I can understand your reasoning if we were using C++....
Title: Re: Winsock api
Post by: l2k-Shadow on March 21, 2007, 02:22 PM
According to the topic, he was asking for winsock API, rather then the VB object. I'd suggest searching for CSocket on google.
Title: Re: Winsock api
Post by: Hell-Lord on March 21, 2007, 07:25 PM
http://www.brutalnet.net/support/index.php?action=tpmod;dl=item26

There is the CSocket :)

and @Brew i was told to do it that way without much reason but after the data has been set once how would it be cleared.

Title: Re: Winsock api
Post by: Mystical on March 21, 2007, 10:07 PM
maybe cuz he has it as DIM not STATIC?
Title: Re: Winsock api
Post by: Hell-Lord on March 22, 2007, 10:15 PM
O sorry you should use "= VbNullString" .
Title: Re: Winsock api
Post by: brew on March 23, 2007, 03:11 PM
Speaking of vbNullString, what's really better? "" or vbNullString? Ofcourse vbNullString would be the obvious choice, but what about ""? "" is 5 bytes more, but it's faster (I have acually proven this) I made this one quick program where it joins byte arrays with either "" or vbNullString, suprise! 50000 concatinations of "l" with vbNullString took .4655 seconds where "" took acually .001 second less. And there's the third option: Declare a public variable "NulString" or w/e and make it = vbNullString on Form_Load. Good idea? Because the value for vbNullString (it's a constant) would already been picked up and assigned to NulString it would be much quicker then the normal vbNullString, while only taking up maybe 2 more bytes for storage, and maybe 1 per use. (note: I didn't try this but it sounds like a good idea...)
Title: Re: Winsock api
Post by: Barabajagal on March 23, 2007, 03:19 PM
I don't think you're understanding something about vbNullString. It's an empty, null string. "" is not null. That's the difference. Yes, using "" within strings is faster, but when are you ever going to do that normally? you set vbNullString to a string variable to make it null, empty, and void of memory usage.
Title: Re: Winsock api
Post by: brew on March 23, 2007, 06:33 PM
Quote from: [RealityRipple] on March 23, 2007, 03:19 PM
I don't think you're understanding something about vbNullString. It's an empty, null string. "" is not null. That's the difference. Yes, using "" within strings is faster, but when are you ever going to do that normally? you set vbNullString to a string variable to make it null, empty, and void of memory usage.
A null string and empty string are different, for example, an empty string aka "" would take up 6 bytes in total: 4 for the length, and 2 for the null terminators. A nullstring is simply null, taking up zero bytes. Btw: I just realized that setting a variable = to vbNullString is the same as having a null string in the first place, except it doesn't have to keep processing it in the first place, and it doesn't make anything faster. *sigh*

"Yes, using "" within strings is faster, but when are you ever going to do that normally?"
Processor intensive programs?
Title: Re: Winsock api
Post by: Barabajagal on March 23, 2007, 11:01 PM
uhm... a null terminator is one byte... 00.
I asked when you're going to concatenate a string with "". The answer is never.
Title: Re: Winsock api
Post by: MyndFyre on March 23, 2007, 11:07 PM
Quote from: brew on March 23, 2007, 06:33 PM
A null string and empty string are different, for example, an empty string aka "" would take up 6 bytes in total: 4 for the length, and 2 for the null terminators.
I'm relatively certain (though not 100%) that Visual Basic 6 strings are not Unicode.
Title: Re: Winsock api
Post by: Barabajagal on March 23, 2007, 11:17 PM
They can be if you use StrConv on them, but otherwise, no, they're not unicode.
Title: Re: Winsock api
Post by: raylu on March 24, 2007, 01:13 AM
In many scripting languages with no way to specify variable types, often, you need:
somevar = 5
somevar += ""

to convert to a string.

Also, using "" would cause the string literal to be created and destroyed every one of those 50,000 times, wouldn't it?
Title: Re: Winsock api
Post by: l2k-Shadow on March 24, 2007, 11:53 AM
Quote from: [RealityRipple] on March 23, 2007, 11:17 PM
They can be if you use StrConv on them, but otherwise, no, they're not unicode.

Uhm.. VB6 strings are definitely stored in unicode, I've run into many problems when writing programs that use parts from different languages with VB6. But you can use this simple test too:


Sub Main()
Dim s As String
    s = "Hello"
    MsgBox LenB(s)
End Sub


Quote from: [RealityRipple] on February 05, 2007, 01:44 AM
Ok, your grammar is lacking a bit there... So I'll try to answer what I think you asked. Yes, I paid $25 for Visual Basic 6 certification. Yes, it's pretty much a worthless feature nowadays (PRETTY MUCH doesn't mean totally useless). It's still there because there are uses for it. The fact is, it's there in (i think) all BASIC languages in some form. It's a handy time saver if you use it right.

Guess that VB6 certification was kinda shitty.
Title: Re: Winsock api
Post by: brew on March 24, 2007, 02:21 PM
Quote from: MyndFyre[vL] on March 23, 2007, 11:07 PM
I'm relatively certain (though not 100%) that Visual Basic 6 strings are not Unicode.

They are. Try this code...

Private Sub Form_Load()
    MsgBox LenB("asdf")
End Sub


8? that must mean the characters must be two bytes each... Then the extra 6 for defining length and the scope of the string. What I'm trying to say is that vb6 strings are stored in unicode.

EDIT***
oops.... shadow already posted this *hits his head off the wall* i should've read before i posted

Quote from: raylu on March 24, 2007, 01:13 AM
Also, using "" would cause the string literal to be created and destroyed every one of those 50,000 times, wouldn't it?

This is correct, but acually it would be created and destroyed all 50,000 times. I'm unsure why, but it still seems to be faster then vbNullString even though using vbnullstring would indeed be the better choice. Perhaps a newer compiler for higher level languages might add some sort of catche for these type of operatons (of a commonly used string or value) and use that as a local variable instead.... just a guess
Title: Re: Winsock api
Post by: MyndFyre on March 24, 2007, 02:26 PM
Oh hey, what do you know (http://www.aivosto.com/vbtips/stringopt2.html#memorylayout)?