需要有关如何在两个应用程序之间发送文件的帮助 [英] Need help in how to send a file between 2 applications
本文介绍了需要有关如何在两个应用程序之间发送文件的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个在两台计算机之间进行聊天应用的任务。
我做了双方''关于简单文本聊天的应用程序和下一步所需的是发送文件(特别是图像)。
如何发送和接收文件?我看到了很多方法,现在感觉很模糊。
这是我双方的代码:
SENDER(服务器):
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(客户):
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
推荐答案
看看这里:
TCP / IP聊天程序 [ ^ ]
虽然它在C#中,但我认为你可以做到VB.net ...
小文件从服务器转移到客户端 [ ^ ]
另见..
http://www.vbforums.com/showthread.php?390875-Client-Server-Socket-classes-for-file-transfer [ ^ ]
Though it is in C# but i think you can do it for VB.net...
Small File Transfer from Server to Client[^]
Also see..
http://www.vbforums.com/showthread.php?390875-Client-Server-Socket-classes-for-file-transfer[^]
[这篇文章不是答案,应该删除 - SA]
非常感谢你们...很抱歉回复这个问题..请立即查看链接..谢谢:)
[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 :)
这篇关于需要有关如何在两个应用程序之间发送文件的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文