各位,帮我看看我这个程序,为什么有的机器会收不到我用net send发送的消息~~
protected void Application_Start(Object sender, EventArgs e)
{
System.Timers.Timer atimer = new System.Timers.Timer();
SendMsg send = new SendMsg();
atimer.Interval = 8888888 ;
// atimer.Interval = 800000 ;
atimer.Enabled = true ;
atimer.AutoReset = true ;
atimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); }
private static void OnTimedEvent(object source,ElapsedEventArgs e)
{
try
{//char enter = (char)13 ;
// foreach(Process q in System.Diagnostics.Process.GetProcessesByName("cmd.exe"))
// {
// q.Kill();
// }
Process p = new Process();
p.StartInfo.FileName = "cmd.exe" ;
p.StartInfo.UseShellExecute = false ;
p.StartInfo.RedirectStandardError = true ;
p.StartInfo.RedirectStandardInput = true ;
p.StartInfo.RedirectStandardOutput = true ;
p.StartInfo.CreateNoWindow = true ;
p.Start();
StreamWriter sw = new StreamWriter(@"C:\Inetpub\wwwroot\Files\Data\Send_Msg.log",true,System.Text.Encoding.Default);
dbupload upload = new dbupload();
Files.Global socket = new Global();
DataTable dt = upload.list_rev() ;
for( int a=0;a<dt.Rows.Count;a++)
{
DataTable dt3 = upload.get_name(dt.Rows[a][0].ToString()) ;
DataTable dt2 = upload.list_ip(dt.Rows[a][0].ToString()) ;
for(int b=0;b<dt2.Rows.Count;b++)
{
string str ;
bool flag = socket.ConnectSocket(dt2.Rows[b][0].ToString(),135);
if(flag == true)
{
p.StandardInput.WriteLine("net send "+ dt2.Rows[b][0].ToString() + " " +dt3.Rows[0][0].ToString() + " 您有新文件还未接收,请上'Http://10.0.249.249/files'查收,打扰之处,敬请见谅 :)");
p.StandardInput.WriteLine(Convert.ToChar(13)) ;
// str = p.StandardError.ReadLine() ;
// if(str.Trim().Equals(""))
// {
str = "成功发送" ;
// }
// else
// {
// str = "对方关闭messenger服务" ;
// }
}
else
str = "建立tcp连接失败" ;
str += dt2.Rows[b][0].ToString() ;
str += "收件人" ;
str += dt3.Rows[0][0].ToString() ;
str += System.DateTime.Now.ToString();
sw.WriteLine(str) ;
}
}
p.StandardInput.WriteLine("exit") ;
p.Close();
sw.Close();
p.Dispose();
// string output = p.StandardError.ReadToEnd();
// p.WaitForExit();
// p.Kill();
//sw.Close();
}
catch(InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
}
public bool ConnectSocket(string server,int port)
{
bool flag = false ;
try
{
IPHostEntry hostEntry = null;
hostEntry = Dns.GetHostByAddress(server);
foreach(IPAddress adress in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(adress,port);
Socket tempSocket = new Socket(ipe.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
tempSocket.Connect(ipe);
if(tempSocket.Connected)
flag = true ;
tempSocket.Close();
}
}
catch(SocketException ex)
{
Console.WriteLine(ex.Message);
}
return flag ;
}
现在出现的情况是有些机器能连接成功,但是他就是收不到我发送的消息(对方机器messenger服务已开启,我自己手动用net send 发送他能收到)。。
还有那个我用str = p.StandardError.ReadLine() ; 获取错误信息的时候,如果这次发送不能成功。但是下一次执行net send(发送成功),我所读取得错误信息仍然是上次的,如果我用str = p.StrandardError.ReadToEnd().来读取错误信息的时候好慢阿。。。。。
那位能帮我解决的,100分相送
或者那位给我个好的方案把(我现在要实现的是,一个发布文件的系统,我要在文件发布过后,通知收件人去看文件,要求实现的是收件人只要开着电脑就能收到信息,我现在想到的就是调用那个信使服务,各位有更好建议的说一下思路,谢谢啦~~~~~~~)
周一回来接帖~~~~~~~~~~~~~~
问题点数:50、回复次数:6Top
1 楼dragonforfly(飘零)回复于 2004-12-03 20:00:36 得分 0
是不是信使服务关闭了Top
2 楼haonanernet(与时俱进)回复于 2004-12-04 11:44:24 得分 0
对方机器messenger服务已开启,但是他的机器不处在监听的状态下,所以无法接收你的信息Top
3 楼haonanernet(与时俱进)回复于 2004-12-04 11:46:27 得分 50
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net.Sockets;
using System.IO;
namespace ch7_2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.StatusBar statusBar1;
//代表监听的线程
private Thread listenThread;
//代表监听的状态
private bool listen=false;
//代表网络监听的对象
private TcpListener tcpListener;
private System.Windows.Forms.RichTextBox richTextBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 8);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(272, 40);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 20);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(176, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 2;
this.label1.Text = "目标地址";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 3;
this.button1.Text = "准备接收";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(190, 56);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "停止接收";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 62);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 5;
this.label2.Text = "请输入姓名";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(104, 56);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(224, 56);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 23);
this.button3.TabIndex = 7;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.textBox1,
this.button1,
this.button2});
this.groupBox1.Location = new System.Drawing.Point(16, 192);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(272, 96);
this.groupBox1.TabIndex = 8;
this.groupBox1.TabStop = false;Top
4 楼haonanernet(与时俱进)回复于 2004-12-04 11:47:03 得分 0
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 303);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(296, 22);
this.statusBar1.TabIndex = 9;
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(16, 88);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(272, 96);
this.richTextBox2.TabIndex = 10;
this.richTextBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 325);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox2,
this.statusBar1,
this.groupBox1,
this.button3,
this.textBox2,
this.label2,
this.richTextBox1});
this.Name = "Form1";
this.Text = "点对点聊天";
this.Closed += new System.EventHandler(this.Form1_Closed);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//改变按钮状态
button1.Enabled = false;
button2.Enabled = true;
//创建监听线程
listenThread = new Thread(new ThreadStart(StartListen));
//启动线程
listenThread.Start();
}
private void StartListen()
{
try
{
tcpListener = new TcpListener(5675);
//开始监听
tcpListener.Start();
//改变状态栏
statusBar1.Text = "正在监听...";
//设置监听状态
this.listen=true;
while(listen)
{
// //接受连接请求
// Socket s = tcpListener.AcceptSocket();
// //设置一个缓冲区
// Byte[] stream = new Byte[100];
// int i=s.Receive(stream) ;
// //将获得的流转化为字符串类型
// string message = System.Text.Encoding.UTF8.GetString(stream);
// this.richTextBox2.AppendText(message);
//获取TcpClient
TcpClient s=tcpListener.AcceptTcpClient();
NetworkStream nc=s.GetStream();
byte[] bytes = new byte[1024];
int bytesRead=nc.Read(bytes,0,bytes.Length);
string message=System.Text.Encoding.Default.GetString(bytes,0,bytesRead);
this.richTextBox2.AppendText(message);
}
}
//错误处理
catch
{
//忽略错误处理
//改变状态栏
statusBar1.Text = "已停止监听!";
}
}
private void button2_Click(object sender, System.EventArgs e)
{
//改变按钮状态
this.button1.Enabled=true;
this.button2.Enabled=false;
//改变监听状态
this.listen=false;
//停止监听器
this.tcpListener.Stop();
//停止监听线程
this.listenThread.Abort();
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
//获得消息
string msg = "<"+this.textBox2.Text+">"+this.richTextBox1.Text+"\n";
//根据目标计算机地址建立连接
TcpClient tcpClient = new TcpClient(textBox1.Text, 5675);
//获得用于网络访问的数据流
NetworkStream tcpStream = tcpClient.GetStream();
StreamWriter streamW = new StreamWriter(tcpStream);
//将字符串写入流
streamW.Write(msg);
//将缓冲数据写入基础流
streamW.Flush();
//关闭网络流
tcpStream.Close();
//关闭
tcpClient.Close();
this.richTextBox2.AppendText(msg);
this.richTextBox1.Clear();
}
catch
{
statusBar1.Text = "目标计算机拒绝连接请求!";
}
}
private void Form1_Closed(object sender, System.EventArgs e)
{
if (this.tcpListener!=null)
{
//关闭监听器
this.tcpListener.Stop();
}
if (this.listenThread!=null)
{
//如果线程还处于运行状态就关闭它
if (this.listenThread.ThreadState==ThreadState.Running)
{
this.listenThread.Abort();
}
}
}
}
}
Top
5 楼nga96(因为我笨,所以努力。陈勇华)回复于 2004-12-04 20:17:51 得分 0
太多代码了,没细看Top
6 楼alps1982(屁股超人)回复于 2004-12-05 09:26:42 得分 0
太长了`~Top
相关问题
- 如何在没有.net的机器上运行C#程序?
- 如何将.net程序直接编译成机器代码?
- 无.net framwork的机器安装.net程序中间出错的错误提示
- 如何在没有装.NET环境的机器上运行.NET写的程序?
- .Net程序可以部署没有安装.Net Framwork的机器上吗?
- c#桌面应用程序怎么做安装程序给没装.NET框架的机器使用????????急
- Visual C#写的程序怎样才能在没装.NET的机器上运行?
- 未安装 .Net 的机器如何运行 VC.Net 编译的程序
- 请问如果在程序里发送控制台消息??如果用程序是否有人访问我的机器!!
- 刚才没有贴上(请问如何在程序里发送控制台消息??如何用程序是否有人访问我的机器!!)




