posted by 방랑군 2012. 1. 6. 23:02


System.IO.FileStream
System.IO.MemoryStream
System.IO.BufferedStream
System.Net,Sockets.NetworkStream
System.Security,Cryptography.CryptoStream : 암호화 객체를 스트림으로 다룬다. 
posted by 방랑군 2012. 1. 6. 22:06
       // 서버 TCP 리스너
        private TcpListener _server = null;
.
.
.
              // TcpListener로 서버 객체를 생성합니다.
                _server = new TcpListener(localHostEntry.AddressList[0], _svrPort);
.
.
.
         // 실제 스레드가 처리되는 메소드
        private void ServerThreadStart()
        {
            // 클라이언트 소켓 객체를 선언합니다.
            Socket clientSocket = null;

            while (!_isStop)
            {
                try { 
                
                    // 서버에 접속된 클라이언트 소켓을 받습니다.
                    clientSocket = _server.AcceptSocket();    //<-- 대기상태
                         : while 돌면서 이부분에서 대기 상태에 있다가 Client에서
                         (TcpClient _tcpClient = new TcpClient(_svrIP, _svrPort);)이 부분을 만나 Connect 하면서 다음
                         줄 명령라인으로 이동된다.

==> 설명 
clientSocket = _server.AcceptSocket();   이 곳에서 주구장창 클라이언트들을 기다린다.
 접속될때마다 각각 클라이언트들은  클라이언트마다 틀린 RemoteEndPoint ( IP :PORT)  즉, 포트만 틀린 종점을 받아
 서로 연결되어 수행이 이루어진다.  이때의 PORT 는 실제(컴퓨터) PORT 가 아닌 가상(SOCKET) PORT 이다.

Connection.LocalEndPoin는 자신의 IP와 Port를 표시하고, 
Connection.RemoteEndPoint는 자신의 LocalEndPoint에 접속된 다른 Socket의 IP와포트를 나타냅니다. 

 LocalEndPoin : 컴퓨터의 IP, PORT
RemoteEndPoint : SOCKET 의 IP, PORT <-- 여러 클라이언트 구별 키.



 
posted by 방랑군 2012. 1. 6. 17:33

            // 서버를 실행하는 컴퓨터의 IP를 찾아 종점을 생성합니다.
            IPHostEntry localHostEntry = Dns.GetHostByName(Dns.GetHostName());
posted by 방랑군 2012. 1. 6. 16:40
1. Server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net.Sockets;
using System.Net;

namespace NetworkServer
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("Before Start");
            IPAddress address = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(address, 8010);

            //Console.WriteLine("Before Listener Start");
            listener.Start();

            //Console.WriteLine("Before Accept Socket");
            Socket socket = listener.AcceptSocket();

            ////Console.WriteLine("Before Receive");
            //byte[] myByte = new byte[256];
            //socket.Receive(myByte);

            //string willOut = System.Text.Encoding.Default.GetString(myByte);

            //// 보내기.
            //byte[] wiiSend = System.Text.Encoding.UTF8.GetBytes("Send To Client From Server");

            Encoding utf8 = Encoding.UTF8;
            while (true)
            { 
                // Receive
                byte[] myByte = new byte[256];
                socket.Receive(myByte);

                // Conversion
                string willOut = utf8.GetString(myByte);
                willOut = willOut.Replace("\0", string.Empty);
                Console.WriteLine("Client : " + willOut);

                // Send
                Console.Write("Server : ");
                string willSendString = Console.ReadLine();
                if (willSendString == "EXIT")
                {
                    break;
                }

                byte[] willSend = utf8.GetBytes(willSendString);
                socket.Send(willSend);
            }

            //Console.WriteLine("Before Close");
            socket.Close();
            listener.Stop();
        }
    }
}


2. Client 
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.IO;

namespace NetworkClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("Before Connect");
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect("127.0.0.1", 8010);
            Stream stream = tcpClient.GetStream();

            ////Console.WriteLine("Before Get Stream");
            //byte[] myByte = new byte[256];
            //string strLetters = "HWANG SEUNG JAE";
            //myByte = System.Text.Encoding.Default.GetBytes(strLetters);
            //Stream stream = tcpClient.GetStream();

            ////Console.WriteLine("Before Write");
            //stream.Write(myByte, 0, myByte.Length);


            ////받기
            //byte[] willOut = new byte[256];
            //stream.Read(willOut, 0, 256);
            Encoding utf8 = Encoding.UTF8;
            while (true)
            { 
                // Send
                Console.Write("Client : ");
                string myString = Console.ReadLine();
                if (myString == "EXIT")
                {
                    break;
                }

                byte[] myByte = utf8.GetBytes(myString);
                stream.Write(myByte, 0, myByte.Length);

                // Receive
                byte[] willOut = new byte[256];
                stream.Read(willOut, 0, 256);
                string willOutString = utf8.GetString(willOut);
                willOutString = willOutString.Replace("\0", string.Empty);
                Console.WriteLine("Server : " + willOutString);
            }

            //Console.WriteLine("Before Close");
            tcpClient.Close();
        }
    }
}
 
posted by 방랑군 2012. 1. 6. 15:48

1. Server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net.Sockets;
using System.Net;

namespace NetworkServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Before Start");
            IPAddress address = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(address, 8010);

            Console.WriteLine("Before Listener Start");
            listener.Start();

            Console.WriteLine("Before Accept Socket");
            Socket socket = listener.AcceptSocket();

            Console.WriteLine("Before Receive");
            byte[] myByte = new byte[256];
            socket.Receive(myByte);

            string willOut = System.Text.Encoding.Default.GetString(myByte); 

            Console.WriteLine("Before Close");
            socket.Close();
            listener.Stop();
        }
    }
}


2. Client 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.IO;

namespace NetworkClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Before Connect");
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect("127.0.0.1", 8010);

            Console.WriteLine("Before Get Stream");
            byte[] myByte = new byte[256];
            string strLetters = "HWANG SEUNG JAE";
            //인터넷상에서 스트림 이동은 모두 바이트만 가능합니다. 
            myByte = System.Text.Encoding.Default.GetBytes(strLetters);
            Stream stream = tcpClient.GetStream();

            Console.WriteLine("Before Write");
            stream.Write(myByte, 0, myByte.Length);

            Console.WriteLine("Before Close");
            tcpClient.Close();
        }
    }
}