프로그램/C# - Study / / 2010. 10. 29. 10:44

C# - Ex08 Client - Server (1ver)

반응형




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

//TCP / IP   10/29
namespace Ex08Server
{
    class Program
    {
        static void Main(string[] args)
        {

            //1. Create Socket
            Socket listener = new Socket(
                AddressFamily.InterNetwork, //ipv4
                //SocketType.Dgram, ProtocolType.Udp//upd 사용
                SocketType.Stream, ProtocolType.Tcp);//tcp 사용

            //2. Bind (Allocate IP, Port)
            IPEndPoint address = new IPEndPoint(
                IPAddress.Any, 9999); // 어떤 아이피든 가능 IP주소
            //IPAddress.Parse("172.16.77.12"), 9999); // 본인 IP주소
            listener.Bind(address);

            //3. Listen (Configration)
            listener.Listen(10);//최대 연결 대기 클라이언트 갯수

            //4. Accept (Wait for Client Connection)
            Socket socket = listener.Accept();

            //5. Receive / Send

            NetworkStream stream = new NetworkStream(socket);
            StreamReader reader = new StreamReader(stream);
            StreamWriter writer = new StreamWriter(stream);

            String message = reader.ReadLine();

            Console.WriteLine("클라이언트 메시지 : {0}", message);

            writer.WriteLine(message);
            writer.Flush();

            //6. Shutdown - Close
            reader.Close();
            writer.Close();
            stream.Close();

            socket.Shutdown(
                SocketShutdown.Both);//진행중인 작업 정리
            socket.Close();

            listener.Close();


        }

    }
}




///////////////////////////////




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace Ex08Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //1. Create Socket
            Socket socket = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            //2. Connect
            socket.Connect("172.16.77.11", 9999);  // 상대 IP주소

            //3. Send, Receive
            NetworkStream stream = new NetworkStream(socket);
            StreamReader reader = new StreamReader(stream);
            StreamWriter writer = new StreamWriter(stream);

            Console.Write("메시지를 입력하세요 : ");
            string message = Console.ReadLine();

            writer.WriteLine(message);
            writer.Flush();
            String message2 = reader.ReadLine();
            Console.WriteLine("서버 메시지 : {0}", message2);

            //4. Shutdown - Close
            reader.Close();
            writer.Close();
            stream.Close();
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }
    }
}

반응형

'프로그램 > C# - Study' 카테고리의 다른 글

C# - Ex08 Client - Server (2ver)  (1) 2010.10.29
C# - Ex07 - Brush 다루기  (0) 2010.10.28
C# - Ex06 - 그림판3  (0) 2010.10.27
C# - Ex06 - 그림판2  (3) 2010.10.27
C# - Ex06 - 그림판  (0) 2010.10.27
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유