[C#]TCP/IP + SQL -_-a C# 2008. 12. 26. 13:52

흠냐.. -_- 그냥 간단한 형식으로 만들었다

테스팅만 되게..

근데 -_- 근데!! 왜!!

-_- WrteLine만 해서 보낼 경우는 데이터가 왜 안가지 -_-;;

Flush()는 스트림을 비우는게 아닌감 -_-

난 그렇게 알고 있었는데...

뭐 암튼지간 -_- 여기 저기 기웃거림 뭔가 알겠지 -_-;;

******TCP/IP + DataBase Server Code 1:1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Data.SqlClient;

namespace server
{
    public partial class Form1 : Form
    {
        TcpListener server;
        TcpClient client;
        NetworkStream Stream = null;
        StreamReader reader;
        StreamWriter writer;
        Thread reciive;
        bool connect;
        private delegate void MBox();

        public Form1()
        {
            InitializeComponent();
        }    
      
        private void Check()
        {
            try
            {               
                client = server.AcceptTcpClient();
                connect = true;
                Stream = client.GetStream();
                reader = new StreamReader(Stream);
                writer = new StreamWriter(Stream);
                reciive = new Thread(new ThreadStart(Recieve));
                reciive.Start();
            }
            catch (Exception ex) { }
        }

        private void Recieve()
        {
            try
            {
                while (connect)
                {
                    Thread.Sleep(1);
                    if (Stream.CanRead)
                    {
                        Thread.Sleep(1);
                        if (Stream.CanRead)
                        {
                            string temp = reader.ReadLine();
                            if (temp.Length > 0)
                            {
                                Search();
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { }
        }

        private void Search()
        {
            string tests = @"User Id=sa; Password=cjstkd; Server =192.168.1.209; Initial Catalog = test";
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;
            try
            {
                conn = new SqlConnection(tests);
                conn.Open();
                cmd = new SqlCommand("select id from ID_PW", conn);
                cmd.CommandType = CommandType.Text;
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    writer.WriteLine(dr.GetString(0));
                    writer.Flush();
                }
                dr.Close();
                conn.Close();
            }
            catch (Exception ex) { }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread checkThread = new Thread(new ThreadStart(Check));
            checkThread.Start();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            connect = false;
            if (reader != null)
                reader.Close();
            if (writer != null)
                writer.Close();
            if (server != null)
                server.Stop();
            if (client != null)
                client.Close();
            if (reciive != null)
                reciive.Abort();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IPAddress addr = new IPAddress(0);
            int port = 5425;
            server = new TcpListener(addr, port);
            server.Start();
            Check();
            button2.Text="Start";
        }
    }
}

*****TCP/IP Client

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace client
{
    public partial class Form1 : Form
    {
        TcpClient client;
        StreamReader reader;
        StreamWriter writer;
        NetworkStream Stream;
        Thread receive;
        bool connect;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try{
                client = new TcpClient();
                client.Connect("192.168.1.209",5425);
                Stream = client.GetStream();
                connect = true;
                reader = new StreamReader(Stream);
                writer = new StreamWriter(Stream);
                receive = new Thread(new ThreadStart(Recieve));
                receive.Start();
            }
            catch(Exception ex){}
        }
                   
        private void Recieve(){
            try{
                while(connect)
                {
                    Thread.Sleep(1);
                    if (Stream.CanRead)
                    {
                        string temp = reader.ReadLine();
                        if (temp.Length > 0)
                            MessageBox.Show(temp);
                    }
                }
            }
            catch(Exception e){}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            writer.WriteLine("search");
            writer.Flush();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            connect = false;
            if (reader != null)
                reader.Close();
            if (writer != null)
                writer.Close();
            if (client != null)
                client.Close();
            if (receive != null)
                receive.Abort();
        }                   
    }
}

*********************************************************************************
1:다 Server
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Data.SqlClient;

namespace server
{
    public partial class Form1 : Form
    {
        TcpListener server;
        TcpClient client = null;
        NetworkStream Stream = null;
        StreamReader reader = null;
        StreamWriter writer = null;
        Thread reciive=null;
        Thread checkThread = null;
        bool connect=false;

        public Form1()
        {
            InitializeComponent();
        }    
      
        private void Check()
        { 
            string temp=null;
            try
            {
                while (true)
                {
                    temp = null;
                    client = server.AcceptTcpClient();
                    connect = true;

                    Stream = client.GetStream();
                    reader = new StreamReader(Stream);
                    writer = new StreamWriter(Stream);
                    temp = reader.ReadLine();
                    if (temp.Equals("search"))
                    {
                        Search();                    
                    }
                    else if (temp.Equals("search1"))
                    {
                        Search1();
                    }
                    temp = null;               
                }
            }
            catch (Exception ex) { }
        }

        private void Recieve()
        {
            try
            {
                while (connect)
                {
                    Thread.Sleep(1);
                    if (Stream.CanRead)
                    {
                        Thread.Sleep(1);
                        if (Stream.CanRead)
                        {
                            string temp = reader.ReadLine();
                            if (temp.Length > 0)
                            {
                                if (temp.Equals("search"))
                                {
                                    MessageBox.Show(temp);
                                    Search();
                                    temp = null;
                                }
                                else if (temp.Equals("search1"))
                                {
                                    MessageBox.Show(temp);
                                    Search1();
                                    temp = null;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { }
        }

        private void Search()
        {
            string tests = @"User Id=sa; Password=cjstkd; Server =192.168.1.209; Initial Catalog = test";
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;
            try
            {
                conn = new SqlConnection(tests);
                conn.Open();
                cmd = new SqlCommand("select id from ID_PW", conn);
                cmd.CommandType = CommandType.Text;
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    writer.WriteLine(dr.GetString(0));
                    writer.Flush();
                }
                dr.Close();
                conn.Close();
            }
            catch (Exception ex) { }
        }
        private void Search1()
        {
            string tests = @"User Id=sa; Password=cjstkd; Server =192.168.1.209; Initial Catalog = test";
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;
            try
            {
                conn = new SqlConnection(tests);
                conn.Open();
                cmd = new SqlCommand("select pw from ID_PW", conn);
                cmd.CommandType = CommandType.Text;
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    writer.WriteLine(dr.GetString(0));
                    writer.Flush();
                }
                dr.Close();
                conn.Close();
            }
            catch (Exception ex) { }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            connect = false;
            if (reader != null)
                reader.Close();
            if (writer != null)
                writer.Close();
            if (server != null)
                server.Stop();
            if (client != null)
                client.Close();             
            if (reciive != null)
                reciive.Abort();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IPAddress addr = new IPAddress(0);
            int port = 5425;
            server = new TcpListener(addr, port);
           
            server.Start(); 
                     
                checkThread = new Thread(new ThreadStart(Check));
               
                checkThread.IsBackground = true;
                checkThread.Start();
            button2.Text="Start";
        }
    }
}
*********************************************************************************
1:다 Client
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace client
{
    public partial class Form1 : Form
    {
        TcpClient client;
        StreamReader reader;
        StreamWriter writer;
        NetworkStream Stream;
        Thread receive;
        bool connect;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try{
                client = new TcpClient();
                client.Connect("localhost",5425);
                Stream = client.GetStream();
                connect = true;
                reader = new StreamReader(Stream);
                writer = new StreamWriter(Stream);
                receive = new Thread(new ThreadStart(Recieve));               
                receive.Start();             
            }
            catch(Exception ex){}
        }
                   
        private void Recieve(){
            try{
                while (connect)
                {
                    Thread.Sleep(1);
                    if (Stream.CanRead)
                    {
                        string temp = reader.ReadLine();
                        if (temp.Length > 0)
                            MessageBox.Show(temp);
                    }
                }
            }
            catch(Exception e){}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            writer.WriteLine("search");
            writer.Flush();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            connect = false;
            if (reader != null)
                reader.Close();
            if (writer != null)
                writer.Close();
            if (client != null)
                client.Close();
            if (receive != null)
                receive.Abort();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            writer.WriteLine("search1");
            writer.Flush();
        }                   
    }
}