-
用VB.net实现基于tcp-ip的点对点通信_ITPUB博客
Imports System.Net
Imports System.Net.Sockets
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Text = ""
Me.TextBox2.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sclient As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim myip As IPAddress 'set server ipaddress
myip = IPAddress.Parse("172.19.3.42")
Dim serverport As New IPEndPoint(myip, 8000) 'set server ipport
Dim clientport As New IPEndPoint(myip, 2002) 'set client port
sclient.Bind(clientport) 'bind client port with socket
Try
Dim str As String = Me.TextBox2.Text
Dim bytes1() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
sclient.SendTo(bytes1, serverport)
Dim bytes2(100) As Byte
sclient.ReceiveFrom(bytes2, serverport)
str = System.Text.Encoding.ASCII.GetString(bytes2)
Me.TextBox1.Text = Me.TextBox1.Text & vbCrLf & str
Catch er As SocketException
MsgBox(er.ToString)
End Try
sclient.Close()
End Sub
*************************************
Imports System.Net.Sockets
Imports System.Net
Module socketsever
Sub main()
startsever() 'set main to start form
End Sub
Private Sub startsever() 'set startsever processor to start server
Dim ssever As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) 'set a socket example to transfer information between two computer
Dim myip As IPAddress 'set server ipaddress
myip = IPAddress.Parse("172.19.3.42")
Dim serverport As New IPEndPoint(myip, 8000) 'set server port
Dim clientport As New IPEndPoint(myip, 2002) 'set client port
ssever.Bind(serverport) 'bind severport with the socket
Try
Dim bytes(100) As Byte 'set currenty of bytes(设置字节流量,并将其存储到一个字节数组中)
ssever.ReceiveFrom(bytes, clientport) '服务器端接受信息
Dim str As String
str = System.Text.Encoding.ASCII.GetString(bytes) '将字节转换为字符处理
str = str.ToUpper() '转换为大写字母
bytes = System.Text.Encoding.ASCII.GetBytes(str) '将字节转换为字符处理
ssever.SendTo(bytes, clientport) '向客户端端口发送数据
Catch e As SocketException
End Try
End Sub
End Module
Imports System.Net.Sockets
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Text = ""
Me.TextBox2.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sclient As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim myip As IPAddress 'set server ipaddress
myip = IPAddress.Parse("172.19.3.42")
Dim serverport As New IPEndPoint(myip, 8000) 'set server ipport
Dim clientport As New IPEndPoint(myip, 2002) 'set client port
sclient.Bind(clientport) 'bind client port with socket
Try
Dim str As String = Me.TextBox2.Text
Dim bytes1() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
sclient.SendTo(bytes1, serverport)
Dim bytes2(100) As Byte
sclient.ReceiveFrom(bytes2, serverport)
str = System.Text.Encoding.ASCII.GetString(bytes2)
Me.TextBox1.Text = Me.TextBox1.Text & vbCrLf & str
Catch er As SocketException
MsgBox(er.ToString)
End Try
sclient.Close()
End Sub
*************************************
Imports System.Net.Sockets
Imports System.Net
Module socketsever
Sub main()
startsever() 'set main to start form
End Sub
Private Sub startsever() 'set startsever processor to start server
Dim ssever As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) 'set a socket example to transfer information between two computer
Dim myip As IPAddress 'set server ipaddress
myip = IPAddress.Parse("172.19.3.42")
Dim serverport As New IPEndPoint(myip, 8000) 'set server port
Dim clientport As New IPEndPoint(myip, 2002) 'set client port
ssever.Bind(serverport) 'bind severport with the socket
Try
Dim bytes(100) As Byte 'set currenty of bytes(设置字节流量,并将其存储到一个字节数组中)
ssever.ReceiveFrom(bytes, clientport) '服务器端接受信息
Dim str As String
str = System.Text.Encoding.ASCII.GetString(bytes) '将字节转换为字符处理
str = str.ToUpper() '转换为大写字母
bytes = System.Text.Encoding.ASCII.GetBytes(str) '将字节转换为字符处理
ssever.SendTo(bytes, clientport) '向客户端端口发送数据
Catch e As SocketException
End Try
End Sub
End Module
栏目列表
最新更新
配置文件中的数据库连接串加密了,你以
async await 异步下载 异步代码加锁 lock 异步
设计模式之访问者模式
C#泛型
[VB.NET]TCP/IP协议编程(简单SOCKTE编程)
请教有关vb.net中TCP编程的问题
vb.net TCP/IP Socket 服务端接收数据问题
优化VB.NET中的TCP / IP套接字实现
用VB.net实现基于tcp-ip的点对点通信_ITPUB博
CRC常用参数模型及C#代码实现
用 Access+Outlook 来采集信息
使用PowerDesigner生成Access数据库
让我们一起用开源数据库和开源框架废弃
随说秋色园从Access升迁到MSSQL过程
当爬虫被拒绝时(Access Denied)
Web API与OAuth:既生access token,何生refres
[认证 & 授权] 6. Permission Based Access Co
Access之C#连接Access
oracle 19c下载和安装教程(database和client)
MySql索引原理与使用大全
php的计数器每次都会清零
PHP基础
数据类型之对象
数据类型之布尔型、整型、浮点型和字符
php教程之数据类型之数组
php教程之PHP 常量
php教程之变量
php教程之语法
PHP简介与安装
phpMyAdmin配置安装全攻略