Results 1 to 8 of 8

Thread: How can I make this code better??? Winsock Related

  1. #1

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    How can I make this code better??? Winsock Related

    hi... I using this code for a chat program....
    Is there a way to use only one Winsock to send and recive data??
    I also want that when someone request a connection, both sides conects....

    VB Code:
    1. ''''''''''Server code
    2. Dim intMax As Integer
    3.  
    4. Private Sub Command2_Click()
    5.     Winsock2.SendData "(" & Format(Time, "HH:MM") & ") " & Winsock2.LocalHostName & " »»>  " & Text1.Text
    6.     List1.AddItem "(" & Format(Time, "HH:MM") & ") " & Winsock2.LocalHostName & " »»>  " & Text1.Text
    7.     Text1.Text = ""
    8.     Text1.SetFocus
    9. End Sub
    10.  
    11.  
    12. Private Sub Form_Load()
    13.     Me.Winsock1(0).LocalPort = GetSetting(App.EXEName, "Options", "Port", "6668")
    14.     Winsock1(0).Listen
    15.     List1.AddItem "(" & Format(Time, "HH:MM") & ") " & Winsock2.LocalHostName & " »»>  " & Text1.Text
    16.  
    17. End Sub
    18.  
    19.  
    20. Private Sub Text1_KeyPress(KeyAscii As Integer)
    21. If KeyAscii = vbKeyReturn Then
    22.     Command2_Click
    23. End If
    24. End Sub
    25.  
    26. Private Sub Winsock1_Close(Index As Integer)
    27.     Winsock1(Index).SendData "Conexão Fechada para:" & Winsock1(Index).RemoteHostIP & "  Em: " & Now
    28.     Winsock1(Index).Close
    29.     List1.AddItem "Conexão Fechada para:" & Winsock1(Index).RemoteHostIP & "  Em: " & Now
    30. End Sub
    31.  
    32.  
    33. Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    34.     If Index = 0 Then
    35.         intMax = intMax + 1
    36.         Load Winsock1(intMax)
    37.         Winsock1(intMax).LocalPort = 0
    38.         Winsock1(intMax).Accept requestID
    39.         List1.AddItem "Conexão iniciada para: " & Winsock1(Index).RemoteHostIP & "  Em: " & Now
    40.     End If
    41. End Sub
    42.  
    43. Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    44. Dim strIncoming As String, Result As String
    45. Dim sString As String, MyArray() As String
    46.  
    47.     Winsock1(Index).GetData strIncoming
    48.  
    49.     List1.AddItem strIncoming
    50.  
    51.     Winsock1(Index).SendData Result
    52.    
    53. End Sub
    54.  
    55. '''''Client Code
    56. Private Sub Command1_Click()
    57.  
    58. If Winsock2.State <> sckClosed Then Winsock2.Close
    59.  
    60.     Winsock2.RemotePort = Combo1.Text
    61.     Winsock2.RemoteHost = Combo2.Text
    62.  
    63.     Winsock2.Connect
    64.  
    65. ' Try and Connect to the Server
    66. Do Until Winsock2.State = sckConnected
    67.     DoEvents: DoEvents: DoEvents: DoEvents
    68.     If Winsock2.State = sckError Then
    69.         MsgBox "There was a problem connecting to the Server!" & vbCrLf & "Please try again later!", vbOKOnly, "Connection Error"
    70.         Exit Sub
    71.     End If
    72. Loop
    73.  
    74. Winsock2.SendData Now & " »»> " & Text1.Text
    75. Command2.Enabled = True
    76. Text1.Enabled = True
    77. Command1.Enabled = False
    78.  
    79. End Sub
    80.  
    81.  
    82.  
    83. Private Sub Winsock2_ConnectionRequest(ByVal requestID As Long)
    84.         intMax = intMax + 1
    85.      
    86.         Winsock1(intMax).LocalPort = 0
    87.         Winsock1(intMax).Accept requestID
    88.         List1.AddItem "Conexão iniciada para: " & Winsock1(Index).RemoteHostIP & "  Em: " & Now
    89.         Winsock2.SendData "Conexão iniciada para: " & Winsock1(Index).RemoteHostIP & "  Em: " & Now
    90.  
    91.     Winsock2.RemotePort = Combo1.Text
    92.     Winsock2.RemoteHost = Winsock1(Index).RemoteHostIP
    93.  
    94.     Winsock2.Connect
    95.  
    96. ' Try and Connect to the Server
    97. Do Until Winsock2.State = sckConnected
    98.     DoEvents: DoEvents: DoEvents: DoEvents
    99.     If Winsock2.State = sckError Then
    100.         MsgBox "There was a problem connecting to the Server!" & vbCrLf & "Please try again later!", vbOKOnly, "Connection Error"
    101.         Exit Sub
    102.     End If
    103. Loop
    104.  
    105. Winsock2.SendData Now & " »»> " & Text1.Text
    106. Command2.Enabled = True
    107. Text1.Enabled = True
    108. Command1.Enabled = False
    109.  
    110.  
    111.  
    112.  
    113. End Sub
    114.  
    115. Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
    116. Dim strReply As String
    117.  
    118.     Winsock2.GetData (strReply)
    119.    
    120.         MsgBox strReply
    121.  
    122. End Sub


    thanks in advance
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Daskalos,

    Clarify, What do you mean both sides connect?

  3. #3

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364
    well... if 2 computers are using the program, one of them connects to other IP and sends a message', it works fine, but when the second person try to send a message, it says that there are an error in protocol, so the second person has to get the 1st person IP and connects to it

    undestand now???
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Daskalos,

    Still not clear on what you mean, but if you are writting a chat program you probably want to use the UDP protocol not the TCP protocol. The difference is that in UDP you do not have to be connected at all times it only connect when you are sending something then disconnects but stays open and your communications is not guaranteed delivery.

    Usually when you get the protocol error, it means that you are attempting to send on a port that is in the process of receiving at the moment. So you may want to put in a loop that will let you know when it is done then request a send.

  5. #5

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364
    thanks for the advice...
    to use the UDP protocol, what I have to do???

    talking about the error... I don't know what to do... Chatter1 have to get Chatter2 IP and conect to send messages and Chatter2 have to get Chatter1 IP to send messages... What i want to do was Chatter1 gets Chatter2 IP than he conects and both Chatters send messages to each other.

    Do I make myself clear now??

    THanks in advance
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  6. #6
    if you want to have the winsock to accept the connection when a connection request is sent then all you need to do is

    VB Code:
    1. Private Sub ws_ConnectionRequest(ByVal requestID As Long)
    2. ws.Close
    3. ws.Accept requestID
    4. End Sub

    this should do what you ask.

    however you seem to want to have multiple connections.

    for this you could use UDP for the connections, as this does not need any handshaking, but is then not very secure.

    the code you used
    VB Code:
    1. Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    2.     If Index = 0 Then
    3.         intMax = intMax + 1
    4.         Load Winsock1(intMax)
    5.         Winsock1(intMax).LocalPort = 0
    6.         Winsock1(intMax).Accept requestID
    7.         List1.AddItem "Conexão iniciada para: " & Winsock1(Index).RemoteHostIP & "  Em: " & Now
    8.     End If
    9. End Sub
    should work.

    also when you accept a connection both sides will be connected to each other.

  7. #7

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364
    so, if I want to change my program from TCP to UDP I don't have to change anything in the code (only the winsock.listen)
    and then change Winsock protocols??
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Daskalos,

    Basically - Yes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width