?
Solved

Network chat

Posted on 2004-10-05
4
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:gregasm
ID: 12228570
What network? Is there a firewall between the two clients?
0
 

Author Comment

by:gleznov
ID: 12228900
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 400 total points
ID: 12228970
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 600 total points
ID: 12235366
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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