我首先尝试调整您的代码,但在搜索信息时,我发现了几个 MSDN 示例:
同步服务器套接字示例
http://msdn.microsoft.com/en-CA/library/6y0e13d3.aspx
同步客户端套接字示例
http://msdn.microsoft.com/en-CA/library/kb5kfec7.aspx
从这些中大量借用,我想出了以下内容。(它们在 C# 中,但将它们翻译成 VB.NET 应该不是很困难。)
ACE_Server.exe 是一个控制台应用程序。它会一直监听连接,直到你用Ctrl+杀死它C:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SynchronousSocketListener
{
// Incoming data from the client.
public static string data = null;
public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
IPHostEntry ipHostEntry = Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostEntry.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null;
// An incoming connection needs to be processed.
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
}
// Show the data on the console.
Console.WriteLine("Text received : {0}", data);
// do the database stuff
string sql = data.Substring(0, data.Length - 5); // remove "<EOF>" terminator
var con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\__tmp\testData.accdb;");
var cmd = new System.Data.OleDb.OleDbCommand(sql, con);
var adapter = new System.Data.OleDb.OleDbDataAdapter();
adapter.SelectCommand = cmd;
var ds = new System.Data.DataSet();
adapter.Fill(ds, "Batch data");
var sw = new System.IO.StringWriter();
ds.Tables["Batch data"].WriteXml(sw, System.Data.XmlWriteMode.IgnoreSchema);
// send the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes(sw.ToString());
int bytesSent = handler.Send(msg);
Console.WriteLine(bytesSent.ToString() + " bytes sent.");
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static int Main(String[] args)
{
StartListening();
return 0;
}
}
ACE_Client.exe 是一个 Windows 窗体应用程序。请注意,它附加"<EOF>"
到 SQL 字符串,以便服务器组件知道何时停止读取:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
namespace ACE_Client
{
public partial class Form1 : Form
{
public static string data = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostEntry = Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostEntry.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
sock.Connect(remoteEP);
string sql = "SELECT * FROM ExpenseDetails";
byte[] msg = Encoding.ASCII.GetBytes(sql + "<EOF>");
// Send the data through the socket.
int bytesSent = sock.Send(msg);
// Receive the response from the remote device.
data = null;
while (true)
{
bytes = new byte[1024];
int bytesRec = sock.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("</NewDataSet>") > -1)
{
break;
}
}
// Release the socket.
sock.Shutdown(SocketShutdown.Both);
sock.Close();
var dsa = new DataSet();
dsa.ReadXml(new System.Xml.XmlTextReader(new System.IO.StringReader(data)));
dataGridView1.DataSource = dsa.Tables[0];
}
}
}