Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ok this must be obvious
#1
ok this must be obvious but i cant seem to be able to find whats wrong. ive tried to make a simple chat program where it will send a message over with winsock using packets. unfortunately it all works except it doesnt seem to be able to send or recieve the packets and display them properly.

i have to projects my server project and client project. and here are the forms as follow
Client:
frmMain

Server:
Form1

and heres the code for the client, since im not sure where it bug is ill just give all of it
frmMain Code
Code:
Private Sub cmdExit_Click()
    End
End Sub

Private Sub cmdSend_Click()
Dim Msg As String
Msg = txtOut.Text

Call GlobalMsg(Msg)
txtIn.Text = txtIn.Text & vbNewLine & Msg
txtOut.Text = ""

End Sub

Private Sub Form_Load()
Dim Host As Long
Dim ServerPort As Long
Dim filename As String
filename = App.Path & "\Setting" & ".ini"

Host = Val(GetVar(filename, "[General]", "HostIP"))
ServerPort = Val(GetVar(filename, "[General]", "Port"))


frmMain.sockMain.RemoteHost = Host
frmMain.sockMain.RemotePort = ServerPort
frmMain.sockMain.Connect
End Sub

Private Sub sockMain_DataArrival(ByVal bytesTotal As Long)
sockMain.GetData strData, vbString
Call HandleData(bytesTotal)
End Sub

modClientTCP
Code:
Sub GlobalMsg(ByVal Msg As String)
Dim Packet As String
Packet = "GlobalMsg" & SEP_CHAR & Msg & SEP_CHAR & END_CHAR

Call SendData(Packet)
End Sub
Sub SendData(ByVal Data As String)
    If IsConnected Then
        frmMain.sockMain.SendData Data
    End If
End Sub
Function IsConnected() As Boolean
    If frmMain.sockMain.State = sckConnected Then
        IsConnected = True
    Else
        IsConnected = False
    End If
End Function
Sub HandleData(ByVal Data As String)
Dim Msg As String
Dim Parse() As String

If Parse(0) = "GlobalMsg" Then
    Msg = Parse(1)
    Call UpdateText(Msg)
End If
End Sub
Sub UpdateText(ByVal Msg As String)
frmMain.txtIn.Text = frmMain.txtIn.Text & vbNewLine & Msg
End Sub

modDatabase
Code:
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpString As String, ByVal lpfilename As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpdefault As String, ByVal lpreturnedstring As String, ByVal nsize As Long, ByVal lpfilename As String) As Long

Function GetVar(File As String, Header As String, Var As String) As String
Dim sSpaces As String   ' Max string length
Dim szReturn As String  ' Return default value if not found
  
    szReturn = ""
  
    sSpaces = Space(5000)
  
    Call GetPrivateProfileString(Header, Var, szReturn, sSpaces, Len(sSpaces), File)
  
    GetVar = RTrim(sSpaces)
    GetVar = Left(GetVar, Len(GetVar) - 1)
End Function

Sub PutVar(File As String, Header As String, Var As String, Value As String)
    Call WritePrivateProfileString(Header, Var, Value, File)
End Sub

and of course my modTypes with
Code:
Public SEP_CHAR as string *1
Public END_CHAR as string * 1

the heres my server code:
Form1:
Code:
Private Sub cmdExit_Click()
    End
End Sub

Private Sub Form_Load()
Dim SettingFile As String
SettingFile = App.Path & "\Setting" & ".ini"

'  lets set our public variables that come from .ini files
MAX_PLAYERS = Val(GetVar(SettingFile, "[Maxes]", "MAX_PLAYERS"))
ServerPort = Val(GetVar(SettingFile, "[General]", "Port"))

' Now lets set up our server so it can start working
sockMain(0).Close
sockMain(0).LocalPort = ServerPort
sockMain(0).Listen


End Sub
Private Sub cmdSend_Click()
Dim Msg As String
Msg = txtOut.Text
    
    Call GlobalMsg(Msg)
    txtIn.Text = txtIn.Text & vbNewLine & Msg
    txtOut.Text = ""
End Sub

Private Sub sockMain_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Call AcceptConnection(Index, requestID)
End Sub

