//以下是属于客户端的代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public static byte[] GetBinaryFormatDataSet(DataSet ds)
{
//创建内存流
MemoryStream memStream = new MemoryStream();
//产生二进制序列化格式
IFormatter formatter = new BinaryFormatter();
//指定DataSet串行化格式是二进制
ds.RemotingFormat = SerializationFormat.Binary;
//串行化到内存中
formatter.Serialize(memStream, ds);
//将DataSet转化成byte[]
byte[] binaryResult = memStream.ToArray();
//清空和释放内存流
memStream.Close();
memStream.Dispose();
return binaryResult;
}
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
SqlConnection conn = new SqlConnection("server=.;database=news;uid=sa;pwd=sa;");
conn.Open();
String selstr = "select * from newsType";
SqlDataAdapter adapter = new SqlDataAdapter(selstr, conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "phonetab");
conn.Close();
byte[] input = GetBinaryFormatDataSet(ds);
try
{
//远程的ip地址和端口号
IPAddress ip = System.Net.IPAddress.Parse("127.0.0.1");
IPEndPoint ipep = new IPEndPoint(ip, 3434);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ipep);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
server.Send(input);
byte[] response = new byte[1024];
int bytesRead = server.Receive(response);
Console.WriteLine(Encoding.ASCII.GetString(response, 0, bytesRead));
server.Shutdown(SocketShutdown.Both);
server.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.ReadLine();
}
}
}
//一下是属于服务器端的代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
namespace ConsoleApplication2
{
class Program
{
public static DataSet ds;
public static DataSet RetrieveDataSet(byte[] binaryData)
{
//创建内存流
MemoryStream memStream = new MemoryStream(binaryData);
memStream.Seek(0, SeekOrigin.Begin);
//产生二进制序列化格式
IFormatter formatter = new BinaryFormatter();
//反串行化到内存中
object obj = formatter.Deserialize(memStream);
//类型检验
if (obj is DataSet)
{
DataSet dataSetResult = (DataSet)obj;
return dataSetResult;
}
else
{
return null;
}
}
static void Main(string[] args)
{
IPAddress ip = System.Net.IPAddress.Parse("127.0.0.1");
IPEndPoint ipep = new IPEndPoint(ip, 3434);
Socket lst = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
lst.Bind(ipep);
lst.Listen(20);
while (true)
{
Console.Write("等待连接.......");
Socket client = lst.Accept();
Console.WriteLine("客户已连接");
byte[] request = new byte[512];
int bytesRead = client.Receive(request);
//ds = RetrieveDataSet(request);
string input = Encoding.ASCII.GetString(request, 0, bytesRead);
Console.WriteLine("客户请求:{0}", input);
string output = "hello, " + input + "!";
byte[] hello = Encoding.ASCII.GetBytes(output);
try
{
client.Send(hello);
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
}
如何通过socket发送和接受dataset数据集(C#)
最新推荐文章于 2023-12-18 16:48:50 发布