[VB]socket:chat

salut tout le monde ,
je viens de réaliser ma premiére application client/serveur en VB en utilisant le composant winSocket.
A travers cette application le client ou le serveur peut echanger/recevoir des messages textes .
J'ai utilisé le protocole TCP.
Il parait que j'ai un probléme dans le code partie serveur:j'ai utilisé pour ce serveur 2 socket une pour l'écoute et une pour la transmission.
J'ai vraiment besoin d'un cuop de main.
je vous fournis le code de l'application serveur:

Private Sub checkState_Timer()
If (socket.State = 7) Then
go.Caption = "se deconnecter"
send.Enabled = True
Else
send.Enabled = False

If (socket.State = 0) Then ' sachant que socket.state<>7
go.Caption = "se connecter"
Else
go.Caption = "annuler"
'MsgBox ("socket.State:" & socket.State)
End If
End If
If (sckecoute.State = 2) Then
ecoute.Caption = "fin écoute"
Else
ecoute.Caption = "écoute"
End If
End Sub

Private Sub ecoute_Click()
If (ecoute.Caption = "écoute") Then
If (sckecoute.State = 2) Then Exit Sub 'si on est déja en écoute on quitte la produre
sckecoute.LocalPort = 1001
sckecoute.Listen
ecoute.Caption = "fin écoute"
Else
sckecoute.Close
ecoute.Caption = "écoute"
End If
End Sub

Private Sub Form_Load()
checkState_Timer
frmClient.Show
End Sub

Private Sub go_Click()
If (socket.State <> 7) Then
If (socket.State = 0) Then
socket.Close
socket.Connect "127.0.0.2", 97
Else
socket.Close
End If
Else
socket.Close
End If
checkState_Timer
End Sub

Private Sub sckecoute_ConnectionRequest(ByVal requestID As Long)
sckecoute.Close 'UN SOCKET DOIT ÊTRE FERMER AVANT D'ACCEPTER UNE connection
sckecoute.Accept requestID 'accepter une connection entrante
MsgBox (requestID)
End Sub

Private Sub socket_DataArrival(ByVal bytesTotal As Long)
Dim data As String
socket.GetData (data)
reception.Text = reception.Text & vbCrLf & "<" & socket.RemoteHostIP & ">" & vbCrLf & data & vbCrLf & vbCrLf
End Sub

Private Sub send_Click()
reception.Text = reception.Text & vbCrLf & " <you> " & vbCrLf & emission.Text & vbCrLf & vbCrLf
socket.SendData (emission.Text)
emission.Text = ""
End Sub

Private Sub socket_Close()
reception.Text = reception.Text & vbCrLf & " connection terminée! " & vbCrLf & socket.RemoteHostIP & vbCrLf & vbCrLf

End Sub

Private Sub socket_Connect()
reception.Text = reception.Text & "<system> connection etablie avec " & vbCrLf & socket.RemoteHostIP & "!" & vbCrLf & vbCrLf

End Sub
Private Sub socket_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox ("Erreur de connection avec la socket !" & vbCrLf & Number & vbCrLf & Description & vbCrLf & Source & vbCrLf & "connection rejetée" & "SCode:" & Scode)
socket.Close
End Sub
Configuration: Windows XP
Internet Explorer 6.0

