Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Request help with BASIC-Winsock programming
#11
No No I know that little bit of code itself doesn't do all that stuff I said, I just meant that's what "a program in general" does.

So after opening the socket, binding it, and setting it to listen...

that's where this bit of code comes in. I'm just trying to see if I understand how that little routine checks/accepts a connection.

It seems that it keeps trying to accept over and over again, failing constantly, until it doesn't fail one time, at which point the connection is accepted. So an alternative to that code would be:

Code:
Dim AcceptSocket as SOCKET

DO
AcceptSocket=accept(m_socket, null, null)
if AcceptSocket <> socket_error then exit do
LOOP

Same thing, right?
Reply
#12
yeah that looks right.
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#13
After several hundred milliena of trial-and-error experimentation, I finally got my server program to accept a connection and receive data from my client program. But it really took a lot of messing around.

Some notes: The different tutorials out there tell you to do things different ways, and usually only one of them is right, but not always the same one. For example, in Beej's tutorial it says in order to accept the connection request, go ahead and create a new socket variable, and accept the connection while taking the incoming info at a new, empty sockaddr_in.

For example, where oldsocket is your listening socket and SA is your sockaddrr_in that it's already bound to:

Code:
dim newsocket as socket  ' the socket you will accept on
dim SB as sockaddr_in      ' the new sockaddr_in you will place the
'                                          remote connector's info into

newsocket=accept(oldsocket, @sb,len (sb))

For me though, this did not work. I got socket errors constantly even when placing that stuff into the correct continuous loop for connection checking.

At the MSDN site, it tackles it slightly differently, telling you to use:

Code:
newsocket=accept(oldsocket, NULL, NULL)
oldsocket=newsocket

And THAT one worked. The null's were necessary, which maybe they aren't in C. With this though, I still don't know how to get the remote connector's info (like IP Address etc.) since the active connected socket refers to a sockaddr_in that was already bound to my local IP Address and Port.

Had Beej's example worked I would already have a new sockaddr_in (SB in my example code) which contained the remote connector's info. But with the null's, I'm stuck for the moment on getting remote info.

Does the sockaddr_in (SA) already bound to oldsocket get automatically loaded with the remote connector's info once the connection is made and transferred, or something?
Reply
#14
Several aeons later, after delving into and examining the Winsock.bi file itself for quite some time, I have the TCP stuff working relatively well in FreeBasic.

The syntax and format of the Winsock commands in FB, though, is dissimilar to any of the notation used in the various tutorials around the web or the guide at Microsoft/MSDN itself. It's unique and has to be learned pretty much by directly perusing the structures of the subs, functions and defs laid out in the header files. Or by asking devs for assistance.

For example the command FD_SET which the tutorials say to use in order to add file descriptors to a set, has been changed in FB to FD_SET_ (the extra underscore character on the end is the difference) to distinguish that command from the *data type* FD_SET, which is still valid under FB.

There are all sorts of changes like that which are perfectly sensible when explained, but which a programmer is not going to have any way of knowing before hand or intuitively figuring out. So keep that in mind if you're trying to get the TCP stuff to work for the first time.

I will try to post some source code examples of my working FB chat test program, once I have it done, for the benefit of any future readers trying to get a handle on it.
Reply
#15
Since you are learning how to use winsock, maybe you could write an article for QB Express on what you have learned. I'm sure that others could benefit from your experiance. I know I personally would like to see how it is done.
Reply
#16
Sure, I'd be happy to shed whatever light I can on the subject for anyone tackling BASIC TCP for the first time, as I have been. I'll include some source code from my recently completed test project, a two-way simultaneous direct client-to-client chat program, which may help serve as a useful reference. I'll have to organize and comment it a bit first though, so as to clearly seperate the bare-bones TCP essentials from the additional window dressing, otherwise it will be too easy for a reader to confuse the two and miss the important stuff.

I'll post an update here when I've added the cleaned-up source to the projects area...
Reply
#17
Quote:Sure, I'd be happy to shed whatever light I can on the subject for anyone tackling BASIC TCP for the first time, as I have been. I'll include some source code from my recently completed test project, a two-way simultaneous direct client-to-client chat program, which may help serve as a useful reference. I'll have to organize and comment it a bit first though, so as to clearly seperate the bare-bones TCP essentials from the additional window dressing, otherwise it will be too easy for a reader to confuse the two and miss the important stuff.

I'll post an update here when I've added the cleaned-up source to the projects area...

Definitely do that!
eep your eyes open and a monkey you'll see -- oh, everybody wants my cocoa crispies.
Reply
#18
I've now posted that program in the FB Projects area. It's entitled "FreeBASIC Winsock TCP Chat Program". Read the notes in the post as well for some important points.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)