我现在有个软件要连接远程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();
} }
}
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();
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货