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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds