Advertisement
TermSpar

Code for a TCP messenger

Oct 28th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.96 KB | None | 0 0
  1. Option Explicit On
  2. Imports System.IO
  3. Imports System.Net.Sockets
  4. Imports System.Net
  5.  
  6. Public Class Form1
  7.     Dim Client As TcpClient
  8.     Dim Listener As New TcpListener(8000)
  9.     Dim Client2 As TcpClient
  10.     Private Sub _FormClosing() Handles Me.FormClosing
  11.         Listener.Stop()
  12.     End Sub
  13.     Private Sub _Load() Handles MyBase.Load
  14.         Timer2.Start()
  15.         Listener.Start()
  16.     End Sub
  17.     Private Sub _Tick() Handles Timer1.Tick
  18.         txtNick.Hide()
  19.         Dim Message As String
  20.         Dim nStart As Integer
  21.         Dim nLast As Integer
  22.  
  23.         If Listener.Pending = True Then
  24.             Message = ""
  25.             Client = Listener.AcceptTcpClient()
  26.             Dim Reader As New StreamReader(Client.GetStream())
  27.  
  28.             While Reader.Peek > -1
  29.                 Message &= Convert.ToChar(Reader.Read()).ToString
  30.             End While
  31.  
  32.             If Message.Contains("</>") Then
  33.                 nStart = InStr(Message, "</>") + 4
  34.                 nLast = InStr(Message, "<\>")
  35.                 Message = Mid(Message, nStart, nLast - nStart)
  36.             End If
  37.  
  38.             RichTextBox1.AppendText(Message & vbNewLine)
  39.         End If
  40.     End Sub
  41.    
  42.  
  43.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs)
  44.  
  45.  
  46.     End Sub
  47.  
  48.     Private Sub Button3_Click(sender As System.Object, e As System.EventArgs)
  49.         Me.ComboBox1.Items.AddRange(Dns.GetHostAddresses(GetComputerName))
  50.     End Sub
  51.  
  52.     Private Function GetComputerName() As String
  53.         Return Dns.GetHostName
  54.     End Function
  55.  
  56.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  57.         Timer1.Start()
  58.        
  59.         Dim r As Rectangle
  60.         If Parent IsNot Nothing Then
  61.             r = Parent.RectangleToScreen(Parent.ClientRectangle)
  62.         Else
  63.             r = Screen.FromPoint(Me.Location).WorkingArea
  64.         End If
  65.  
  66.         Dim x = r.Left + (r.Width - Me.Width) \ 2
  67.         Dim y = r.Top + (r.Height - Me.Height) \ 2
  68.         Me.Location = New Point(x, y)
  69.  
  70.  
  71.         Me.ComboBox1.Items.AddRange(Dns.GetHostAddresses(GetComputerName))
  72.         Label8.Hide()
  73.         Dim myIPaddress As String
  74.         Dim tmpHostName As String = System.Net.Dns.GetHostName()
  75.         myIPaddress = System.Net.Dns.GetHostByName(tmpHostName).AddressList(0).ToString()
  76.         txtNick.Text = myIPaddress
  77.     End Sub
  78.  
  79.     Private Sub Button2_Click_1(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  80.         Application.Exit()
  81.     End Sub
  82.  
  83.     Private Sub btnClick(sender As System.Object, e As System.EventArgs)
  84.  
  85.     End Sub
  86.  
  87.     Private Sub PictureBox2_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox2.Click
  88.         Dim Person As String
  89.         Person = ComboBox1.Text
  90.         Try
  91.             Client = New TcpClient(Person, 8000)
  92.             Dim Writer As New StreamWriter(Client.GetStream())
  93.             Writer.Write("</> " & TextBox1.Text & " -From " & txtNickname.Text & " (" & txtNick.Text & ")" & " at " & Label8.Text & " <\>")
  94.             Writer.Flush()
  95.         Catch ex As Exception
  96.             MsgBox(ex.Message)
  97.         End Try
  98.         If ComboBox1.Text = Nothing Then
  99.             MsgBox("Please Enter an IP To Recieve Your Message", MsgBoxStyle.Critical)
  100.         End If
  101.         Me.Text = "IPv4 Messanger (Messaging: " & ComboBox1.Text & " as " & txtNick.Text & ")"
  102.         ListBox1.Items.Add(ComboBox1.Text)
  103.     End Sub
  104.  
  105.     Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
  106.  
  107.     End Sub
  108.     Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  109.         If e.KeyCode = Keys.Enter Then
  110.             Dim Person As String
  111.             Person = ComboBox1.Text
  112.             Try
  113.                 Client = New TcpClient(Person, 8000)
  114.                 Dim Writer As New StreamWriter(Client.GetStream())
  115.                 Writer.Write("</> " & TextBox1.Text & " -From " & txtNickname.Text & " (" & txtNick.Text & ")" & " at " & Label8.Text & " <\>")
  116.                 Writer.Flush()
  117.             Catch ex As Exception
  118.                 MsgBox(ex.Message)
  119.             End Try
  120.             ListBox1.Items.Add(ComboBox1.Text)
  121.         End If
  122.         Me.Text = "IPv4 Messanger (Messaging: " & ComboBox1.Text & " as " & txtNick.Text & ")"
  123.     End Sub
  124.  
  125.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  126.         TextBox1.Clear()
  127.     End Sub
  128.  
  129.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
  130.         Label8.Text = Format(Now, "hh:mm tt")
  131.     End Sub
  132.  
  133.     Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
  134.         Application.Exit()
  135.     End Sub
  136.  
  137.     Private Sub Button3_Click_1(sender As System.Object, e As System.EventArgs) Handles Button3.Click
  138.         ListBox1.Items.Clear()
  139.     End Sub
  140.  
  141.     Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  142.  
  143.     End Sub
  144.  
  145.     Private Sub txtNick_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNick.TextChanged
  146.  
  147.     End Sub
  148.  
  149.     Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
  150.  
  151.     End Sub
  152.  
  153.     Private Sub InfoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InfoToolStripMenuItem.Click
  154.         MsgBox("IPv4 Messanger uses a system of networking called TCP to send messages from one IPv4 address to another as long as they are on the same WiFi", MsgBoxStyle.Information)
  155.     End Sub
  156.  
  157.     Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
  158.         RichTextBox1.Clear()
  159.     End Sub
  160.  
  161.     Private Sub RedToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RedToolStripMenuItem.Click
  162.         RichTextBox1.ForeColor = Color.Red
  163.         RichTextBox1.BackColor = Color.White
  164.         TextBox1.BackColor = Color.White
  165.         TextBox1.ForeColor = Color.Black
  166.     End Sub
  167.  
  168.     Private Sub BlackToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BlackToolStripMenuItem.Click
  169.         RichTextBox1.ForeColor = Color.Black
  170.         RichTextBox1.BackColor = Color.White
  171.         TextBox1.BackColor = Color.White
  172.         TextBox1.ForeColor = Color.Black
  173.     End Sub
  174.  
  175.     Private Sub PinkToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PinkToolStripMenuItem.Click
  176.         RichTextBox1.ForeColor = Color.Violet
  177.         RichTextBox1.BackColor = Color.White
  178.         TextBox1.BackColor = Color.White
  179.         TextBox1.ForeColor = Color.Black
  180.     End Sub
  181.  
  182.     Private Sub BlueToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BlueToolStripMenuItem.Click
  183.         RichTextBox1.ForeColor = Color.Blue
  184.         RichTextBox1.BackColor = Color.White
  185.         TextBox1.BackColor = Color.White
  186.         TextBox1.ForeColor = Color.Black
  187.     End Sub
  188.  
  189.     Private Sub LimeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LimeToolStripMenuItem.Click
  190.         RichTextBox1.ForeColor = Color.Green
  191.         TextBox1.BackColor = Color.White
  192.         TextBox1.ForeColor = Color.Black
  193.         RichTextBox1.BackColor = Color.White
  194.     End Sub
  195.  
  196.     Private Sub H4X0RToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles H4X0RToolStripMenuItem.Click
  197.         RichTextBox1.ForeColor = Color.Lime
  198.         RichTextBox1.BackColor = Color.Black
  199.         TextBox1.BackColor = Color.Black
  200.         TextBox1.ForeColor = Color.Lime
  201.     End Sub
  202.  
  203.     Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
  204.  
  205.     End Sub
  206.  
  207.     Private Sub Label8_Click(sender As System.Object, e As System.EventArgs) Handles Label8.Click
  208.  
  209.     End Sub
  210. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement