Jump to content
xisto Community

JimiHex

Members
  • Content Count

    23
  • Joined

  • Last visited

Everything posted by JimiHex

  1. This tutorial shows how to transfer file of any size using winsock control. - Open VB; - Select standard exe; - Press Ctrl + t to show the add component window; - Select winsock control and microsoft common dialog; - Add one winsock control in the project; - Name it winsock1; - If you want to add chat then add another winsock and name it winsock2; - Insert another winsock object if you want to add chat also; - Add a microsoft common dialog box; - Name it cd; - We will use this winsock1 object to transfer the file and winsock2 for chat; ------------- The basic idea : To send a file of any size to any ip using winsock first we have to open the file in binary mode. Then get chunks of data from it, chunk is a constant which is initialized to 8192, so we get 8192 bytes of data each time and send it using winsock to the client. for example let "fname" be the string variable containg the file name then : ------------- Private Const chunk = 8192Dim fname As String 'get the name of the fileDim dataOpen fname For Binary As #1Do While Not EOF(1)data = Input(chunk, #1)winsock1.SenData dataDoEventsLoop------------- this will send 8192 bytes of data from the file until the file ends. * But before sending data from file to client we must send info about the file like..the name of the file...the extension...etc.. So when send is clicked first check wether a file is there i mean check wether something is typed in the text box and if yes check wether the file exists. If both the above conditions are met then get the filename with the extension. send the file name to the client with "rqst" in front. for eg. if the name of file is "text.txt" then send "rqsttext.txt" to the client. The client will then get the file name and display a MsgBox with the name of the file and the user will be given a choice wether to accept the file or not if he\she selects yes then the client sends "okay" to the server and if he\she selects no then it sends "deny" to the server..this data i.e. "okay" or '"deny" arrivers on winsock1's local port the data is then checked using select case if its okay then "send" function is called with file address as an argument and send button and all buttons and text boxes associated with send file are disabled. If the response from client is "deny" then a msgbox is shown on server saying that the request to send the file .... as been denied..the user can send another request..or ask the client's user to accept the file using the chat module... - This is called when send is clicked ------------- Private Sub send_Click()'GET FILE NAME'using getfilename function to get the file nameDim fnamea As StringDim fname As StringIf text1.Text = "" ThenMsgBox "Please type the file name!!!",Exit SubEnd Iffname = text1.Text 'checking wether the file existsIf Dir(fname) = "" ThenMsgBox "File Does not exist Exists"Exit Sub 'exiting sub it file does not existsEnd Iffnamea = GetFileName(text1.Text)fname = text2.TextDim temp As Stringtemp = "rqst" & fnamea'SENDwinsock1.SendData temp 'sending file name of fileEnd Sub 'now the request is sent to the client'then the server has to wait for the client's response' this event is called when data arrives on winsock1Private Sub winsock1_dataarrival(ByVal bytestotal As Long)Dim response As Stringwinsock1.GetData response, vbStringSelect Case responseCase "okay"send fname 'send function is called with file name as argumentCase "deny"MsgBox "Your request to send the file " & fname & " has been denied", 'message when request is deniedEnd SelectEnd Sub' The send function which actally sends the filePrivate Sub send(fname As String)Command2.Enabled = FalseCommand3.Enabled = Falsetext1.Enabled = FalseDim data As StringDim a As LongDim data1 As StringDim data2 As StringOpen fname For Binary As #1Do While Not EOF(1)data = Input(chunk, #1)winsock1.SendData dataDoEventsLoopwinsock1.SendData "EnDf"Close #1Command2.Enabled = TrueCommand3.Enabled = Truetext1.Enabled = TrueEnd Sub'Other supporting functions: Function GetFileName(attach_str As String) As String Dim s As Integer Dim temp As String s = InStr(1, attach_str, "\") temp = attach_str Do While s > 0 temp = Mid(temp, s + 1, Len(temp)) s = InStr(1, temp, "\") Loop GetFileName = tempEnd Function------------- On the client side : set winsock1 to listen to a particular port say : 165 and winsock2 if you want chat too :166 winsock1 is listening to port 165 and winsock2 is listening to port 166 on the client side so when connection request arrives : Private Sub winsock1_ConnectionRequest(ByVal requestID As Long)If winsock1.State <> sckConnected Thenwinsock1.Closewinsock1.Accept requestIDEnd IfEnd Suband:Private Sub winsock2_ConnectionRequest(ByVal requestID As Long)If winsock2.State <> sckConnected Thenwinsock2.Closewinsock2.Accept requestIDEnd IfEnd Sub DATA ARRIVAL: and when data arrives ------------- Private Sub winsock1_DataArrival(ByVal bytestotal As Long)Dim data As StringDim data4 As StringDim data2 As StringDim data3 As StringDim data5 As StringDim data6 As StringWinsock1.GetData data, vbStringdata2 = Left(data, 4)Select Case data2Case "rqst" 'file request arrivesdata3 = Right(data, Len(data) - (4)) 'Get the file nameDim msg1 As Integer 'Stores user's selectionmsg1 = MsgBox(Winsock1.RemoteHost & " wants to send you file " & data3 & " accept ? ", vbYesNo) 'msgbox displayedIf msg1 = 6 Then 'if user selects yesWinsock1.SendData "okay"cd.FileName = data3data5 = Split(data3, ".")(1)data6 = "*." & data5cd.DefaultExt = "(data6)"data4 = App.Path & "\" & data3MsgBox data5cd.ShowSaveOpen data4 For Binary As #1Elsewinsock1.SendData "deny"Exit SubEnd IfCase "EnDf"Label1.Caption = "File revieved.Size of file : " & sz & " Kb"size = 0sz = 0Close #1Case Elsesize = size + 1Label1.Caption = size * 8 & "Kb Recieved"sz = size * 8Put #1, , dataEnd SelectEnd Sub------------- This will take care of file transfer now for the chat: we will be using winsock2 for chat: On server side : WHEN SEND IS CLICKED ------------- Private Sub Command1_Click()Dim chat As Stringchat = text1.TextList1.AddItem (chat)winsock2.SendData chatEnd Sub' when data arrivesPrivate Sub winsock2_DataArrival(ByVal bytestotal As Long)Dim cht As StringWinsock2.GetData cht, vbStringList1.AddItem (cht)End Sub ------------- the same will be on the client side also... ................
  2. there is one good book"Art Of Assembly Language"beautifull book
  3. hello...I am using Linux not too long, but I can tell youlinux is the most beautifull OS for beginerstry with UBUNTU distribution, it's 100% free http://forums.xisto.com/no_longer_exists/;
  4. in my opinionSATAN(Security Administrator Tool for Analyzing Networks)is probably the best
  5. and what is all story about?
  6. JimiHex

    Vb Samples

    hehe...very smart
  7. I vote for1stPage 2000 by Evrsoftvery good program with a lot of JavaScript sources
  8. I have read something about IPv6, and I don't understand some things:what about IPv4?when IPv6 will be in use?thanks
  9. where did you find this? do you have some more tutorials like this one?thank you
  10. very usefullsomeone can learn a lot form this tutorialthank you
  11. just use GetAsyncKeyState API the syntax is Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
  12. )try to find some books about protocolsand then start reading, and reading, and readingand never stop...then you will see how that goes
  13. but,I still don't understandHOW TO SEND SMS?please help
  14. try to find something on http://www.allapi.net/
  15. Winsock.RemoteHost = "1.0.0.27"try with remote host 127.0.0.1that is localhost
  16. try with http://www.planet-source-code.com/
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.