Important alert: (current site time 7/15/2013 6:26:00 PM EDT)
 

winzip icon

Sox (WinSock OCX)

Email
Submitted on: 6/7/2003 12:23:49 PM
By: Trevor Herselman 
Level: Advanced
User Rating: By 22 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 21578
author picture
(About the author)
 
     Sox is a project to make a fast, lightweight and easy to use WinSock server. The OCX is written in C/C++, but I've written 2 apps in VB that demonstrate the use of Sox. The first is a project that demonstrates most of the common features in a single application and the second, is a Client/Server chat program. You will notice the code is short, because it's simple to use and powerful enough to handle at least 7000 simultaneous connections with only 300K of memory, and should be easy to read and follow! Read the Readme.doc file inside for further instructions. Happy coding! (Update: I've added 1 new function (RemoteHostIP) and cleaned my Error reporting code, the OCX is now almost half the original size, at 28K)

 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/8/2003 8:43:13 AMDream

Excellent!
(If this comment was disrespectful, please report it.)

 
6/8/2003 11:43:28 AMThushan Fernando

awseome! 5 globes!
(If this comment was disrespectful, please report it.)

 
6/9/2003 12:24:02 AMJeff Khor

I try to connect flood and it produce a lot of error in the text box. It also just connect to until 278 connection and stop. It also tell that it is out of memory for every time I try to connect flood. I am using Windows 98 and what is happen. I would like to know what is happen as I am study on socket too.
(If this comment was disrespectful, please report it.)

 
6/9/2003 2:01:06 AMKnoton

I made a simple client myself with the MS WinSock Control, I connected 3000 clients (socks) with an interval of 20ms successfully, to me that is impressing :-) Keep on the good work, any chance of seeing updates ? :-)
(If this comment was disrespectful, please report it.)

 
6/9/2003 5:41:11 PMTrevor Herselman

To Jeff Khor: You have reached the WinSock implementation limits on Windows 98! It is not considered to be a WinSock server OS!

To Knoton: Updates will come! Any suggestions?
(If this comment was disrespectful, please report it.)

 
6/9/2003 11:50:40 PMJeff Khor

Yes, Thank you. You get my rate.
(If this comment was disrespectful, please report it.)

 
6/11/2003 2:19:06 AMKnoton

Hi Trevor :-)
It would be nice to be able to get IP and Hostname of each connected.
(If this comment was disrespectful, please report it.)

 
6/11/2003 6:13:54 AMGerhard Jordaan

Excellent!

This is a brilliant example of how to use an OCX control. I have been looking for a way to use my C++ code in VB progs, but as you said in your readme file - documentation is very scarse. Are there any documents/web site that you can recommned to learn more about OCX controls?

You might consider writing a small tutorial on how exactly you created the OCX from start to finish. I'm sure that I am not the only one who will learn a lot from it.

Anyway, brilliant coding. Ek stem vir jou vyf uit vyf ;-)
(If this comment was disrespectful, please report it.)

 
6/12/2003 3:10:39 PMTrevor Herselman

To Knoton: These features are on my list of additions! But I would love to sort out my existing problem before devoting too much time to them!

To Gerhard Jordaan: Baie dankie!
I'm afraid I never studied OCX's online, only trial & error and MSDN. I learn things faster by myself. Learning how to do the OCX was like making a 10,000 piece puzzle, little clues were scattered all over the place! I suppose I could write a tutorial but I'm not sure how many people would be interested (or would be able to follow my explanations). You should be able to work a lot of things out from this example, although it's not well commented! Creating this OCX has been one of my most challenging experiences.
(If this comment was disrespectful, please report it.)

 
6/13/2003 4:53:50 AM

Gook Work Trevor!!!
(If this comment was disrespectful, please report it.)

 
7/5/2003 3:13:03 AMKnoton

Hello Trevor
Waiting impatiently for updates :-)
Another feature that I would appreciate would be to not have the socks indexed, to have the socks identified by their sock descriptor. Also a closed sock would be great if it could be removed instead of just changing state. I believe this would improve the ease of implementing a protocol to the TCP-Server.
(If this comment was disrespectful, please report it.)

 
7/5/2003 3:15:16 AMKnoton

hm... My English s*cks, hope you understand what I mean :-)
(If this comment was disrespectful, please report it.)

 
7/10/2003 7:07:43 AMTrevor Herselman

