Results 1 to 3 of 3

Thread: Need help in network chatting

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    88

    Need help in network chatting

    Hi All

    I need to make a network chatting program.in which if 5 systems are in
    network they can chat with each other.
    I have made a program in VB using winsok but its taking only one one machine's IP.if i m connecting the system2 through IP in system1 its connecting but when system2 connecting sysetm1 its not connecting.

    Here is my code
    //
    code for client

    Private username As String

    Private Sub cmdConnect_Click()
    'Collect the Data for the username and IP Address
    username = txtUserName.Text
    Winsock1(0).Connect txtIP.Text, 10101
    End Sub
    Private Sub cmdDisconnect_Click()
    'Close the connection
    SafeClose
    txtIP.Enabled = True
    txtUserName.Enabled = True
    cmdConnect.Enabled = True
    cmdConnect.Visible = True
    cmdDisconnect.Visible = False
    End Sub
    Private Sub cmdSend_Click()
    'Check if the socket is still connected
    SafeSend username & ": " & txtMessage.Text
    txtMessage.Text = ""
    txtMessage.SetFocus
    End Sub

    'This method closes the connection when the program is exited
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    SafeClose
    End Sub

    ''Private Sub Winsock1_Connect()
    ' txtIP.Enabled = False
    ' txtUserName.Enabled = False
    ' cmdConnect.Enabled = False
    ' cmdConnect.Visible = False
    ' cmdDisconnect.Visible = True
    'End Sub

    Private Sub Winsock1_Connect(Index As Integer)
    txtIP.Enabled = False
    txtUserName.Enabled = False
    cmdConnect.Enabled = False
    cmdConnect.Visible = False
    cmdDisconnect.Visible = True
    End Sub

    'Private Sub Winsock1_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)
    ' 'In case of Winsock1 error close connection
    ' SafeClose
    'End Sub

    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim strIncomingData As String
    'Get the Data from the Server
    Winsock1(Index).GetData strIncomingData
    'Display the data in the Chat history text box
    txtHistory.Text = txtHistory.Text & vbCrLf & strIncomingData
    End Sub

    Private Sub SafeClose()
    If Winsock1(0).State = sckConnected Then
    Winsock1(0).Close
    End If
    End Sub

    Private Sub SafeSend(ByVal strData As String)
    If Winsock1(0).State = sckConnected Then
    Winsock1(0).SendData strData
    End If
    End Sub

    //end code


    // code for Server

    Private blnArray() As Boolean 'Global variable that shows whether or not the socket at index "i" is in use

    Private Sub Form_Load()
    ReDim blnArray(0)
    'set Local Port
    Winsock1(0).LocalPort = 10101 'This is the port the server listens on
    'Listen
    Winsock1(0).Listen
    blnArray(0) = True
    End Sub

    Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    'Check if any free sockets
    If Winsock1.Count <> 1 Then
    For i = 1 To Winsock1.Count - 1
    If blnArray(i) = False Then 'Checks if the socket at index "i" is available
    Winsock1(i).Accept requestID 'Accepted connection to old but available socket
    Exit Sub
    End If
    Next i
    End If

    'Only runs if no open sockets
    'Increase size of blnArray
    ReDim Preserve blnArray(Winsock1.UBound + 1)
    'Load new Winsock1

    Load Winsock1(Winsock1.UBound + 1)
    'Accept Connection to newly created socket
    Winsock1(Winsock1.UBound).Accept requestID
    'Updates the Array at requested position to true stating socket is in use at theis index
    blnArray(Winsock1.UBound) = True
    'Do not worry your new Winsock1 control automatically is given an open port by the Windows OS
    End Sub

    Private Sub Winsock1_Close(Index As Integer)
    SafeClose (Index)
    End Sub

    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim strData As String
    Dim i As Integer
    'get data from client
    Winsock1(Index).GetData strData
    'Send the data to all connected clients
    If LCase(strData) = "/quit" Then
    'Closes the connection at index of requested user
    SafeClose (Index)
    Else
    For i = LBound(blnArray) To UBound(blnArray)
    SafeSend i, strData
    Next i
    End If
    End Sub

    Private Sub Winsock1_Error(Index As Integer, 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)
    'Closes the connection at the errored index
    SafeClose (Index)
    End Sub

    Private Sub SafeSend(Index As Integer, ByVal strData As String)
    If Winsock1(Index).State = sckConnected Then
    Winsock1(Index).SendData strData
    End If
    End Sub

    Private Sub SafeClose(Index As Integer)
    If Winsock1(Index).State = sckConnected Then
    blnArray(Index) = False
    Winsock1(Index).Close
    Unload Winsock1(Index)
    End If
    End Sub

    // end code


    Please help me where i m doing wrong.
    if any other way to do the same the please help me to solve my problem.

    Thnaks

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Need help in network chatting

    Hi

    Check this out...

    i had downloaded this file longtime back so i don't remember who is the author but it works just fine

    Hope it helps...
    Last edited by Siddharth Rout; Mar 25th, 2007 at 08:41 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Need help in network chatting

    It would make it a lot easier if you put your code in [ VBCODE] [ /VBCODE] tags.

    Also, you don't need a boolean variable for checking if a socket is in use. You can use the Winsock.State property to check if it = sckClosed.

    Here is an example for loading a new socket for a connection.

    VB Code:
    1. Option Explicit
    2.  
    3. 'Maximum possible number of sockets that can be loaded (integer value).
    4. Private Const MAX_CLIENTS As Integer = 32767
    5.  
    6. 'Finds an available socket to use.
    7. 'If none are found, then it loads one.
    8. Private Function NextOpenSocket() As Integer
    9.     Dim intLoop As Integer, intFound As Integer
    10.     Dim intNew As Integer
    11.    
    12.     If sckServer.UBound = 0 Then
    13.         Load sckServer(1)
    14.         sckServer(1).Close
    15.         NextOpenSocket = 1
    16.     Else
    17.        
    18.         For intLoop = 1 To sckServer.UBound
    19.            
    20.             If sckServer(intLoop).State = sckClosed Then
    21.                 intFound = intLoop
    22.                 Exit For
    23.             End If
    24.        
    25.         Next intLoop
    26.        
    27.         If intFound > 0 Then
    28.             sckServer(intLoop).Close
    29.             NextOpenSocket = intLoop
    30.         Else
    31.            
    32.             If sckServer.UBound < MAX_CLIENTS Then
    33.                 intNew = sckServer.UBound + 1
    34.                 Load sckServer(intNew)
    35.                 sckServer(intNew).Close
    36.                 NextOpenSocket = intNew
    37.             End If
    38.        
    39.         End If
    40.    
    41.     End If
    42.    
    43. End Function
    44.  
    45. Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    46.     Dim intNext As Integer
    47.    
    48.     'Get next available socket.
    49.     intNext = NextOpenSocket
    50.    
    51.     If intNext > 0 Then
    52.         sckServer(intNext).Accept requestID 'Found one.
    53.     Else 'All MAX_CLIENTS sockets are being used.
    54.         Debug.Print "MAX CLIENTS (" & MAX_CLIENTS & ") REACHED! CONNECTION DENIED."
    55.     End If
    56.    
    57. End Sub

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