怎样用VB.net实现基于tcp-ip的点对点通信?

MissileCat 2003-09-22 08:46:45
在vb6中实现TCP IP的通信可以用windowzs socket控件,但在vb.net中我该怎么办,而且我希望写的通信程序可以在双方有网关及防火墙的情况下还可以工作正常,请请点一二,最好有一点源程序啦8-)
...全文
141 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MissileCat 2003-09-23
  • 打赏
  • 举报
回复
jary12581(狼) ,谢谢啊
jary12581 2003-09-22
  • 打赏
  • 举报
回复
用套接字:
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
LongBow007 2003-09-22
  • 打赏
  • 举报
回复
做个标记 看看是大家是怎么样绕过防火墙的
mainone 2003-09-22
  • 打赏
  • 举报
回复
学习

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