Hi Knoton,
I'm not sure if you understand why the Sockets are indexed like that, the index number is their array number, it makes Sox a lot faster to index them by array number, because when you send the sox number to the control, it immediately knows where to find the Socket. If I changed it to refer to the socket number, every time you sent the socket number to the control, it would have to do a new search through the array to find the correct socket for additional information.

(If this comment was disrespectful, please report it.)

 
7/10/2003 7:08:01 AMTrevor Herselman

Additionally, a closed socket IS effectively removed from the array, making that position within the array available for new sockets. I didn't want to create a trully dynamic array which erases the closed socket and shrinks the array if a socket in the middle closed, for speed reasons.
Also, I AM currently working on my origional Sox design, as I've overcome that designs problems. The new design is similar to this one, except I'm using pure API calls to the DLL's instead of using the MFC CAsyncSocket class/object. This improves the speed and efficiency in the overall design and reduces memory requirements. The new design will also allow on the fly data compression and encryption and also allow binary (not just string) data to be sent.
Thank you for your suggestions and patience ;-)

(If this comment was disrespectful, please report it.)

 
7/10/2003 7:08:43 AMTrevor Herselman

PS:- The Socket descriptor can easily be queried!
(If this comment was disrespectful, please report it.)

 
7/10/2003 4:10:22 PM

Hi Trevor, I'm really newbie to VC++ and I don't know how to make a "workaround" for sending VB Byte Data... How could I do that?...

If it is possible... how can I write a method call "confirm" with 1 byte parameter???... I need to confirm to the client with a VB CByte(0) function call result... Again, Is this possible?

PS: The component is great... Thanx for Share It!
(If this comment was disrespectful, please report it.)

 
7/10/2003 4:12:31 PM

I would like to know why my name doesn't not appears in my post... but anyway, I'm the VC++ Newbie!!!

Jean Paul Mir
(If this comment was disrespectful, please report it.)

 
7/11/2003 2:09:22 AMKnoton

Hi Trevor :-)
I was refering to what in VB is called Collections, I think it is called Hash tables in C++, then you could use the original socket descriptor as the identifier in the collection / hash table.
(If this comment was disrespectful, please report it.)

 
7/11/2003 2:19:56 AMKnoton

I really need to learn C++ :-)
Since I started out trying to make a very heavy trafficated TCP-Server in VB I realized that, for the first time, VB was not going to do the job for me.
Your Sox really impressed me with its speed and I think it could be a great winsock fundation for making servers :-)
(If this comment was disrespectful, please report it.)

 
7/11/2003 5:41:58 AM

I hate to get in the middle, But you guys a are suggesting items that will change the structure of other's programs. Lets try to stay compatible with upgrades.

For example - why change the indexing method. A new connection comes it can be pluged in the middle of the array with out the neccesity of seeing it at the end. That could mean changing the socket number.

other issue is the event driven property. I would like to see it runs also On-Demand. Which means that only when i wanat, i go to read if there is data ready in the buffer.

and 2nd idea, allow to change or fix the receiving buffer size.

Thank you travor. It is a nice and reliable OCX. I do not know what problems your are talking about.

Yaacov

(If this comment was disrespectful, please report it.)

 
9/8/2003 8:07:51 PMShannara

I cant wait for an update :) Does this use I/O Completion ports? That would be faster (*?*), and quite a bit awesome for use w/ VB examples :) I found one on another site, but it's just too problematic.
(If this comment was disrespectful, please report it.)

 
9/9/2003 2:44:15 PMTrevor Herselman

Hi Shannara,
Unfortunately I haven't had much time to work on it, my job has taken priority. But I plan to improve it again, hopefully soon!
To answer your question, no it doesn't use IOCP and yes, it probably would benefit with the right design.
Have phun.
(If this comment was disrespectful, please report it.)

 
9/19/2003 4:35:45 PMShannara

Oki dok. However, do you have a patch or a mod to this code for it to return the IP and Hostname of any Sox passed to it? That is the only thing missing for using this control in a server environment. After all, gotta get some sort of security in there.
:)

(If this comment was disrespectful, please report it.)

 
9/24/2003 1:55:34 AM

I think an active-x dll with an implemented(events are slowish) event system and collection based sockets (socket("key").connect like the list view control) would be nicer than an ocx.. but this is cool too.
(If this comment was disrespectful, please report it.)

 
11/1/2003 3:53:18 PMShannara

Any updates to this entry yet? Really needing to be able to grab the IP address of the client that has connected...
(If this comment was disrespectful, please report it.)

 
11/2/2003 5:24:49 AM

