블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
반응형

Category

Recent Post

Recent Comment

Archive

반응형

 Server Socket 작성하기
using System;
using System.Net.Sockets;
using System.Net
using System.Text;

public class SocketServer{

                                         public static void Main (string [] args)


IPHostEntry ipHost = Dns.Resolve("localhost");
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

Socket sListener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try {
       sListener.Bind(ipEndPoint);
       sListener.Listen(10); 
     
       while (true) {

                    Console.WriteLine("Waiting for a connection on port {0}",ipEndPoint); 
                    Clientsoket handler = sListener.Accept();
                    string data = null;
        while(true) {
                           byte[] bytes = new byte[1024];
                           int bytesRec = handler.Receive(bytes);
                           data += Encoding.ASCII.GetString(bytes,0,bytesRec);
                           if (data.IndexOf("") > -1)
                           {
                              break;
                           }
         }
Console.WriteLine("Text Received: {0}",data);
string theReply = "Thank you for those " + data.Length.ToString() + " characters...";
byte[] msg = Encoding.ASCII.GetBytes(theReply);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch(Exception e) {
                             Console.WriteLine(e.ToString());
}

}


 Client Socket 작성하기

using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
public class SocketClient{
                        public static void Main (string [] args)
                        { 
                           byte[] bytes = new byte[1024];
                           try{
                                IPHostEntry ipHost = Dns.Resolve("127.0.0.1");
                                IPAddress ipAddr = ipHost.AddressList[0];
                                IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
                                Socket sender = new Socket
                            (AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

                                  sender.Connect(ipEndPoint); 
                                  Console.WriteLine("Socket connected to {0}",
                                                              sender.RemoteEndPoint.ToString());
                                                               string theMessage;

                                 if (args.Length==0)
                                         theMessage = "This is a test";
                                  else
                                         theMessage = args[0];
                                         byte[] msg = Encoding.ASCII.GetBytes(theMessage+"");
                                         int bytesSent = sender.Send(msg);
                                         int bytesRec = sender.Receive(bytes); 
                                         Console.WriteLine("The Server says : {0}",
                                          Encoding.ASCII.GetString(bytes,0, bytesRec));

                                          Sender.Shutdown(SocketShutdown.Both);
                                          sender.Close();
                                   } catch(Exception e)
                       {
                         Console.WriteLine("Exception: {0}", e.ToString()); 
                       }
             }

반응형

'A Concern Interest > C#소켓프로그래밍' 카테고리의 다른 글

02장 TCP Server&Client 활용  (0) 2009.09.15
01장.C# Socket 이란?  (0) 2009.09.15
posted by Magic_kit