Results 1 to 9 of 9

Thread: Help with sockets or winsock

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Smile Help with sockets or winsock

    Hi to all
    I want to connect with a barcode printer and send him some specific characters and take back anything they send to me

    In VB6 i have use the winsock and (for me) the things is simple....

    Code:
    Winsock1(totlistsite_w).RemoteHost = ListView1.ListItems(totlistsite_w).SubItems(1)
    Winsock1(totlistsite_w).RemotePort = ListView1.ListItems(totlistsite_w).SubItems(2)
    Winsock1(totlistsite_w).Connect
    Do    
      txtCounter = ""    
      If Return_Status.Enabled = False Then Exit Do    
      If ListView1.ListItems(totlistsite_w).ListSubItems(6) = "" Then Exit Do    
      DoEvents    
        If Winsock1(totlistsite_w).State = 7 Then        
        Winsock1(totlistsite_w).SendData "!V32 1" & vbCrLf        
        txtCounter = ReadString1(Winsock1(totlistsite_w))    
      End If
    Loop Until Winsock1(totlistsite_w).State = 7 Or Winsock1(totlistsite_w).State = 9
    Winsock1(totlistsite_w).Close
    Please could you help me to make something similar with vb.net.
    What did you sugest to use sockets or winsock??
    An example it will be much acceptable!!!

    Thanks and regards.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with sockets or winsock

    Use the System.Net.Sockets.Socket class from the .NET Framework. Lots of examples around if you care to search. You may even be able to get away with using a TcpClient, which is simpler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Help with sockets or winsock

    jmcilhinney thanks for your time.
    I was wondering if you are able to write a simple example with a socket connection using an ip address and port and how i can send and receive data thru socket.
    Believe me i have read some examples but all of them is for Server and client and confuse me more…….

    Thanks again for your time and hope to help me again with 5 or 10 lines of code.

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Help with sockets or winsock

    .....
    Last edited by vagelisr; Feb 22nd, 2011 at 10:27 AM.

  5. #5
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Help with sockets or winsock

    On the server side:

    vb Code:
    1. Dim listener as new TCPListener(IPAddress.Parse("111.111.111.111"),1000)
    2. listener.start()
    3.  
    4. Dim client as TCPClient = listener.AcceptTCPClient()
    5.  
    6. Console.WriteLine("Client Connected!")
    7.  
    8. Console.ReadLine()

    On the client side:

    vb Code:
    1. Dim client as TCPClient
    2.  
    3. client.connect(IPAddress.Parse("111.111.111.111"),1000)
    4.  
    5. if client.connected then
    6.  
    7.     Messagebox.show("client connected successfully!")
    8.  
    9. end if

    That's as simple as it gets : ).

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Help with sockets or winsock

    Thanks a lot MonkOFox for your time and for your code BUT.....
    In my case i dont have client-server. See my first post with code from VB6
    I only want to sent in printer some characters (in my case "!V32 1") and after this to receive his answer....

    Please give me an example for my case!!!!!!!!!!!!!!!!!!!!!!!!!

    Thanks and regards

  7. #7
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Help with sockets or winsock

    Oh sorry about that... Well just do what J said and use the new sub of the Socket class.

    I don't know what addressfamily, sockettype or protocoltype you'd use, but I'm sure you can figure that out.

    Then you can call the Socket.Connect(host as string, port as integer) to connect it.

    There are send and receive subs too.

    vb Code:
    1. 'buffer containing data to be sent over the socket.
    2. Dim buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes("!V32 1" & vbCrLf)
    3.  
    4. 'send the data over the socket to the defined EndPoint. (host,port)
    5. soc.send(buffer,buffer.Length,0)
    6.  
    7. 'define byte() to hold received contents.
    8. Dim RBytes(256) as Byte
    9.  
    10. 'receive data into a byte()
    11. soc.Receive(RBytes,RBytes.Length,0)
    12.  
    13. 'the message received if anything is sent back.  Converted from Byte() to string for use.
    14. Dim receiveStr as string = System.Text.ASCIIEncoding.ASCII.GetString(RBytes)

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Help with sockets or winsock

    Thank you very much MonkOFox.
    I'm sorry for delaying my answer.
    Tomorrow I will try your last post.

    I don't know what addressfamily, sockettype or protocoltype you'd use, but I'm sure you can figure that out.
    For the connection to winsock i use 192.168.1.61 (IP Address) and 23 (port)


    One last question.
    How I will make the connect with the socket??
    Is the connection for client or for server or none of them??
    If none how I will make the connection???

    I wish some day to help you as much as you help me now.
    Last edited by vagelisr; Feb 22nd, 2011 at 02:59 PM.

  9. #9
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Help with sockets or winsock

    If you're trying to connect to a printer and send it messages, once you connect via socket it should be listening for messages and send something back if you send a valid message.

    That being said, I've never used socket to connect to a device before. I've used the twain API for a scanner once...

    Good luck,

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width