[sorry for my double posting... don't know what happend]
Trevor: Great work! I found your OCX by searching an alternative MS winsock. I tested csocket class from vbip, but there was the limit in multiple channel building. Your OCX is very helpful, but now I'm stumbled over the "binary" limit. Is it true that I'm nit able to trabsfer binary data... only string data is possible? I only try to write a simple HTTP download. The download of html pages works fine, but how can I download gif's or something binary else? Not possible?
(If this comment was disrespectful, please report it.)

 
11/3/2003 3:00:47 PMTrevor Herselman

The problem with the Sox OCX and sending Binary Data at the moment is, Strings are NULL terminated, but Binary data contains NULLs. So as soon as it finds a NULL in the binary data stream, it thinks the "string" is complete. I still need to add a function to send and receive binary data.

I'm currently working on an ultra fast Sox DLL version in my spare time written in 100% 32bit Assember code using IOCP's, capable of handling over 50,000 simultaneous connections with an average CPU usage on a P4 of 30-60%. The new version will be the fastest, most scalable version of free IOCP WinSock software available. The overall design is taking a bit of time to implement because of extreme IOCP complexity, especially when written in Assembler. You will be able to use the new version as a normal DLL linked to ur EXE. It is fully multi-threaded and current tests are ... unbelievably impressive.
(If this comment was disrespectful, please report it.)

 
11/3/2003 3:01:04 PMTrevor Herselman

PS: It #$%@ easy to extend Sox to return IP addresses. I'll give it a shot in a day or two (Hope I remember what I did in this code!)
(If this comment was disrespectful, please report it.)

 
11/3/2003 7:13:51 PMShannara

Thanks on both accounts. Im just pretty clueless when it comes to C++ :) Im a pure VBer :) So im a dumbwitt :D

Anyways, looking foward to the new IOCP as well, really looking foward.. :)
(If this comment was disrespectful, please report it.)

 
11/5/2003 4:08:58 PMTrevor Herselman

I've uploaded a new version. You can now determine local and remote IP and Port for any Socket. Check my VB broadcasting chat server for an example. Let me know if it's ok!

I'm working on the Sending/Receiving binary data, but it will be VERY difficult because I can use only the Variant type and variant is fundamentally very difficult to use in C++! Also, it will require many changes to the other functionality of the control!

Regards
Trevor
(If this comment was disrespectful, please report it.)

 
11/6/2003 12:23:39 PMShannara

Cool, I think... have any of these been updated? The only server I found, was the VB sample you posted months ago: http://pscode.com/vb/scripts/ShowCode.asp?txtCodeId=46023&lngWId=1

Both have been updated?
(If this comment was disrespectful, please report it.)

 
11/9/2003 1:18:31 AMTrevor Herselman

I have now modified Sox to include Binary Data transmission. In fact, I had to re-code the DataArrival event to return Binary Data, this data is returned in a Byte Array (0..255) within a Variant. In order to convert this binary data to VB Unicode strings, StrConv() is used, examples of this are in both demo programs.
(If this comment was disrespectful, please report it.)

 
11/9/2003 1:28:39 AMTrevor Herselman

In all honesty, Sox was never meant to be used in professional development. It was to provide people with an alternate perspective to Socket development, to provide examples of rarely used but useful C++ & VB techniques including using C++ Variants, SafeArray's, CString Objects, MFC Sockets, C++ OCX Events etc.

However, due to the number of people posting feedback here and through personal messages, I decided to extend it's capabilities. Hope you still find it useful although the new changes make things a little less "newbie" friendly!
(If this comment was disrespectful, please report it.)

 
11/9/2003 1:35:43 AMTrevor Herselman

Sox now also includes a RecvSize property which sets the default memory allocated to Receive buffers. This is the buffer initially allocated when data arrives, the Default in Sox is 32K, VB WinSock OCX is about 16K. Set the DefaultRecvSize property on startup, new sockets inherit this size. An example of changing this would be for chat programs that send/receive small messages, the buffer in this case allocates many unused bytes. A benchmark of throughput on your server (using Sox) could provide insight into the best size for this property.
(If this comment was disrespectful, please report it.)

 
11/10/2003 3:12:07 PMShannara

Thanks for the update. I'm planning on updating my server tonight to make use of this & new features :) Thanks again.
(If this comment was disrespectful, please report it.)

 
11/10/2003 3:56:13 PMShannara

