Solved

Network chat

Posted on 2004-10-05
4
219 Views
Last Modified: 2010-04-23
Hi,

     I'd like to make a quick personalized 2-person network chat program.  Something that can kinda stay resident but not be in the way, and that will allow my computer and another on the network who's computer name I know to chat.  How can I go about this?  Any sample code?

JP
0
Comment
Question by:gleznov
  • 2
4 Comments
 
LVL 8

Expert Comment

by:gregasm
Comment Utility
What network? Is there a firewall between the two clients?
0
 

Author Comment

by:gleznov
Comment Utility
An internal network.  I don't think there's a firewall between the clients, just between the network and the internet.

JP
0
 
LVL 8

Assisted Solution

by:gregasm
gregasm earned 100 total points
Comment Utility
In that case, you can use remoting over TCP IP channels:

http://www.codeproject.com/csharp/c_sharp_remoting.asp

http://www.codeproject.com/dotnet/dotnetchatapplication.asp

http://www.codeproject.com/csharp/socketsincs.asp

If you should need to communicate through a firewall, I recommend using a web service as your server. It can relay SOAP encoded messages through firewalls.

Remoting is great! Have fun.

0
 
LVL 3

Accepted Solution

by:
jazduck earned 150 total points
Comment Utility
The following is a basic implementation of a chat program.  Very basic, but it allows for two way chat, if you want it to be out of the way you can look at minimizing to systray, but thats an answer for a different question :)

Should work with IP's or Hostnames, feel free to alter the port if you find it conflicts with any of your current apps, I havn't commented the code as it was a rush job, but if you dont understand something, just post and ill try and clear it up for you :)  Good luck :)

Jaz


Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim TcpListener As New TcpListener(2233)
    Dim TcpClient As New TcpClient()
    Const KEY_ENTER = Chr(13)

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtChatSend As System.Windows.Forms.TextBox
    Friend WithEvents txtAlias As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtIP As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtChatReceive As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtChatSend = New System.Windows.Forms.TextBox()
        Me.txtAlias = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtIP = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.txtChatReceive = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'txtChatSend
        '
        Me.txtChatSend.Location = New System.Drawing.Point(24, 304)
        Me.txtChatSend.Multiline = True
        Me.txtChatSend.Name = "txtChatSend"
        Me.txtChatSend.Size = New System.Drawing.Size(552, 88)
        Me.txtChatSend.TabIndex = 0
        Me.txtChatSend.Text = ""
        '
        'txtAlias
        '
        Me.txtAlias.Location = New System.Drawing.Point(64, 16)
        Me.txtAlias.Name = "txtAlias"
        Me.txtAlias.Size = New System.Drawing.Size(128, 20)
        Me.txtAlias.TabIndex = 1
        Me.txtAlias.Text = "Jaz"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(32, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(32, 24)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Alias"
        '
        'txtIP
        '
        Me.txtIP.Location = New System.Drawing.Point(240, 16)
        Me.txtIP.Name = "txtIP"
        Me.txtIP.Size = New System.Drawing.Size(128, 20)
        Me.txtIP.TabIndex = 3
        Me.txtIP.Text = "192.168.0.82"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(216, 16)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(16, 16)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "IP"
        '
        'txtChatReceive
        '
        Me.txtChatReceive.Location = New System.Drawing.Point(24, 48)
        Me.txtChatReceive.Multiline = True
        Me.txtChatReceive.Name = "txtChatReceive"
        Me.txtChatReceive.Size = New System.Drawing.Size(552, 240)
        Me.txtChatReceive.TabIndex = 7
        Me.txtChatReceive.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(424, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(112, 24)
        Me.Button1.TabIndex = 8
        Me.Button1.Text = "Clear"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(600, 414)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.txtChatReceive, Me.Label2, Me.txtIP, Me.Label1, Me.txtAlias, Me.txtChatSend})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t As New Thread(AddressOf StartServer)
        t.Start()
    End Sub



    Sub StartServer()
        TcpListener.Start()
        Do
            Try
                TcpClient = TcpListener.AcceptTcpClient()

                Dim i As Integer = 0
                Dim clientData As String = ""
                Dim output As String = ""
                Dim [bytestream](TcpClient.ReceiveBufferSize) As Byte
                Dim networkStream As NetworkStream = TcpClient.GetStream()

                networkStream.Read(bytestream, 0, TcpClient.ReceiveBufferSize)
                output = Encoding.ASCII.GetString(bytestream)

                If InStr(output, Chr(0)) Then
                    For i = 1 To Len(output)
                        If Mid(output, i, 1) <> Chr(0) Then
                            clientData = clientData & Mid(output, i, 1)
                        Else
                            Exit For
                        End If
                    Next
                End If

                txtChatReceive.Text = clientData
                TcpClient.Close()

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error!")
            End Try
        Loop
    End Sub

    Sub SendData(ByVal Data As String)
        Try
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            tcpClient.Connect(txtIP.Text, 2233)
            Dim networkStream As NetworkStream = tcpClient.GetStream()

            If networkStream.CanWrite And networkStream.CanRead Then
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(txtAlias.Text & ": " & Data)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                tcpClient.Close()
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub txtChatSend_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtChatSend.KeyPress
        If e.KeyChar = KEY_ENTER Then
            SendData(txtChatSend.Text)
            txtChatSend.Clear()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtChatReceive.Clear()
        txtChatSend.Clear()
    End Sub
End Class
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now