|
-
Feb 22nd, 2011, 06:06 AM
#1
Thread Starter
Member
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.
-
Feb 22nd, 2011, 08:58 AM
#2
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.
-
Feb 22nd, 2011, 10:21 AM
#3
Thread Starter
Member
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.
-
Feb 22nd, 2011, 10:23 AM
#4
Thread Starter
Member
Re: Help with sockets or winsock
Last edited by vagelisr; Feb 22nd, 2011 at 10:27 AM.
-
Feb 22nd, 2011, 10:35 AM
#5
Frenzied Member
Re: Help with sockets or winsock
On the server side:
vb Code:
Dim listener as new TCPListener(IPAddress.Parse("111.111.111.111"),1000)
listener.start()
Dim client as TCPClient = listener.AcceptTCPClient()
Console.WriteLine("Client Connected!")
Console.ReadLine()
On the client side:
vb Code:
Dim client as TCPClient
client.connect(IPAddress.Parse("111.111.111.111"),1000)
if client.connected then
Messagebox.show("client connected successfully!")
end if
That's as simple as it gets : ).
Justin
-
Feb 22nd, 2011, 11:10 AM
#6
Thread Starter
Member
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
-
Feb 22nd, 2011, 01:48 PM
#7
Frenzied Member
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:
'buffer containing data to be sent over the socket. Dim buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes("!V32 1" & vbCrLf) 'send the data over the socket to the defined EndPoint. (host,port) soc.send(buffer,buffer.Length,0) 'define byte() to hold received contents. Dim RBytes(256) as Byte 'receive data into a byte() soc.Receive(RBytes,RBytes.Length,0) 'the message received if anything is sent back. Converted from Byte() to string for use. Dim receiveStr as string = System.Text.ASCIIEncoding.ASCII.GetString(RBytes)
Justin
-
Feb 22nd, 2011, 02:52 PM
#8
Thread Starter
Member
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.
-
Feb 22nd, 2011, 02:58 PM
#9
Frenzied Member
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|