Lan을 이용한 메세지 전송 코드

V닉맨 2014.12.15 16:13:54 *: *수신코드*
Option Explicit On
Imports System.IO
Imports System.Net.Sockets

Public Class Form1
Dim Listener As New TcpListener(8000)
Dim Client As TcpClient

Private Sub _FormClosing() Handles Me.FormClosing
Listener.Stop()
End Sub

Private Sub _Load() Handles MyBase.Load
Timer1.Start()
Listener.Start()
End Sub

Private Sub _Tick() Handles Timer1.Tick
Dim Message As String
Dim nStart As Integer
Dim nLast As Integer

If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream())

While Reader.Peek > -1
Message &= Convert.ToChar(Reader.Read()).ToString
End While

If Message.Contains("</>") Then
nStart = InStr(Message, "</>") + 4
nLast = InStr(Message, "<\>")
Message = Mid(Message, nStart, nLast - nStart)
End If

Label1.Text = Message
End If
End Sub
End Class


*전송코드*
Option Explicit On
Imports System.IO
Imports System.Net.Sockets

Public Class Form1
Dim Client As TcpClient

Private Sub btnClick() Handles Button1.Click
Try
Client = New TcpClient("127.0.0.1", 8000)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write("</> " & TextBox1.Text & " <\>")
Writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class



* Lan을 이용한 메세지 전송코드 입니다.

* 가장 기본적인 코드로 이해 하고 활용하면 좋을것 같습니다

* 현재 전송, 수신 프로그램이 나눠서 실행돼는데, 하나로 합쳐질수 있게 해보세요


11.png

20141216-LanChat_Sender.zip

20141216-LamChat_수신.zip