Hi everyone,
I have an assignment to make a chat application between two computers.
I made the both sides' application concerning the simple text chat and the next step required is to send a file (specifically an image).
How can I send it and receive it? I saw really many ways and I feel so fuzzy now.
This is my code for both sides:
SENDER(Server):
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.IO
Imports System.Drawing.Imaging
Public Class ServerForm
Private sock As Socket
Private acc As Socket
Private trd As Thread
Private Const EXIT_MSG As String = "{BYEBYE}"
Sub Listen()
Dim port As Integer = CInt(portTxt.Text)
GUIListening()
sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
sock.Bind(New IPEndPoint(IPAddress.Any, port))
sock.Listen(0)
trd = New Thread(AddressOf ThreadTask)
trd.Start()
End Sub
Sub Disconnect()
If (Not (acc Is Nothing)) Then
acc.Close()
End If
If (Not (sock Is Nothing)) Then
sock.Close()
End If
Me.Invoke(New MethodInvoker(AddressOf GUIDisconnect))
End Sub
Sub ThreadTask()
acc = sock.Accept()
Me.Invoke(New MethodInvoker(AddressOf GUIConnect))
Read()
sock.Close()
End Sub
Sub Read()
While (True)
Try
Dim data(1023) As Byte
Dim re As Integer = acc.Receive(data, 0, data.Length, SocketFlags.None)
Array.Resize(data, re)
Dim msg As String = Encoding.Default.GetString(data)
If (msg = EXIT_MSG) Then
Disconnect()
Return
End If
Dim tstr As String
tstr = "Client: " + msg
Me.Invoke(New MethodInvoker(Function() msgsBox.Items.Add(tstr)))
Catch ex As Exception
MsgBox(ex.Message)
Return
End Try
End While
End Sub
Sub Send(ByVal msg As String)
Dim data() As Byte = Encoding.Default.GetBytes(msg)
acc.Send(data, 0, data.Length, SocketFlags.None)
End Sub
Sub GUIConnect()
StatusLab.Text = "Connected"
listenBtn.Enabled = True
listenBtn.Text = "Disconnect"
End Sub
Sub GUIDisconnect()
StatusLab.Text = "Disconnected"
listenBtn.Enabled = True
listenBtn.Text = "Listen"
End Sub
Sub GUIListening()
StatusLab.Text = "Listening"
listenBtn.Enabled = False
listenBtn.Text = "Listening"
End Sub
Private Sub listenBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listenBtn.Click
Dim cmd As String
cmd = listenBtn.Text
If (cmd = "Listen") Then
Listen()
ElseIf (cmd = "Disconnect") Then
Disconnect()
End If
End Sub
Private Sub sendBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendBtn.Click
Send(sendTxt.Text)
Dim tstr As String
tstr = "Me: " & sendTxt.Text
msgsBox.Items.Add(tstr)
sendTxt.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filep As New OpenFileDialog
If filep.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = filep.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim addr As String
addr = TextBox1.Text
Dim img As Image
Dim b_arr As Byte()
Try
img = Image.FromFile(addr)
Dim s As String
s = ""
s += img.Height & vbNewLine
s += img.Width & vbNewLine
MessageBox.Show(s, "Height+Width")
b_arr = itoa(img)
Dim img2 As Image
img2 = atoi(b_arr)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Function itoa(ByVal img As Image) As Array
Dim strm As MemoryStream = New MemoryStream()
img.Save(strm, System.Drawing.Imaging.ImageFormat.Jpeg)
strm.Close()
Dim b_arr As Byte()
b_arr = strm.ToArray()
strm.Dispose()
Return b_arr
End Function
Public Function atoi(ByRef b_arr() As Byte) As Image
Dim strm As MemoryStream
Dim img As Image
strm = New MemoryStream(b_arr)
img = Image.FromStream(strm)
img.Save("pink")
Return (img)
End Function
End Class
RECEIVER(Client):
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Public Class Client
Private Const EXIT_MSG As String = "{BYEBYE}"
Dim sock As Socket
Dim trd As Thread
Sub Connect()
Dim ip As String = ipTxt.Text
Dim port As Integer = CInt(portTxt.Text)
sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
sock.Connect(New IPEndPoint(IPAddress.Parse(ip), port))
GUIConnect()
trd = New Thread(AddressOf Read)
trd.Start()
End Sub
Sub Disconnect()
Send("{BYEBYE}")
Me.Invoke(New MethodInvoker(AddressOf GUIDisconnect))
If (Not (sock Is Nothing)) Then
sock.Close()
End If
End Sub
Sub Read()
While (True)
Try
Dim data(1023) As Byte
Dim re As Integer = sock.Receive(data, 0, data.Length, SocketFlags.None)
Array.Resize(data, re)
Dim msg As String = Encoding.Default.GetString(data)
If (msg = "{BYEBYE}") Then
Disconnect()
Return
End If
Dim tstr As String
tstr = "Server: " & msg
Me.Invoke(New MethodInvoker(Function() msgsBox.Items.Add(tstr)))
Catch ex As Exception
MsgBox(ex.Message)
Return
End Try
End While
End Sub
Sub Send(ByVal msg As String)
Try
Dim data() As Byte = Encoding.Default.GetBytes(msg)
sock.Send(data, 0, data.Length, SocketFlags.None)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub connectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connectBtn.Click
Dim cmd As String = connectBtn.Text
Try
If (cmd = "Connect") Then
Connect()
ElseIf (cmd = "Disconnect") Then
Disconnect()
End If
Catch ex As Exception
Disconnect()
MsgBox(ex.Message)
End Try
End Sub
Private Sub sendBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendBtn.Click
Send(sendTxt.Text)
Dim tstr As String
tstr = "Me: " & sendTxt.Text
msgsBox.Items.Add(tstr)
sendTxt.Text = ""
End Sub
Sub GUIConnect()
connectBtn.Text = "Disconnect"
StatusLab.Text = "Connected"
End Sub
Sub GUIDisconnect()
connectBtn.Text = "Connect"
StatusLab.Text = "Connected"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filedir As New OpenFileDialog
If filedir.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = filedir.FileName
End If
End Sub
End Class