Socket을 이용한 간단 메신저 코드

V닉맨 2015.01.12 23:22:34 *: Imports System.IO
Imports System.Threading
Imports System.Net.Sockets

Public Class Form1
Dim Listener As New TcpListener(8000)
Dim Clint As TcpClient
Dim Message As String

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

Private Sub Listening()
Listener.Start()
End Sub
Private Sub 보내기()
Dim MyTime As String = My.Computer.Clock.LocalTime.ToShortTimeString

Clint = New TcpClient(TextBox1.Text, 8000)
Dim StreamW As New StreamWriter(Clint.GetStream())
StreamW.Write(TextBox3.Text)
TextBox2.AppendText("나 : " & TextBox3.Text & MyTime & " " & vbNewLine)
StreamW.Flush()
TextBox3.Focus()
TextBox3.Text = ""
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
보내기()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim MyTime As String = My.Computer.Clock.LocalTime.ToShortTimeString

If Listener.Pending = True Then
Message = ""

Clint = Listener.AcceptTcpClient
Dim StreamR As New StreamReader(Clint.GetStream())
While StreamR.Peek > -1

Message = Message + Convert.ToChar(StreamR.Read()).ToString
End While

TextBox2.AppendText("상대방 : " & Message & " " & MyTime & vbNewLine)
End If
End Sub

Private Sub TextBox3_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown
If e.KeyCode = Keys.Enter Then 보내기()
End Sub
End Class

* Socket을 이용한 간단한 메신져 코드 입니다

* 다른 대역의 IP를 입력해서 테스트 해보세요



123.png


20150112-IP_Chat.zip