Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
Posted 19-Dec-12 0:00am
Edited 19-Dec-12 0:25am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Have a look here:
A TCP/IP Chat Program[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

[This post is not an answer and should be removed — SA]
 
Thank you all very much.. and sorry to reply this late.. Will check the links right away.. thank you Smile | :)
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 PIEBALDconsult 110
3 Richard MacCutchan 105
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100