Using socket in VB. NET to implement a simple string sending and receiving program
Source: Internet
Author: User
First, check the msdn help system and find the socket class, which provides detailed instructions on creating a socket, using a socket to listen, and using a client socket. Open vs. NET 2003 and create a new VB. NET project named server. Add a list box and two buttons on the interface. Interface:
We use multithreading to implement
Import namespace first:
Imports system. net. Sockets
Imports system.net
Imports system. Text
Imports system. threading
Define two form variables
Dim s as socket = nothing
Dim t as thread
Create a new process to process received socket data packets
Public sub waitdata ()
S = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP) ''' use the TCP protocol
Dim localendpoint as new ipendpoint (IPaddress. parse ("127.0.0.1"), 1024) ''' specifies the IP address and port
S. BIND (localendpoint) ''' bind to this socket
S. Listen (100) ''' listens and can accept up to 100 connections
While (true)
Dim bytes (1024) as byte ''' is used to store received bytes.
Dim ss as socket = S. Accept () ''' if it is received, a new socket is created to connect to it.
SS. Receive (bytes) ''' receives data. If ss. Send (byte () is used, data is sent.
Listbox1.items. insert (0, encoding. Unicode. getstring (bytes) ''' before inserting it into the first item in the list box
''' If encoding. ASCII. getstring (bytes) is used, the received Chinese characters cannot be properly displayed.
End while
End sub
Add the following code to the click event of btnstart:
T = new thread (addressof waitdata) ''' creates a new thread.
T. Start () ''' start the thread
Btnstart. Enabled = false ''' button unavailable to avoid starting another thread
In the btnstop Click Event, add the following code:
Try
S. Close () ''' close socket
T. Abort () ''' abort the thread
Catch
Finally
Btnstart. Enabled = true ''' enable btnstart
End try
To prevent the user from directly exiting without clicking stop, rather than end the thread, add the following code in the closing event of the form:
Try
S. Close ()
T. Abort ()
Catch
End try to create a VB. NET project named client. Add a text box and a button to the interface. Interface: Import namespace first:
Imports system. net. Sockets
Imports system.net
Imports system. Text in the click event of btnsend, add the following code:
Try
Dim bytes (1024) as byte
Dim S = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP)
Dim localendpoint as new ipendpoint (IPaddress. parse ("127.0.0.1"), 1024)
S. Connect (localendpoint)
S. Send (encoding. Unicode. getbytes (textbox1.text ))
S. Close ()
Catch ex as exception end try this way, this small program is complete, of course, you can add new features to make it more powerful! Source code: Download
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.