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