十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
用 vb.net socket通信
创新互联建站-专业网站定制、快速模板网站建设、高性价比泾源网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式泾源网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖泾源地区。费用合理售后完善,十年实体公司更值得信赖。
Dim th As Threading.Thread
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub
using System.Net;
if(!string.IsNullOrEmpty(txtPort.Text))
{
IPAddress ip = IPAddress.Parse(txtIp.Text);
IPEndPoint point=new IPEndPoint(ip,int.Parse(txtPort.Text));
try
{
TcpClient tcp=new TcpClient();
tcp.Connect(point);
MessageBox.Show("端口打开");
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
通过以上这个方法,只能判断你是否能链接这个端口。
通过以下这个方法,能判断端口是否开放
public static void GetTcpConnections()
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
string str="";
foreach (TcpConnectionInformation t in connections)
{
str+="Local endpoint:"+ t.LocalEndPoint.ToString()+",";
str+="Remote endpoint:"+ t.RemoteEndPoint.ToString()+",";
str+=t.State.ToString()+",";
}
MessageBox.Show(str);
}
程序语言 Microsoft Visual Basic
运行平台 WINDOWS
功能描述 IPX和NETBIOS接口需要网络地址 该文通过详细的步骤演示了如何通过VB获取网卡地址 步骤
)在Visual Basic生成标准的EXE文件 缺省创建 Form
)在Form 中添加一命令按钮 缺省名为Command
)把下列代码放到Form 中说明部分
Option Explicit Private Const NCBASTAT = H Private Const NCBNAMSZ = Private Const HEAP_ZERO_MEMORY = H Private Const HEAP_GENERATE_EXCEPTIONS = H Private Const NCBRESET = H Private Type NCB ncb_mand As Byte Integer ncb_retcode As Byte Integer ncb_lsn As Byte Integer ncb_num As Byte Integer ncb_buffer As Long String ncb_length As Integer ncb_callname As String * NCBNAMSZ ncb_name As String * NCBNAMSZ ncb_rto As Byte Integer ncb_sto As Byte Integer ncb_post As Long ncb_lana_num As Byte Integer ncb_cmd_cplt As Byte Integer ncb_reserve( ) As Byte Reserved must be ncb_event As Long End Type Private Type ADAPTER_STATUS adapter_address( ) As Byte As String * rev_major As Byte Integer reserved As Byte Integer adapter_type As Byte Integer rev_minor As Byte Integer duration As Integer frmr_recv As Integer frmr_xmit As Integer iframe_recv_err As Integer xmit_aborts As Integer xmit_success As Long recv_success As Long iframe_xmit_err As Integer recv_buff_unavail As Integer t _timeouts As Integer ti_timeouts As Integer Reserved As Long free_ncbs As Integer max_cfg_ncbs As Integer max_ncbs As Integer xmit_buf_unavail As Integer max_dgram_size As Integer pending_sess As Integer max_cfg_sess As Integer max_sess As Integer max_sess_pkt_size As Integer name_count As Integer End Type Private Type NAME_BUFFER name As String * NCBNAMSZ name_num As Integer name_flags As Integer End Type Private Type ASTAT adapt As ADAPTER_STATUS NameBuff( ) As NAME_BUFFER End Type Private Declare Function Netbios Lib netapi dll (pncb As NCB) As Byte Private Declare Sub CopyMemory Lib kernel Alias RtlMoveMemory _(hpvDest As Any ByVal hpvSource As Long ByVal cbCopy As Long) Private Declare Function GetProcessHeap Lib kernel () As Long Private Declare Function HeapAlloc Lib kernel _ (ByVal hHeap As Long ByVal dwFlags As Long _ ByVal dwBytes As Long) As Long Private Declare Function HeapFree Lib kernel (ByVal hHeap As Long _ ByVal dwFlags As Long lpMem As Any) As Long
把下面的代码放入Command _Click的事件中
Private Sub Command _Click() Dim myNcb As NCB Dim bRet As Byte myNcb ncb_mand = NCBRESET bRet = Netbios(myNcb) myNcb ncb_mand = NCBASTAT myNcb ncb_lana_num = myNcb ncb_callname = * Dim myASTAT As ASTAT tempASTAT As ASTAT Dim pASTAT As Long myNcb ncb_length = Len(myASTAT) Debug Print Err LastDllError pASTAT = HeapAlloc(GetProcessHeap() HEAP_GENERATE_EXCEPTIONS _ Or HEAP_ZERO_MEMORY myNcb ncb_length) If pASTAT = Then Debug Print memory allcoation failed! Exit Sub End If myNcb ncb_buffer = pASTAT bRet = Netbios(myNcb) Debug Print Err LastDllError CopyMemory myASTAT myNcb ncb_buffer Len(myASTAT) MsgBox Hex(myASTAT adapt adapter_address( )) _ Hex(myASTAT adapt adapter_address( )) _ Hex(myASTAT adapt adapter_address( )) _ Hex(myASTAT adapt adapter_address( )) _ Hex(myASTAT adapt adapter_address( )) _ Hex(myASTAT adapt adapter_address( )) HeapFree GetProcessHeap() pASTAT End Sub
)按F 运行该程序
lishixinzhi/Article/program/net/201311/12061
关于串口通讯的问题:
很明显,你还不知道(不会、不习惯)使用事件驱动的方式接收数据。
建议你仔细看看串口组件(无论VB6还是VB.net)的OnComm事件,你的问题很容易解决。
关于以太网通讯:
在TCP通讯中端口确实可以重用,你百度一下“TCP端口重用”能查到很多示例。
但绝大多数情况下不推荐端口重用,而应该采取服务器端建立连接池的方法。
或者,干脆不用TCP,用UDP解决也可以。
DatagramSocket用于接收和发送UDP的Socket实例。该类有3个构造函数:DatagramSocket():通常用于客户端编程,它并没有特定监听的端口,仅仅使用一个临时的。程序会让操作系统分配一个可用的端口。DatagramSocket(int port):创建实例,并固定监听Port端口的报文。通常用于服务端。
有两种方法:
"Server=192.168.0.2,现在的端口;packet size=4096;Database=login;User ID=sa;Password=12345678;Trusted_Connection=False"
或:
"Server=192.168.0.2;Port=现在的端口;packet size=4096;Database=login;User ID=sa;Password=12345678;Trusted_Connection=False"