Windows_/Windows_C# & App

C# Socket echo server/client 예제

sosal 2014. 10. 8. 04:15
반응형


/*

 http://sosal.kr/
 * made by so_Sal
 */




Server에서 Listen으로 Client의 접속을 기다립니다.

Client가 접속하면, Client가 보낸 메시지를 시간과 함께 돌려줍니다. (Echo 서버)


Stream을 이용하여 데이터 전송을 구현하였습니다. (StreamReader, StreamWriter)

인코딩은 UTF8을 사용하였습니다.



Client 소스



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

 

using System.IO;

using System.Net;

using System.Net.Sockets;

 

namespace ConsoleTest

{

    class Program

    {

        static void Main(string[] args)

        {

            int PORT = 5555;

            string IP = "localhost";

 

            NetworkStream NS = null;

            StreamReader SR = null;

            StreamWriter SW = null;

            TcpClient client = null;

 

            try

            {

                client = new TcpClient(IP, PORT); //client 연결

                Console.WriteLine("{0}:{1} 접속하였습니다.",IP,PORT);

                NS = client.GetStream(); // 소켓에서 메시지를 가져오는 스트림

                SR = new StreamReader(NS, Encoding.UTF8); // Get message

                SW = new StreamWriter(NS, Encoding.UTF8); // Send message

 

                string SendMessage = string.Empty;

                string GetMessage = string.Empty;

 

                while ((SendMessage = Console.ReadLine()) != null)

                {

                    SW.WriteLine(SendMessage); // 메시지 보내기

                    SW.Flush();

 

                    GetMessage = SR.ReadLine();

                    Console.WriteLine(GetMessage);

                }

            }

           

            catch (Exception e)

            {

                System.Console.WriteLine(e.Message);

            }

            finally

            {

                if(SW != null) SW.Close();

                if (SR != null) SR.Close();

                if (client != null) client.Close();

               

               

            }

        }

    }

}




Server 소스




using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

 

using System.IO;

using System.Net;

using System.Net.Sockets;

 

 

namespace Sock_console_server

{

    class Program

    {

        static void Main(string[] args)

        {

            TcpListener Listener = null;

            NetworkStream NS = null;

 

            StreamReader SR = null;

            StreamWriter SW = null;

            TcpClient client = null;

 

            int PORT = 5555;

           

            Console.WriteLine("서버소켓");

            try

            {

                Listener = new TcpListener(PORT);

                Listener.Start(); // Listener 동작 시작

 

                while (true)

                {

                    client = Listener.AcceptTcpClient();

 

                    NS = client.GetStream(); // 소켓에서 메시지를 가져오는 스트림

                    SR = new StreamReader(NS, Encoding.UTF8); // Get message

                    SW = new StreamWriter(NS, Encoding.UTF8); // Send message

 

                    string GetMessage = string.Empty;

 

                    while (client.Connected == true) //클라이언트 메시지받기

                    {

                        GetMessage = SR.ReadLine();

                       

                        SW.WriteLine("Server: {0} [{1}]",GetMessage,DateTime.Now); // 메시지 보내기

                        SW.Flush();

                        Console.WriteLine("Log: {0} [{1}]",GetMessage,DateTime.Now);

                    }

                }

            }

            catch (Exception e)

            {

                System.Console.WriteLine(e.Message);

            }

            finally

            {

                SW.Close();

                SR.Close();

                client.Close();

                NS.Close();

            }

        }

    }

}