整理的socket编程希望对大家有用

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
'这里创建一个UDP Socket连接的实例 TCP也是一样的
'省略SOCKET连接的代码 当SOCKET连接成功后 可以用下面的语句获取远程信息
Dim IP as string ,Port as integer
'定义IP和Port变量 用来保存地址及端口
IP = IPAddress.Parse(CType(REV_SCK.RemoteEndPoint, IPEndPoint).Address.ToString())
'WaitData写完后 建立新线程去调用它
Dim REV_THREAD As Thread '''REV_THREAD线程全局变量
REV_THREAD = New Thread(AddressOf WaitData) '''线程委托
REV_THREAD.Start() '''线程启动
Me.RichTextBox1.Clear()
Catch ex As Exception
MessageBox.Show(ex.Message, "信息提示 ", MessageBoxButtons.OK, rmation)
&&&&&&&&&&&&&&&&&&&&&&&&&
TextBox1 为要连接的ip
TextBox2 为呢称
RichTextBox1 为聊天内容
RichTextBox2 为要发送的内容
Button1 发送
Imports System.Threadime As String = Dns.GetHostName
Dim ip As IPAddress = Dns.GetHostByName(hostName).AddressList.GetValue(0) '''用2次Dns.GetHostName获得本机电脑的IP号
MyTcpListener = New TcpListener(888)
'开始监听
MyTcpListener.Start()
'While (True)
'获取TcpClient
Dim MyTcpClient As TcpClient = MyTcpListener.AcceptTcpClient()
DimsendsocketAsNewNet.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork,Net.Sockets.SocketType.Stream,Net.Sockets.ProtocolType.Tcp) ''实例化socket
Port = IPAddress.Parse(CType(REV_SCK.RemoteEndPoint, IPEndPoint).Port)
以Udp做为例子吧.
接收数据需要一个Socket实例
Dim REV_SCK As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) '''UDP方式Socket实例
End Try
End Sub
End Class
*********************************************
发送端
Public Class Form1
InheritsSystem.Windows.Forms.Form
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
Try
REV_SCK.Bind(New IPEndPoint(ip, port)) '''Bind绑定。 IPEndPoint通过IP/Port确定1个连接端点
Catch ex As Exception
MsgBox(ex.Message)
'这里写出错代码
End Try
Dim MyStream As NetworkStream = MyTcpClient.GetStream()
Dim MyBytes(1024) As Byte
Dim MyBytesRead As Integer = MyStream.Read(MyBytes, 0, MyBytes.Length)
End If
Exit Sub
Try
Dim MyMessage As String = "消息来自( " + Me.TextBox2.Text + "): " + Me.RichTextBox1.Text + Chr(10) + Chr(13)
'根据目标计算机地址建立连接
'将缓冲数据写入基础流
MyStream.Flush()
'关闭网络流
MyStream.Close()
'MyTcpClient.Close()
Me.RichTextBox2.AppendText( "发送: " & Me.RichTextBox1.Text)
Socket类连接后 可以通过类中的 RemoteEndPoint 来获取远程IP和端口信息.
要注意的是.需要将它强制转化为 IPEndPoint类型 然后通过 IPEndPoint.Address以及IPEndPoint.Port来获取对应的IP及端口
Dim REV_SCK As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
MessageBox.Show(ex.Message, "信息提示 ", MessageBoxButtons.OK, rmation)
End Try
e.Cancel = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Imports .Sockets
Public Class Form1
Dim MyTcpListener As TcpListener
Dim MyListenThread As Thread
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'End While
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (Me.TextBox1.Text.Length < 1 Or Me.TextBox2.Text.Length < 1 Or Me.RichTextBox1.Text.Length < 1) Then
'关闭SOCKET
Try
If Me.MyTcpListener IsNot Nothing Then
'关闭监听器
Me.MyTcpListener.Stop()
End If
If Me.MyListenThread IsNot Nothing Then
Dim MyMessage As String = System.Text.UnicodeEncoding.GetEncoding( "gb2312 ").GetString(MyBytes, 0, MyBytesRead)
Me.RichTextBox2.AppendText(MyMessage)
For i = 0 To 1024
If bytes(i) = 0 Then Exit For
Next
REV_STR = Encoding.UTF8.GetString(bytes, 0, i)
End While
'上面的代码中 REV_STR是一个全局变量.然后读取REV_STR的值 就可以获取接收的信息
' 开始监听()
'创建监听线程
Me.MyListenThread = New Thread(AddressOf StartListen)
'启动线程
Me.MyListenThread.Start()
End Sub
Private Sub StartListen()
'如果线程还处于运行状态就关闭它
If Me.MyListenThread.ThreadState <> ThreadState.Running Then
Me.MyListenThread.Abort()
End If
Catch ex As Exception
'接收信息的方法是写一个获取接收信息的函数,然后委托一个新线程去调用它
'WaitData表示一个循环接收信息的函数 代码如下
While True
Dim bytes(1024) As Byte '用来存储接收到的字节
REV_SCK.Receive(bytes)
Dim i As Integer
Dim MyStream As IO.StreamWriter = New IO.StreamWriter(MyTcpStream, System.Text.UnicodeEncoding.GetEncoding( "gb2312 "))
'将字符串写入流
MyStream.Write(MyMessage)
Dim MyTcpClient As TcpClient = New TcpClient(Me.TextBox1.Text, 888)
'获得用于网络访问的数据流
Dim MyTcpStream As workStream = MyTcpClient.GetStream()
DimipendpiontAsNewNet.IPEndPoint(Net.IPAddress.Parse("192.168.0.1"),8888)''建立终结点
''OpenFileDialog1.ShowDialog()
DimfsAsNewIO.FileStream("c:p.doc",IO.FileMode.OpenOrCreate,IO.FileAccess.Read)''要传输的文件
Dimfssize(fs.Length-1)AsByte
DimstrreadAsNewIO.BinaryReader(fs)''流处理要传输的文件
''fs.Read(fssize,0,fssize.Length-1)
相关文档
最新文档