Winsock

Posts 1–11 of 11 · Page 1 of 1
Winsock
hello mpgh

can anyone tell me how to send a picture over winsock?
its for a client/server application

thans
You're best off just sending the info via a HttpWebRequest to a handling PHP page. Wrapper classes are there for a reason
Quote Originally Posted by Jason View Post
You're best off just sending the info via a HttpWebRequest to a handling PHP page. Wrapper classes are there for a reason
thanks, but i want to make it offline
Quote Originally Posted by Kudi View Post
thanks, but i want to make it offline
Well that's impossible lol. Sockets are very much online (unless operating across a LAN I guess).
is there any way to send a picture offline?
Quote Originally Posted by Kudi View Post
is there any way to send a picture offline?
... No. You need to be connected to the internet to send something to another computer through the internet. :/
no unless the other pc is on lan with u :O
Quote Originally Posted by AndrewxXx View Post
no unless the other pc is on lan with u :O
yes, the other pc is in the lan
Look up how to transfer files across a LAN then. For anyone interested here's my drafted ImageUploader class

[highlight=vb.net]
Public Class ImageUploader
Private _maxsize As UInt32 = 1024 * 1024 'default 1KB limit
Private _uploadpage As String = String.Empty 'Webpage that handles the upload request

Public Property SizeLimit() As UInt32
Get
Return Me._maxsize
End Get
Set(ByVal value As UInt32)
Me._maxsize = value
End Set
End Property

Public Property UploadPage() As String
Get
Return Me._uploadpage
End Get
Set(ByVal value As String)
Me._uploadpage = value
End Set
End Property

Public Sub New(ByVal uploadPage As String, Optional ByVal maxfilesize As UInt32 = 1024 * 1024)
Me.UploadPage = uploadPage
Me.SizeLimit = maxfilesize
End Sub

Public Function UploadImage(ByVal imgpath As String)
Return UploadImage(imgpath, System.Text.Encoding.UTF8)
End Function

Public Function UploadImage(ByVal imgpath As String, ByVal encoding As System.Text.Encoding) As String
Dim response As String = String.Empty
If Not IsURLValid(Me.UploadPage) OrElse Not IsImageValid(imgpath) Then Return response 'make sure the URL is valid, and the Image is valid before going further.
Using wClient As New WebClient()
Try
Dim respBytes As Byte() = wClient.UploadFile(Me.UploadPage, imgpath) 'get the servers response to the upload request (in bytes)
response = encoding.GetString(respBytes) 'convert the bytes to string with the given encoding.
Catch ex As WebException
response = String.Empty 'reset the response.
End Try
End Using
Return response
End Function

Private Function IsImageValid(ByVal imgfile As String) As Boolean
If String.IsNullOrEmpty(imgfile) OrElse Not IO.File.Exists(imgfile) Then Return False
Dim imgSize As UInt32 = CType(New IO.FileInfo(imgfile).Length, UInt32)
If imgSize > Me.SizeLimit Then Return False 'validate the image size to make sure it's not too large.
Try
Using img As Image = Image.FromFile(imgfile) : End Using 'attempt to open and close an image stream
Return True 'image is valid if no exception was thrown
Catch mem As OutOfMemoryException
Return False 'OutOfMemoryException is thrown by Image.FromFile if the image was invalid.
End Try
End Function

Private Function IsURLValid(ByVal url As String) As Boolean
If String.IsNullOrEmpty(url) Then Return False
Return Uri.TryCreate(url, UriKind.Absolute, Nothing)
End Function
End Class
[/highlight]

To use:

[highlight=vb.net]
Dim iUploader As New ImageUploader("http://www.example.com/upload.php", (1024 * 1024) * 5) 'new uploader with a 5KB size limit.
Dim pageResponse As String = iUploader.UploadImage("C:\users\jason\img001.jpg") 'retrieve the page response.
If String.IsNullOrEmpty(pageResponse) Then
MessageBox.Show("Upload failed")
Else
MessageBox.Show("You upload request went through")
End If
[/highlight]
Quote Originally Posted by Jason View Post
Look up how to transfer files across a LAN then. For anyone interested here's my drafted ImageUploader class

[highlight=vb.net]
Public Class ImageUploader
Private _maxsize As UInt32 = 1024 * 1024 'default 1KB limit
Private _uploadpage As String = String.Empty 'Webpage that handles the upload request

Public Property SizeLimit() As UInt32
Get
Return Me._maxsize
End Get
Set(ByVal value As UInt32)
Me._maxsize = value
End Set
End Property

Public Property UploadPage() As String
Get
Return Me._uploadpage
End Get
Set(ByVal value As String)
Me._uploadpage = value
End Set
End Property

Public Sub New(ByVal uploadPage As String, Optional ByVal maxfilesize As UInt32 = 1024 * 1024)
Me.UploadPage = uploadPage
Me.SizeLimit = maxfilesize
End Sub

Public Function UploadImage(ByVal imgpath As String)
Return UploadImage(imgpath, System.Text.Encoding.UTF8)
End Function

Public Function UploadImage(ByVal imgpath As String, ByVal encoding As System.Text.Encoding) As String
Dim response As String = String.Empty
If Not IsURLValid(Me.UploadPage) OrElse Not IsImageValid(imgpath) Then Return response 'make sure the URL is valid, and the Image is valid before going further.
Using wClient As New WebClient()
Try
Dim respBytes As Byte() = wClient.UploadFile(Me.UploadPage, imgpath) 'get the servers response to the upload request (in bytes)
response = encoding.GetString(respBytes) 'convert the bytes to string with the given encoding.
Catch ex As WebException
response = String.Empty 'reset the response.
End Try
End Using
Return response
End Function

Private Function IsImageValid(ByVal imgfile As String) As Boolean
If String.IsNullOrEmpty(imgfile) OrElse Not IO.File.Exists(imgfile) Then Return False
Dim imgSize As UInt32 = CType(New IO.FileInfo(imgfile).Length, UInt32)
If imgSize > Me.SizeLimit Then Return False 'validate the image size to make sure it's not too large.
Try
Using img As Image = Image.FromFile(imgfile) : End Using 'attempt to open and close an image stream
Return True 'image is valid if no exception was thrown
Catch mem As OutOfMemoryException
Return False 'OutOfMemoryException is thrown by Image.FromFile if the image was invalid.
End Try
End Function

Private Function IsURLValid(ByVal url As String) As Boolean
If String.IsNullOrEmpty(url) Then Return False
Return Uri.TryCreate(url, UriKind.Absolute, Nothing)
End Function
End Class
[/highlight]

To use:

[highlight=vb.net]
Dim iUploader As New ImageUploader("http://www.example.com/upload.php", (1024 * 1024) * 5) 'new uploader with a 5KB size limit.
Dim pageResponse As String = iUploader.UploadImage("C:\users\jason\img001.jpg") 'retrieve the page response.
If String.IsNullOrEmpty(pageResponse) Then
MessageBox.Show("Upload failed")
Else
MessageBox.Show("You upload request went through")
End If
[/highlight]
is this now for lan?
Posts 1–11 of 11 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?