Hej jeg har virkelig brug for hjælp!
Jeg arbejder med et nyt server system med winsock.
Men for at nemt kunne sætte det ind i mine programmer har jeg beslutte at lave en .OSC kaldet CTFServer.
Det er en rimelig lang kode men jeg tror at det er bedst at jeg sætte det ind her!
Og tak til de gode artikler her på udvikleren der har hjulpet mig.
Her er usercontrol SERVER:
Public ServerName As String
Public Sub SetServerName(Name As String)
On Error Resume Next
ServerName = Name
With IconData
.cbSize = Len(IconData)
.hWnd = hWnd
.uID = 0
.uFlags = Tray_Message Or Tray_Icon Or Tray_Tip
.uCallbackMessage = Tray_MouseMove
.hIcon = PIC.Picture
.szTip = ServerName & " CTF Based Server at port:" & W.LocalPort & Chr$(0)
End With
Call Shell_NotifyIcon(Tray_Delete, IconData)
Call Shell_NotifyIcon(Tray_Tray_Add, IconData)
End Sub
Public Sub QuitServer()
On Error Resume Next
Call Shell_NotifyIcon(Tray_Delete, IconData)
Unload Me
End Sub
Public Sub CreateServer(Name As String)
SetServerName Name
W.Protocol = sckUDPProtocol
W.LocalPort = 1122
W.RemotePort = 1123
End Sub
Private Sub mnuStop_Click()
On Error Resume Next
For i = 0 To 31
W.RemoteHost = CTF.User(i).IP
W.SendData "Kicked|The Server have quit! By server|"
CTF.User(i).IP = ""
Next i
QuitServer
End Sub
Private Sub Timer1_Timer()
On Error Resume Next
Dim i As Integer
Dim ii As Integer
For i = 0 To 31
User(i).TIMEBACK = User(i).TIMEBACK - 1
Next i
For i = 0 To 31
If User(i).IP = "" Then: GoTo NE
If User(i).TIMEBACK <= 0 Then
User(i).IP = ""
For ii = 0 To 31
W.RemoteHost = CTF.User(i).IP
W.Bind
W.SendData "MSG|" & i & " has gone!|"
W.SendData "Away|" & i & "|"
User(i).TIMEBACK = 25
Next ii
End If
NE:
Next i
End Sub
Private Sub UserControl_Initialize()
W.Close
Timer1.Enabled = False
CreateServer "My"
End Sub
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
'For at få dette til at virke skal man sætte ens forms property, ScaleMode, til Pixel
Select Case X
Case Tray_RightButtonUp
PopupMenu mnuMenu
End Select
End Sub
Private Sub UserControl_Resize()
Width = 2385 - 400
Height = 510 - 50
End Sub
Private Sub UserControl_Terminate()
QuitServer
End Sub
Private Sub W_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Dim Data As String
W.Getdata Data
RTF.Text = RTF.Text + Data + vbNewLine
RTF.SaveFile "C:\\Log.rtf"
MsgBox Data
Dim DataI As String
DataI = FindDelAfTekst(Data, 1)
Getdata DataI, Data
End Sub
Public Sub Getdata(DataI As String, Data As String)
Dim i As Integer
Dim noUser As CTF.CTFSERVERTYPE
Select Case DataI
Case "AskForThere" ' paramentras:
' SendIN sender Ip
' SendOut "here",server ip,servername
W.RemoteHost = FindDelAfTekst(Data, 2)
W.Bind
W.SendData "Here|" & W.LocalIP & "|" & UserControl_ServerName & "|"
Case "SendToAll" ' paramentras:
' SendIN none
' SendOut "MSG",datas
For i = 0 To 31
W.RemoteHost = CTF.User(i).IP
W.Bind
W.SendData "MSG" & FindDelAfTekst(Data, 2) & "|"
Next i
User(FindDelAfTekst(Data, 2)).TIMEBACK = User(i).TIMEBACK + 5
Case "PlaceKick" ' paramentras:
' SendIN kicker,kicked
' SendOut "Kicked",message
If CTF.User(FindDelAfTekst(Data, 2)).bAdmin = True Then
CTF.User(FindDelAfTekst(Data, 3)) = noUser
W.RemoteHost = FindDelAfTekst(Data, 3)
W.Bind
W.SendData "Kicked|You were kicked by admin!|"
For i = 0 To 31
W.RemoteHost = CTF.User(i).IP
W.Bind
W.SendData "MSG|" & i & " has gone!|"
W.SendData "Away|" & i & "|"
Next i
Else
CTF.User(FindDelAfTekst(Data, 3)).KickVotes = CTF.User(FindDelAfTekst(Data, 3)).KickVotes + 1
If CTF.User(FindDelAfTekst(Data, 3)).KickVotes >= Users Then
CTF.User(FindDelAfTekst(Data, 3)) = noUser
W.RemoteHost = FindDelAfTekst(Data, 3)
W.SendData "Kicked|You were kicked out of the server with " & CTF.User(FindDelAfTekst(Data, 3)).KickVotes & "!|"
For i = 0 To 31
W.RemoteHost = CTF.User(i).IP
W.SendData "MSG|" & i & " has gone!|"
W.SendData "Away|" & i & "|"
Next i
End If
End If
User(FindDelAfTekst(Data, 2)).TIMEBACK = User(i).TIMEBACK + 10
Case "LeaveServer" ' paramentras:
' SendIN none
' SendOut "Kicked",message
CTF.User(FindDelAfTekst(Data, 3)) = noUser
For i = 0 To 31
W.RemoteHost = CTF.User(i).IP
W.Bind
W.SendData "MSG|" & i & " has gone!|"
W.SendData "Away|" & i & "|"
Next i
Case "StopRunning" ' paramentras:
' SendIN Stopper
' SendOut "Kicked",message
If CTF.User(FindDelAfTekst(Data, 2)).bAdmin = True Then
For i = 0 To 32
W.RemoteHost = CTF.User(i).IP
W.Bind
W.SendData "Kicked|The Server have quit! By admin|"
CTF.User(i) = noUser
Next i
QuitServer
Else
W.RemoteHost = CTF.User(FindDelAfTekst(Data, 2)).IP
W.Bind
W.SendData "MSG|Non-Admin can't stop server!|"
User(FindDelAfTekst(Data, 2)).TIMEBACK = User(i).TIMEBACK + 5
End If
Case "Enter" ' paramentras:
' SendIN IP,COMNAME,bAdmin
' SendOut "Joined",server ip,servername
For i = 0 To 31
W.RemoteHost = CTF.User(FindDelAfTekst(Data, 2)).IP
If User(i).IP = "" Then
User(i).bAdmin = FindDelAfTekst(Data, 4)
User(i).COMNAME = FindDelAfTekst(Data, 3)
User(i).IP = FindDelAfTekst(Data, 2)
W.Bind
W.SendData "Joined|Velcome to " & ServerName & ". Hope you will enjoy:-)!"
User(i).TIMEBACK = 20
Exit Sub
End If
Next i
W.SendData "MSG|Sorry server full!|"
Case "Ask" ' paramentras:
' SendIN WHO
' SendOut "Hey"
User(FindDelAfTekst(Data, 2)).TIMEBACK = User(i).TIMEBACK + 1
W.RemoteHost = CTF.User(FindDelAfTekst(Data, 2)).IP
W.Bind
W.SendData "Hey!|"
Case Else
MsgBox DataI
End Select
End Sub
På usercontrolen har jeg en timer med interval 1000,
Et billede kaldet PIC med et icon
og endeligt en winsock kaldet w
Her er modul CTF
Type CTFSERVERTYPE
IP As String
COMNAME As String
bAdmin As Boolean
KickVotes As Integer
TIMEBACK As Integer
End Type
Public User(31) As CTFSERVERTYPE
Public Users As Integer
Public TekstDel(300) As String, TekstFra, TekstDele As Integer
Public Function FindDelAfTekst(temp As String, Which As Integer) As String
' Nulstiller
TekstFra = 1: TekstDele = 0
' Finder hver tekststump i den samlede tekst og deler
' den op i dele.
For n = 1 To Len(temp)
If Mid(temp, n, 1) = "|" Then
TekstDele = TekstDele + 1
TekstDel(TekstDele) = Mid(temp, TekstFra, n - TekstFra)
TekstFra = n + 1
End If
Next n
' Skriver den ønskede del til brugeren
FindDelAfTekst = TekstDel(Which)
End Function
Den er der nok ikke så meget og sige om!
Her er modulet icon:
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dTrayessage As Long, lpData As NotifyIconData) As Long
Public Const Tray_Add = &H0
Public Const Tray_Modify = &H1
Public Const Tray_Delete = &H2
Public Const Tray_Message = &H1
Public Const Tray_Icon = &H2
Public Const Tray_Tip = &H4
Public Const Tray_MouseMove = &H200
Public Const Tray_LeftButtonDoubleClick = &H203
Public Const Tray_LeftButtonDown = &H201
Public Const Tray_LeftButtonUp = &H202
Public Const Tray_RightButtonDoubleClick = &H206
Public Const Tray_RightButtonDown = &H204
Public Const Tray_RightButtonUp = &H205
Type NotifyIconData
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type
Public IconData As NotifyIconData
Det var .OSX filen.
For at kalde den har jeg her koden til et test program:
Jeg har form1. På den er der en Winsock Kaldet W.
Og min usercontrol Server Kaldet S.
Koden er:
Private Sub Form_Load()
S.CreateServer "Please Wait... Makeing Server..."
S.SetServerName "Out there"
W.Protocol = sckUDPProtocol
W.LocalPort = 1123
W.RemotePort = 1122
W.RemoteHost = W.LocalIP
Dim datai As String
Dim data As String
datai = "Enter"
data = "Enter|" & W.LocalIP & "|" & W.LocalHostName & "|" & True & "|"
S.GetData datai, data
End Sub
Private Sub W_DataArrival(ByVal bytesTotal As Long)
Dim data As String
W.GetData data
MsgBox data
End Sub
Hvad der sker gider jeg ikke forklare her men hvis i har nogle spørgsmål så kom med dem!
De svar jeg søger er måske en forklaring af hvad jeg gør galt eller min kode rettet til.
Jeg har forsøgt rigtig mange gange og er nu oppe i omkring V 41 eller mere. Kan ikke lige huske.
Håber i kan hjælpe!
---
En hånd på armen er bedre end to i skraldespanden!... ehh.. :/?