Hello, I am new to this forum so I am sorry if this is posted in the wrong spot or anything but I was trying to send a file using winsock. My method was to first convert the file to a bytearray, send the bytearray, and then reconvert it to a file. I am using the newer winsock (2005). Here is my code, I was hoping someone might be able to figure out my error, because it won't send any bytes or it will only send part of the bytes but not the whole thing. The forum has 3 buttons, one to listen, one to connect, and one to send.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Winsock1.Listen()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Winsock2.Connect()
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal sender As Winsock_Control.Winsock, ByVal requestID As System.Net.Sockets.Socket) _
Handles Winsock1.ConnectionRequest
Winsock3.Accept(requestID)
End Sub
Dim inarray() As Byte
Private Sub Winsock3_DataArrival(ByVal sender As Winsock_Control.Winsock, ByVal BytesTotal As Integer) Handles Winsock3.DataArrival
'winsock3 = receiver
Winsock3.GetData(inarray)
bytearraytofile(inarray, "c:\test.jpg")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'winsock2 = sender
Dim filedialog As New OpenFileDialog
Dim out() As Byte
filedialog.ShowDialog()
out = filetobytearray(filedialog.FileName)
Winsock2.Send(out)
End Sub
Public Function filetobytearray(ByVal filename As String)
Dim infilestream As New IO.FileStream(filename, IO.FileMode.Open)
Dim inreader As New IO.BinaryReader(infilestream)
Dim infile() As Byte
infile = inreader.ReadBytes(CInt(infilestream.Length))
inreader.Close()
Return (infile)
End Function
Public Sub bytearraytofile(ByVal outfile() As Byte, ByVal filename As String)
Dim outfilestream As New IO.FileStream(filename, IO.FileMode.Open)
Dim i As Integer
i = 0
Do Until i = (outfile.Length - 1)
outfilestream.WriteByte(outfile(i))
i = i + 1
Loop
outfilestream.Close()
End Sub
Last edited by a moderator: