我现在有个软件要连接远程SqlServer但是不可能把远程SqlServer密码放到程序里面,目前我做个服务器,服务器断负责处理一些数据发送到客户端,我下面有个很笨的例子,但是不知道怎么取客户端那边的IP,我想每当监视到信息后,服务器端就自动处理一些数据返回此时接受到的客户端;例子:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;namespace Server
{
    public partial class Form1 : Form
    {
        //声明监听线程对象
        private Thread MyListenThread;
        //声明网络监听对象
        private TcpListener MyTcpListener;
        //线程所须
        delegate void SetTextCallback(string text);        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            //开始监听
            //创建监听线程
            this.MyListenThread = new Thread(new ThreadStart(StartListen));
            //启动线程
            this.MyListenThread.Start();
        }        #region Send
        private void Send(string Command)
        {
            try
            {
                //根据目标计算机地址建立连接
                TcpClient MyTcpClient = new TcpClient("60.190.223.9", 8800);
                //获得用于网络访问的数据流
                NetworkStream MyTcpStream = MyTcpClient.GetStream();
                StreamWriter MyStream = new StreamWriter(MyTcpStream);
                //将字符串写入流
                MyStream.Write(Command);
                //将缓冲数据写入基础流
                MyStream.Flush();
                //关闭网络流
                MyStream.Close();
                MyTcpClient.Close();
            }
            catch (Exception Err)
            {
                Systeminfo("连接错误:" + Err.Message);
            }
        }
        #endregion        #region 开始监听
        private void StartListen()
        {
            try
            {
                MyTcpListener = new TcpListener(8800);
                //开始监听
                MyTcpListener.Start();
                while (true)
                {//获取TcpClient
                    TcpClient MyTcpClient = MyTcpListener.AcceptTcpClient();
                    NetworkStream MyStream = MyTcpClient.GetStream();
                    byte[] MyBytes = new byte[1024];
                    int MyBytesRead = MyStream.Read(MyBytes, 0, MyBytes.Length);
                    string MyMessage = System.Text.Encoding.Default.GetString(MyBytes, 0, MyBytesRead);
                    JTTS(MyMessage);                }
            }
            catch
            {
                //
            }
        }
        #endregion        #region 关闭监听
        private void ClosedListen()
        {//关闭SOCKET
            try
            {
                if (this.MyTcpListener != null)
                {//关闭监听器
                    this.MyTcpListener.Stop();
                }
                if (this.MyListenThread != null)
                { //如果线程还处于运行状态就关闭它
                    if (this.MyListenThread.ThreadState == ThreadState.Running)
                    {
                        this.MyListenThread.Abort();
                    }
                }
            }
            catch (Exception Err)
            {
                Systeminfo("错误:" + Err.Message);
            }
        }
        #endregion        #region JTTS
        private void JTTS(string text)
        {
            try
            {
                if (this.TS.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(JTTS);
                    this.Invoke(d, new object[] { text });
                }
                else
                {
                    this.TS.Items.Add(text);
                }
                if (TS.Items.Count > 10)
                {
                    TS.SelectedIndex = TS.Items.Count - 1;
                }
            }
            catch
            {
                //
            }
        }
        #endregion        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ClosedListen();
        }        private void button1_Click(object sender, EventArgs e)
        {
            ClosedListen();
            this.Close();
        }    }
}

解决方案 »

  1.   

    你使用Socket来写,服务器可以通过socket连接知道连接上来的client的ip的.
      

  2.   

    客户端那边的IP不知道怎么取的到,只能取到192.168.*.*之类的IP,真实IP不知道怎么取?你知道吗? 网上有好多例子,但是都不行!
      

  3.   

    jiezhi(风满袖)你那有例子吗?有的话给我发个!谢谢~
      

  4.   

    建立TCP长连接,这样,知不知道客户端IP就无所谓了,服务器端有了数据就可以发送给客户端了
      

  5.   

    cs920(头痛不是两三天)(此情可待)谢谢你,发到这里或我QQ里都行 我QQ
      

  6.   

    YAOHE(吆喝)建立TCP长连接,有点不明白!
      

  7.   

    服务端程序一直打开,并监听一特定的端口(需要有固定IP)的TCP连接(非UDP),客户端启动,主动与服务器连接,连接成功后,服务器端可以发消息到客户端,客户端也可以发消息到服务器端,在没有消息时,定期发送链路检测包,保持网络连接.
      

  8.   

    我程序是那样设计的但是 客户端有不同的IP 那IP不知道怎么才能得到