VampyreMan Studios - Project Boards
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
 
 

Display results as :
 


Rechercher Advanced Search

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
Latest topics
» DDChr - ReadMe
Simple TCP server Empty2016-08-21, 6:03 am by Khristos

» Chat with the Admin
Simple TCP server Empty2016-08-21, 3:14 am by Khristos

» DDChr - Updates
Simple TCP server Empty2016-07-29, 5:25 pm by Khristos

» DDChr - Suggestions
Simple TCP server Empty2015-11-20, 8:05 am by Khristos

» DDChr - Issues
Simple TCP server Empty2015-11-20, 8:04 am by Khristos

» New NPC List
Simple TCP server Empty2014-11-30, 5:45 pm by Khristos

» Eve-Pricer Now v1.6.1 and Going Mobile!
Simple TCP server Empty2013-08-09, 3:54 pm by Khristos

» Eve-Pricer - Issues
Simple TCP server Empty2013-08-09, 3:04 pm by Khristos

» VMS is now on Facebook!
Simple TCP server Empty2012-10-02, 4:11 pm by Khristos

April 2024
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
2930     

Calendar Calendar


Simple TCP server

2 posters

Go down

Simple TCP server Empty Simple TCP server

Post  Alcyhall 2010-07-15, 8:28 pm

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

Alcyhall

Posts : 1
Join date : 2010-07-15

Back to top Go down

Simple TCP server Empty Re: Simple TCP server

Post  Khristos 2010-07-20, 9:07 pm

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.

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.

Khristos

Posts : 54
Join date : 2009-12-01
Location : Camp Leatherneck, Afghanistan

http://www.vampyremanstudios.com

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum