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());

                 }

             }



         }



     }

}




좋은 웹페이지 즐겨찾기