socket을 통해 데이터 세트 전송 및 수락 방법(C#)
//
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());
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception Class에서 에러 코드 해석 ~초기초편~직장에서 C# 프로젝트가 내뿜는 오류 코드를 구문 분석하고 오류의 위치를 확인하기 위해 Exception class를 활용할 수 있었습니다. 지금까지 Exception Class 에 대해서 별로 파악할 수 없었기 때...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.