Well, I have read your post found @ http://board.win32asmcommunity.net/showthread.php?s=10e0169ccf62335acd21699b02a8cc28
(If this comment was disrespectful, please report it.)

 
12/3/2003 2:27:20 PM

Being a VB programmer... I dont see the OCX or a renamed OCX in the ZIP. Anybody want to send me one.. :)
(If this comment was disrespectful, please report it.)

 
12/28/2003 2:25:36 AM

I need OCX too

(If this comment was disrespectful, please report it.)

 
1/2/2004 12:13:30 PM

Good news guys, this also compiles fine on VC.NET, but with warnings.
(If this comment was disrespectful, please report it.)

 
5/22/2004 11:48:14 PM

where is the OC_?
(If this comment was disrespectful, please report it.)

 
5/26/2004 8:59:20 AMTrevor Herselman

The ocx has been removed by PSC. Actually, I 'wrongfully' added the file to the zip, to help VB programmers who don't know how to 'compile' C/C++ projects. It's really not that difficult! Double click the .dsw file, and press F7 (Build). The OCX will be 'created' and registered.
(If this comment was disrespectful, please report it.)

 
5/26/2004 9:05:06 AMTrevor Herselman

I'd love to hear from other/more developers using this control. If you wish to get in touch, asmfreak@hotmail.com

I wish I could upload 'compiled' code actually, I wanted to make a (Kick A55) WinSock assembler DLL, obviously most here wouldn't know how to compile it, but PSC won't accept the DLL and they have no assembler section. Sorry!
Hope this one is good enough though!

Regards
(If this comment was disrespectful, please report it.)

 
9/4/2004 12:58:14 PMShannara

Hmm, for odd reasons there is no ability to get IP of any client (sox) connected to the sox server.. maybe you uploaded the wrong version? ..

Still looking foward to seeing your ASM DLL :)
(If this comment was disrespectful, please report it.)

 
10/24/2004 5:50:32 PMShannara

Sorry, have to vote this as a 1, due to missing basic network functionality (no client ip ability), plus the project looks abandon, as the author no longer replies here nor to emails.
(If this comment was disrespectful, please report it.)

 
10/26/2004 6:20:55 AMTrevor Herselman

Even though getting the remote IP
address falls into basic network
functionality, there are many applications that don't need/use it!
Giving my code a 1 for this reason, is very insulting. Since you had the source, it took me less than 30 minutes to add the function! Not very nice of you! Giving me a 1 ... on my birthday! Bessides, this project hasn't been abandoned, it's simply been left in the dust!
(If this comment was disrespectful, please report it.)

 
11/16/2004 7:16:11 PMShannara

Hmm, Please see the VB "Sox" project.. There's been a few cross-posting :D
(If this comment was disrespectful, please report it.)

 
1/21/2005 11:23:45 PM

It would've been good except that it doesn't have a function for getting the headers returned after an HTTP request
(If this comment was disrespectful, please report it.)

 
2/1/2005 5:43:40 AM

have u posted yr new & fast ocx on forum? or when u will post it?
(If this comment was disrespectful, please report it.)

 
2/11/2005 6:27:39 PM

Is it possible to make this work on linux? Or doesn't linux support it?
(If this comment was disrespectful, please report it.)

 
3/9/2005 8:55:34 AM

Really nice work, but the chat program doesn't work with multiple connections. Or do the SOX ocx support this?
(If this comment was disrespectful, please report it.)

 
6/29/2005 10:03:50 AMHardStream Software Development

I don't really understand the C language (I know it's more powerfull than Visual Basic (the language I use) etc. but I don't know how to use C.
There is one thing I know: this code is cool! You've got my 5 globes :D
(If this comment was disrespectful, please report it.)

 
2/12/2007 3:10:10 AMJason Newland

well written, well documented code well done trevor 5 globes
(If this comment was disrespectful, please report it.)

 
12/14/2007 4:27:21 AMMitch Adamou

Excellent. I'm going to try to attempt to change this into a dynamic link library, for use in C/C++ (Since I find this very effective). It shouldn't be too hard luckily, considering you've done most of the work Trevor :D

I love this code, I use the VB equivalent very often (I'm a recent migrator to the C/C++ language, though I still work in VB since I have past projects that need to be completed)

So a definant 5 stars. I am also suprised at Shannara (since I've known him for a bit) .. but regardless, thank you very much for sharing this piece.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.