3 réponses

  1. je veux savoir la definition de chaque fonction utilisée dans le tchat
    1. 1. Imports System
      2. Imports System.Diagnostics
      3. Imports System.Collections
      4. Imports System.Runtime.InteropServices
      5. Imports ActiveDs
      6.
      7. Namespace ADSI_Net
      8. Class NetApi32
      9. Public Enum NetError
      10. NERR_Success = 0
      11. NERR_BASE = 2100
      12. NERR_UnknownDevDir = (NERR_BASE + 16)
      13. NERR_DuplicateShare = (NERR_BASE + 18)
      14. NERR_BufTooSmall = (NERR_BASE + 23)
      15. End Enum
      16.
      17. Public Enum SHARE_TYPE As ULong
      18. STYPE_DISKTREE = 0
      19. STYPE_PRINTQ = 1
      20. STYPE_DEVICE = 2
      21. STYPE_IPC = 3
      22. STYPE_SPECIAL = &H80000000
      23. End Enum
      24.
      25. <StructLayout(LayoutKind.Sequential)> _
      26. Public Structure SHARE_INFO_502
      27. <MarshalAs(UnmanagedType.LPWStr)> _
      28. Public shi502_netname As String
      29. Public shi502_type As UInteger
      30. <MarshalAs(UnmanagedType.LPWStr)> _
      31. Public shi502_remark As String
      32. Public shi502_permissions As Int32
      33. Public shi502_max_uses As Int32
      34. Public shi502_current_uses As Int32
      35. <MarshalAs(UnmanagedType.LPWStr)> _
      36. Public shi502_path As String
      37. Public shi502_passwd As IntPtr
      38. Public shi502_reserved As Int32
      39. Public shi502_security_descriptor As IntPtr
      40. End Structure
      41.
      42. <DllImport("Netapi32.dll")> _
      43. Public Shared Function NetShareAdd(<MarshalAs(UnmanagedType.LPWStr)> ByVal strServer As String, ByVal dwLevel As Int32, ByVal buf As IntPtr, ByVal parm_err As IntPtr) As Integer
      44. End Function
      45.
      46.
      47. End Class
      48.
      49. Class AD_ShareUtil
      50. <STAThread()> _
      51. Private Shared Sub Main(ByVal args As String())
      52. Dim strServer As String = "HellRaiser"
      53. Dim strShareFolder As String = "G:\Mp3folder"
      54. Dim strShareName As String = "MyMP3Share"
      55. Dim strShareDesc As String = "Share to store MP3 files"
      56. Dim nRetVal As NetApi32.NetError = 0
      57. Dim shUtil As New AD_ShareUtil()
      58. nRetVal = shUtil.CreateShare(strServer, strShareFolder, strShareName, strShareDesc, False)
      59. If nRetVal = NetApi32.NetError.NERR_Success Then
      60. Console.WriteLine("Share {0} created", strShareName)
      61. ElseIf nRetVal = NetApi32.NetError.NERR_DuplicateShare Then
      62. Console.WriteLine("Share {0} already exists", strShareName)
      63. End If
      64. End Sub
      65.
      66. Private Function CreateShare(ByVal strServer As String, ByVal strPath As String, ByVal strShareName As String, ByVal strShareDesc As String, ByVal bAdmin As Boolean) As NetApi32.NetError
      67. Dim shInfo As NetApi32.SHARE_INFO_502 = New ADSI_Net.NetApi32.SHARE_INFO_502()
      68. shInfo.shi502_netname = strShareName
      69. shInfo.shi502_type = CUInt(NetApi32.SHARE_TYPE.STYPE_DISKTREE)
      70. If bAdmin Then
      71. shInfo.shi502_type = CUInt(NetApi32.SHARE_TYPE.STYPE_SPECIAL)
      72. shInfo.shi502_netname += "$"
      73. End If
      74. shInfo.shi502_permissions = 0
      75. shInfo.shi502_path = strPath
      76. shInfo.shi502_passwd = IntPtr.Zero
      77. shInfo.shi502_remark = strShareDesc
      78. shInfo.shi502_max_uses = -1
      79. shInfo.shi502_security_descriptor = IntPtr.Zero
      80.
      81. Dim strTargetServer As String = strServer
      82. If strServer.Length <> 0 Then
      83. strTargetServer = strServer
      84. If strServer(0) <> "\"c Then
      85. strTargetServer = "\\" & strServer
      86. End If
      87. End If
      88. Dim nRetValue As Integer = 0
      89. ' Call Net API to add the share..
      90. Dim nStSize As Integer = Marshal.SizeOf(shInfo)
      91. Dim buffer As IntPtr = Marshal.AllocCoTaskMem(nStSize)
      92. Marshal.StructureToPtr(shInfo, buffer, False)
      93. nRetValue = NetApi32.NetShareAdd(strTargetServer, 502, buffer, IntPtr.Zero)
      94. Marshal.FreeCoTaskMem(buffer)
      95.
      96. Return DirectCast(nRetValue, NetApi32.NetError)
      97. End Function
      98. End Class
      99. End Namespace