| |||
| |||
| |||
| |||
| |||
| |||
| Writting the Client | |||
| The client is actually the exact same used in part 1 , since changes have been made (changed a bit one line ) you can read how to write it at part 1,Here is the Link | |||
| Writting the Server | ||
| The server in we want to create is much more complex than part 1, since this one will be able to handle multiple client , and be responsible to distribute data to clients. If you don't know how to create the simple server i suggest you read Part 1 first. | ||
| Ok, Here is the Server Form | ||
| Now before i get into it , i must first tell you some basic things about control arrays. Control arrays is a very nice feature of VB that allows you to have many controls and handle them just like you would use an array. Control can be anything ( labels, command buttons, picture boxes , winsock , etc) and you can create new controls at runtime, unload them , or use them by saying ControlName(ControlIndex) where ControlName is then name of your control eg. label1 , pic1 and so on , and ContolIndex is a number which represent the current control in the control array I made a simple control demonstration program to see and test how to use them You can download it from here Note : To create a control array, select a control , for instance a label , and copy it , then try to paste it back to the form. Visual Basic will then ask you "You already have a control named 'xxxxxx'. Do you want to create a control array?" and click yes | ||
| Ok now back to the winsock thing... The logic behind this is to create a server that will be listening on a port. When a connection is requested by a client , instead of assigning the connection to the listening socket , a new socket will be created , and the connection will be assigned to it , thus keeping the listening socket listening for requests to come... | ||
| In the project we will have a global variable SocketCounter that will count how many sockets we made and what will be the index of the new one... That isn't really necessary because you can always get control count using the controls array .count function ( see the control array example above ) | ||
| First the listening button code.... | ||
| ||
With the server we need to accept the request from the client before the connection is completed To do that we use the sock1_ConnectionRequest that is triggered when a client tries to connect on our host. the connection will be completed only if we accept the request. ( command is bellow with bold fonts ) But remember, we must create a NEW winsock control and assign the connection to it, otherwise it you assign the connection to the sock with index 0 (which is our listening sock ) then it will stop listening for connection so clients will not be able to connect to the server Bellow is the code that handles the connection request | ||
| ||
| Next we need to write the DataArrival event. What is important to remember for this, is that when a client send a text string to the server , it is not meant for the server, but all other clients that are currently connected. ( remember we are trying to create an IRC like chatroom ) So when server get some data from the client , it need to redistribute it to all connected clients (also informing them who send the text ) | ||
Ok here is the code for the dataarrival event | ||
| ||
| Ok! those are the most crucial parts, there is also theError and Close events but there is nothing special about them, you can see them in the example source | ||
| This tutorial was written by VirusFree. Thank you for reading it and please excuse my English For any problems or question please don't hesitate to post them in our forums and i ( or anyone else who can answer them ) will reply as soon as possible | ||
| Keywords : Visual Basic, winsock, winsock connections, vb, multiuser chat, irc, server, client, winsock tutorial , visual basic winsock tutorial. | ||
discuss this topic to forum