Private Sub sockMain_DataArrival(Index As Integer, ByVal bytesTotal As Long)
sockMain(Index).GetData strData, vbString
Call HandleData(Index, strData)
End Sub

modServerTCP
Code:
Sub GlobalMsg(ByVal Msg As String)
Dim Packet As String
Packet = "GlobalMsg" & SEP_CHAR & Msg & SEP_CHAR & END_CHAR

    Call SendDatatoAll(Packet)
End Sub
Sub SendDatatoAll(ByVal Data As String)
Dim i As Long

For i = 1 To MAX_PLAYERS
    Call SendDataTo(i, Data)
Next i
End Sub
Sub SendDataTo(ByVal Index As Long, ByVal Data As String)
    Form1.sockMain(Index).SendData Data
End Sub
Sub HandleData(ByVal Index As Long, ByVal Data As String)
Dim UserName As String
Dim Password As String
Dim Msg As String
Dim Parse() As String


If Parse(0) = "GlobalMsg" Then
    Msg = Parse(1)
    Call UpdateText(Msg)
    Call SendDatatoAll(Msg)
End If
End Sub

Sub UpdateText(ByVal Msg As String)
Form1.txtIn.Text = Form1.txtIn.Text & vbNewLine & Msg
End Sub
Function FindOpenPlayerSlot() As Long
Dim i As Long

    
    FindOpenPlayerSlot = 0
    
    For i = 1 To MAX_PLAYERS
        If Not IsConnected(i) Then
            FindOpenPlayerSlot = i
            Exit Function
        End If
    Next i
End Function
Sub AcceptConnection(ByVal Index As Integer, ByVal requestID As Long)
    If (Index = 0) Then
        i = FindOpenPlayerSlot
        
        If i  0 Then

            Form1.sockMain(i).Close
            Form1.sockMain(i).Accept requestID
        End If
    End If
End Sub
Function IsConnected(ByVal Index As Long) As Boolean
    If Form1.sockMain(Index).State = sckConnected Then
        IsConnected = True
    Else
        IsConnected = False
    End If
End Function

modDatabase
Code:
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpString As String, ByVal lpfilename As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpdefault As String, ByVal lpreturnedstring As String, ByVal nsize As Long, ByVal lpfilename As String) As Long

Function GetVar(File As String, Header As String, Var As String) As String
Dim sSpaces As String   ' Max string length
Dim szReturn As String  ' Return default value if not found
  
    szReturn = ""
  
    sSpaces = Space(5000)
  
    Call GetPrivateProfileString(Header, Var, szReturn, sSpaces, Len(sSpaces), File)
  
    GetVar = RTrim(sSpaces)
    GetVar = Left(GetVar, Len(GetVar) - 1)
End Function

Sub PutVar(File As String, Header As String, Var As String, Value As String)
    Call WritePrivateProfileString(Header, Var, Value, File)
End Sub

modTypes
Code:
Public SEP_CHAR As String * 1
Public END_CHAR As String * 1

Public MAX_PLAYERS As Long
Public ServerPort As Long

and thats it, im sry if the bug is completely obvious but i cant see it, im a complete newb at VB as you all have prolly figured out by now, but can you please help me
Reply
#2
Wait a sec.. By the looks of it, you are like imitating the mirage chatting system. To make a basic chat system, you really dont need all those packets, unless of course you have those shouts (broadcast, tell player, global, etc..) But anyway, if you are just gonna make a regular chat, why not just send the text over to the server and from server to client with the winsock function 'SendData'.

Code:
frmClient.Winsock.SendData

then in the server, have it recieve it with the GetData function.
Reply
#3
well i was goin to start with the rest of my engine after i got the chat working so i am using the packets, and im essentially imitating mirage largely because i dont know any other way to keep it all organized. and im not using the actually mirage source because theres so much code that i get lost, plus im not making a visual engine just a text based, like a MUD
Reply
#4
Well, first of all, there are ALOT of problems in the source. Before we should fix it, lets add what you dont have. In the HandleData sub, in both client and server, right under Dim Parse() As String, you need to add

Code:
Parse = Split(Data, SEP_CHAR)

That is important and needed to separate the data in your packets. What you need to do, is add me to msn or yahoo (what ever you have) and we can correct this code.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)