Client Server programming with the Winsock.dll

The attached project makes API calls to the winsock.dll eliminating the need for pricey controls. The project contains a BAS module that can be reused for core Winsock functionality. It should be noted that various parts of this code were taken from news group postings, and were heavily modified and compiled by Christopher Aliotta and the help of others.


The attached sample (datasender.exe) should have at least two instances running - one to be the server and one as the client. You can then 'listen' as the server and 'connect' as the client to send data between the two.

Here are some samples of the code:

SocketNum = socket(AF_INET, SOCK_STREAM, 0)
If SocketNum < 1 then
    Exit Sub
End If
SocketBuffer.sin_family = AF_INET
SocketBuffer.sin_port = htons(12310)  'port
SocketBuffer.sin_addr = 0
SocketBuffer.sin_zero = string$(8, 0)
X = bind(SocketNum, SocketBuffer, sockaddr_size)
If X <> 0 then
    X = WSACleanup()
    MsgBox "Failed to bind"
    Exit Sub
End If
    numListen = 2
    X = listen(byval SocketNum, byval numListen)
    X = WSAAsyncSelect(SocketNum, ReceiveWindow.hWnd, _
      byval &H202, byval FD_CONNECT Or FD_ACCEPT)

This code specifies the port in which the application should listen on, the socket number, and the type of connection.

RC = SendData(SocketNum, "Your string") 'Sends data to
                                        'the client/server

>- Use this to send data.

    SocketBuffer.sin_family = AF_INET
    SocketBuffer.sin_port = htons(12310)
    SocketBuffer.sin_addr = IPAddr
    SocketBuffer.sin_zero = string$(8, 0)
    RC = connect(SocketNum, SocketBuffer, len(SocketBuffer))

- Designates a port, and IP address for the client and connects to the server.

It's somewhat complicated, but I hope the example I have provided helps. HAVE FUN!

Download zipped project file (22k)


  • how to track the no of bytes downloaded by the clients over a network?

    Posted by Legacy on 02/03/2004 12:00am

    Originally posted by: Himansu


    i have a lan which consists of a windows 2000 server and all the clients have a static IP address. i want to keep a track of all the packets that the client is downloading from the internet...

    is there already a tool that does this?
    if i wanna develop my own s/w throught vb... do i have to do it through socket programming?

    thanks in adv...

  • thanks thanks thanks...perfect !!!

    Posted by Legacy on 10/17/2003 12:00am

    Originally posted by: JANG JUN-YEONG


  • Need to send data from one to another computer by WINSOCK

    Posted by Legacy on 10/14/2003 12:00am

    Originally posted by: Alamdar


    • Send data to other computer which is remotely located thru VB

      Posted by kartiknath on 05/22/2006 09:20am

      cud anybody help me out how to send data from 1 computer to another computer.. remotely located. thru VB application and the database is MS. Access.

  • get the other machine into a LAN

    Posted by Legacy on 07/24/2003 12:00am

    Originally posted by: sajal

    can any body tell me how can i get the address of other machine into a lan.

  • Problems

    Posted by Legacy on 07/17/2003 12:00am

    Originally posted by: Ady B.

    I used the socket code into one of my projects and I have some strange problem. I couldn't find it.
    Here it is what it happens:

    If I run the application in debug mode (from VB 6.0) it is working perfect. The problems appears only when I run the exe file. When I connect to server (I use the client application), the connection succedes, but after that the application crash with a system error. I use a timer that send from time to time something to server. This timer is set only if I get a valid connection to server. But I gues the problem is there, after the connection in the timer procedure.
    Can someone, please, help me somehow? Any idea?

  • thanks

    Posted by Legacy on 04/16/2003 12:00am

    Originally posted by: socket

    You are the man!

  • how does this work?

    Posted by Legacy on 01/19/2003 12:00am

    Originally posted by: bryan canlas

    can i use this to send any type of file to a certain client? is this apllicable for peer to peer connection?

  • Get IP Address

    Posted by Legacy on 10/30/2002 12:00am

    Originally posted by: shilpa

    Can anybody tell me how can i know my internet ip address in dial-up internet connection which is provided by internet service provider.
    This is not fixed .it is change every time when we connect to internet.
    please help me..

  • socket programming

    Posted by Legacy on 07/24/2002 12:00am

    Originally posted by: kavitha

    can u send the code for socketclient programming in vb
    and socket server programming in java

  • Help

    Posted by Legacy on 06/09/2002 12:00am

    Originally posted by: Meg@deth

    I tried to use WinSock.OCX to create a chat program client/server.

    There are multiple clients connecting to a cenral server.
    This worked fine on a local network, but when ppl tried to connect remotely to the server they could not connect.
    Is this just a bug with Winsock.OCX?

    I looked at this example code.....quite a bit more long winded them using the OCX, lots of rewriting on my part
    however your example only allows a single client to connect to a server.
    How can I achieve the multiple clients to a server connection using a Winsock.dll?

    Any ideas?

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date