|
-
Jan 5th, 2007, 07:35 AM
#1
Thread Starter
Lively Member
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
-
Jan 5th, 2007, 08:08 AM
#2
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
-
Jan 5th, 2007, 08:27 AM
#3
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:
Option Explicit
'Maximum possible number of sockets that can be loaded (integer value).
Private Const MAX_CLIENTS As Integer = 32767
'Finds an available socket to use.
'If none are found, then it loads one.
Private Function NextOpenSocket() As Integer
Dim intLoop As Integer, intFound As Integer
Dim intNew As Integer
If sckServer.UBound = 0 Then
Load sckServer(1)
sckServer(1).Close
NextOpenSocket = 1
Else
For intLoop = 1 To sckServer.UBound
If sckServer(intLoop).State = sckClosed Then
intFound = intLoop
Exit For
End If
Next intLoop
If intFound > 0 Then
sckServer(intLoop).Close
NextOpenSocket = intLoop
Else
If sckServer.UBound < MAX_CLIENTS Then
intNew = sckServer.UBound + 1
Load sckServer(intNew)
sckServer(intNew).Close
NextOpenSocket = intNew
End If
End If
End If
End Function
Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim intNext As Integer
'Get next available socket.
intNext = NextOpenSocket
If intNext > 0 Then
sckServer(intNext).Accept requestID 'Found one.
Else 'All MAX_CLIENTS sockets are being used.
Debug.Print "MAX CLIENTS (" & MAX_CLIENTS & ") REACHED! CONNECTION DENIED."
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|