主要开发环境是Visual Studio 2008,Visual Studio系列产品一直以来都提供了强大的控件功能,然而我们利用这些控件可以编写出功能强大的应用程序。本文主要利用微软的最新.net开发工具为大家展示一个发送与接收端程序的开发过程,让大家对Socket更加熟悉,很适合.net开发工具的初学者,具有一定的实用价值。  
  打开 Visual Studio 2008在文件 (File) 菜单上,单击新建项目 (New Project)。 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中,单击 Windows 应用程序(Windows Application)。单击确定 (OK) 如图1。 
  
                        
  我们需要创建两个窗体,一个是发送端(Form1窗体),另外一个是接收端(Form2窗体) 
  详细操作步骤如下: 
  选择Form1窗体,在Form1窗体中添加如下控件: 
  Label1、Label2、Label3、 Button1控件、OpenFileDialog1控件 
  控件属性设置如下:   
  Label1 属性Text 已经发送的文件长度: 
  Label2 属性Text 为空 
  Label3 属性Text 字节数 
  Button1 属性Text 发送文件   
  最终界面效果如下: 
  
                        
  选择Form2窗体,在Form2窗体中添加如下控件: 
  Label1、Label2、TextBox1 、Button1控件、OpenFileDialog1控件、SaveFileDialog1控件 
  控件属性设置如下: 
  Label1 属性Text 已接收的文件: 
  Label2 属性Text 为空 
  TextBox1 属性Multiline 
  属性ScrollBars True 
  Both 
  Button1 属性Text 接收文件并保存 
  最终界面效果如下: 
  
  
                        
  好了界面工作已经完成了,接下来我们需要输入代码了 
  选择Form1窗体,进入代码编辑器中 
  首先我们需要进行声明: 
  Imports System.Net.Sockets 
  Imports System.Net 
  Imports System.IO   
  Public Class Form1 
  Inherits System.Windows.Forms.Form   
  进入Button1_Click事件中 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
  Dim mysocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 
  '声明socket 
  Dim myipendponit As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888) '建立一个终结点 
  OpenFileDialog1.Filter = "文本文件(.txt) *.txt" 
  OpenFileDialog1.InitialDirectory = "c:\" 
  If OpenFileDialog1.ShowDialog() = DialogResult.OK Then 
  Dim fs As New IO.FileStream(OpenFileDialog1.FileName, _ 
  IO.FileMode.OpenOrCreate, IO.FileAccess.Read) '你所要传输的文件 
  Dim ssize(fs.Length - 1) As Byte 
  Dim br As New BinaryReader(fs) '处理要传输的文件 
  br.Read(ssize, 0, ssize.Length - 1) 
  
  mysocket.Connect(myipendponit) '连接到远程计算机 
  mysocket.Send(ssize) '发送文件 
  Label2.Text = fs.Length() 
  fs.Close() 
  mysocket.Shutdown(Net.Sockets.SocketShutdown.Send) 
  '关闭已发送连接 
  mysocket.Close() '关闭本机socket 
  End If 
  End Sub   
  进入Form1_Load事件中 
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
  Dim window2 As New Form2() 
  window2.Show() 
  End Sub   
  选择Form2窗体,进入代码编辑器中
  首先我们需要进行声明:   
  Imports System.Net.Sockets 
  Imports System.Net 
  Imports System.IO 
  Public Class Form2 
  Inherits System.Windows.Forms.Form 
  Dim receivesocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)   
  进入Form2_Load事件中 
  Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
  Dim hostipendpoint As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888) 
  receivesocket.Bind(hostipendpoint) 
  '建立远程计算机的的socket 
  receivesocket.Listen(2) '监听socket 
  End Sub
  进入Button1_Click事件中
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  SaveFileDialog1.Filter = "文本文件(.txt) *.txt"
  SaveFileDialog1.FileName = "接收的文件.txt"
  SaveFileDialog1.InitialDirectory = "c:Mytext"  
  If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
  Dim fs As New IO.FileStream(SaveFileDialog1.FileName, _
  IO.FileMode.OpenOrCreate)
  '接收数据并将其保存到一个新的文件
  Dim rebyte(229888) As Byte
  Dim myhostsocket As Socket = receivesocket.Accept()
  '发送端计算机建立连接
  Dim wr As New BinaryWriter(fs) '流写
  myhostsocket.Receive(rebyte)
  wr.Write(rebyte, 0, rebyte.Length - 1)
  fs.Close()
  myhostsocket.Shutdown(SocketShutdown.Receive)
  myhostsocket.Close()
  Label2.Text = SaveFileDialog1.FileName
  '读取已保存的文件
  Dim Readw As StreamReader
  Readw = File.OpenText(SaveFileDialog1.FileName)  
  ' 设置指针到开始位置
  Readw.BaseStream.Seek(0, SeekOrigin.Begin)
  Readw.BaseStream.Position = 0
  While (Readw.Peek() > -1)
  TextBox1.Text += Readw.ReadLine() & vbCrLf
  End While
  Readw.Close()  
  End If
  End Sub  
  好了代码输入完毕,接下来我们来运行程序测试一下
  程序启动后会弹出Form1窗体(发送端)和Form2窗体(接收端),我们选择Form1窗体(发送端)点击发送按钮,随意选择一个文本文件即可,看一看效果,如图4
                        
  接下来我们需要选择Form2窗体(接收端)点击接收文本并保存按钮图5 
  
                        
  
  这个窗体是利用了TCP协议进行了数据的通信,在传输时必须设置同时打开发送与接收端窗体才能进行数据的传输。如图6 
  
  
                        
  通过以上的测试,程序运行成功,我们主要利用了Socket以及TCP文件传输的技术进行了数据的发送与接收,希望此程序实例能够给大家带来帮助。