Simple TCP server
2 posters
Page 1 of 1
Simple TCP server
since your pretty versed in server codeing I cant for the life of me find simple code for a simple back and forth server that just exchanges information for example sets of numbers like Example:Client one sends data to server which sends to client two data at clients two checks against values on clients two and sends data back client one
any any ideas would be greatly appreciated
any any ideas would be greatly appreciated
Alcyhall- Posts : 1
Join date : 2010-07-15
Re: Simple TCP server
A simple TCP client/server application such as that can easily be accomplished using the Winsock control in VB6. I have heard mixed opinions about Winsock, but from my experience it is very simple to pass data the way you are requesting using either separate applications for server and client, or bundled as one application.
The code below shows the two primary server-side functions. Create a new VB6 application, under the components menu add the Microsoft Winsock Control (I have version 6). Drag the winsock control from your tools to the form, in the properties pane set the "Index" property to 0 (this will allow multiple "copies" of the control to be loaded as users connect to the server).
I have defined "clntCNT" as a public integer and "blnRDY" as a public boolean variable.
"clntCNT" will track the number of clients that are connected and the index of the winsock socket being used.
"blnRDY" will be used if the server has conditions that must be met before accepting data from clients.
The code would still need a method of determining which clients are on which winsock index so the server could decide which winsock socket to send the data out, this can be done by referencing a listview control containing the client id and the index of the winsock socket that accepted that client.
If you need further assistance, or would like me to actually code a skeleton program that you can build upon don't hesitate to ask.
The code below shows the two primary server-side functions. Create a new VB6 application, under the components menu add the Microsoft Winsock Control (I have version 6). Drag the winsock control from your tools to the form, in the properties pane set the "Index" property to 0 (this will allow multiple "copies" of the control to be loaded as users connect to the server).
I have defined "clntCNT" as a public integer and "blnRDY" as a public boolean variable.
"clntCNT" will track the number of clients that are connected and the index of the winsock socket being used.
"blnRDY" will be used if the server has conditions that must be met before accepting data from clients.
- Code:
//This function is called upon a connection request to the server//
//This controls if a client should begin sending data or wait//
Private Sub sckMain_ConnectionRequest(Index As Integer, ByVal requestID As Long)
clntCNT = clntCNT + 1 //Increase the client count by 1//
If blnRDY = True Then //If the server is ready for data//
Load sckMain(clntCNT) //Create a new copy of the socket to be used//
sckMain(clntCNT).Accept requestID //Accept the client request//
sckMain(0).Close //Close the initial socket controlling requests//
sckMain(0).Listen //Open the initial socket for new connections//
sckMain(clntCNT).SendData "READY" //Inform the server is ready//
ElseIf blnRDY = False Then //If the server is not ready for data//
Load sckMain(clntCNT)
sckMain(clntCNT).Accept requestID
sckMain(0).Close
sckMain(0).Listen
sckMain(clntCNT).SendData "NOT READY" //Inform the server isn't ready//
sckMain(clntCNT).Close //Close the socket, no longer accept data//
Unload sckMain(clntCNT) //Remove the socket, it is no longer used//
clntCNT = clntCNT - 1 //Decrease the client count by 1//
End If
End Sub
//This function will be called after a client has been accepted//
//Add "If..Then" or "Select Case" code here to determine//
//what the incoming data represents and decide where it should go//
//CLIENT1 has sent the data "MSG|CLIENT2|1"//
//CLIENT2 will receive the data after the server checks it//
//The data "MSG|CLIENT2|HI" has three parts the server will check//
//These parts are divided by "|" which will be my delimiter//
//The first part, "MSG" will tell the server the data is a message//
//The second part, "CLIENT2" is who to send the message to//
//The third part, "HI" is the actual message to be sent to the recipient//
Private Sub sckMain_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim data$ //Define a variable to be used to assign incoming data//
Dim arrD$() //Define an array to split the data into seperate parts//
sckMain(Index).GetData data$ //Assign the incoming data to "data$"//
If Left(data$, 3) = "MSG" Then //If the first 3 letters are "MSG"//
arrD$() = Split(data$, "|") //Divide data, place in an array//
//arrD$(1) is the recipient, arrD$(2) is the actual message//
ElseIf ...
//More code here, repeat while necessary//
End If
End Sub
The code would still need a method of determining which clients are on which winsock index so the server could decide which winsock socket to send the data out, this can be done by referencing a listview control containing the client id and the index of the winsock socket that accepted that client.
If you need further assistance, or would like me to actually code a skeleton program that you can build upon don't hesitate to ask.
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
2016-08-21, 6:03 am by Khristos
» Chat with the Admin
2016-08-21, 3:14 am by Khristos
» DDChr - Updates
2016-07-29, 5:25 pm by Khristos
» DDChr - Suggestions
2015-11-20, 8:05 am by Khristos
» DDChr - Issues
2015-11-20, 8:04 am by Khristos
» New NPC List
2014-11-30, 5:45 pm by Khristos
» Eve-Pricer Now v1.6.1 and Going Mobile!
2013-08-09, 3:54 pm by Khristos
» Eve-Pricer - Issues
2013-08-09, 3:04 pm by Khristos
» VMS is now on Facebook!
2012-10-02, 4:11 pm by Khristos