代码如下using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.Text;
using System.Threading;namespace service
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private TcpListener tcpl;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(168, 24);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(264, 77);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "服务器控制";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
tcpl = new TcpListener(13);
} private void service()
{
try
{
Encoding ASCII = Encoding.ASCII;
tcpl.Start();
while (true)
{
Socket s = tcpl.AcceptSocket();
string okstring = "o";
Byte[] byteDateLine = ASCII.GetBytes(okstring.ToCharArray());
s.Send(byteDateLine, byteDateLine.Length, 0);
s.Close();
}
}
catch (SocketException socketError)
{
if (socketError.ErrorCode == 10048)
{
MessageBox.Show("连接到此端口失败。有另一台服务器正在此端口上侦听。");
}
}
} private void button1_Click(object sender, System.EventArgs e)
{
service();
}
}
}当它运行的时候整个form就失去响应了,只有socket服务在运行,我如何避免这种现象?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.Text;
using System.Threading;namespace service
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private TcpListener tcpl;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(168, 24);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(264, 77);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "服务器控制";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
tcpl = new TcpListener(13);
} private void service()
{
try
{
Encoding ASCII = Encoding.ASCII;
tcpl.Start();
while (true)
{
Socket s = tcpl.AcceptSocket();
string okstring = "o";
Byte[] byteDateLine = ASCII.GetBytes(okstring.ToCharArray());
s.Send(byteDateLine, byteDateLine.Length, 0);
s.Close();
}
}
catch (SocketException socketError)
{
if (socketError.ErrorCode == 10048)
{
MessageBox.Show("连接到此端口失败。有另一台服务器正在此端口上侦听。");
}
}
} private void button1_Click(object sender, System.EventArgs e)
{
service();
}
}
}当它运行的时候整个form就失去响应了,只有socket服务在运行,我如何避免这种现象?
解决方案 »
- 一个中小型企业QQ系统的完整实现[附C#源代码下载]
- 求教一个关于回滚DATASET写入数据库的回滚方法
- 如何在数据访问层里实现多表联接
- 如何让一个方法里的后面一部分语句在webbrowser.DocumentCompleted事件后执行
- 为什么在查询分析器连接远程的sqlserver服务器可以,但下面的语句不行?
- [编译后exe安全吗?] 不用Reflector.exe就知道你开发时文件结构
- 求助:如何用AT指令对串口GSM MODEM操作?
- 求一字符串的拆分方法
- 发布时出现"两个或多个对象具有相同的目标位置(“[targetdir]\bin\oi.dll”)"提示,不能发布
- 拿optimizeit enterprise 5.5的全套注册码。 _weiKun_ (Virus) 進來領分!
- 请问哪位做过点击按钮直接打开QQ对话窗口的功能?
- [求助]送分,C#中如何使用通配符来模糊比较字符串!
代码就不用看了
while (true)
{
}
的不休息,界面不死才怪。
1。最好点击后开一个线程运行监听。2。设计到窗口界面问题,需要等待或大量操作时,建议用Application.DoEvents();
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace tbserver
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
private static Socket CurSocket;
private static IPEndPoint ServerIPEP;
private static Thread CurThread;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
CurSocket=new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);
ServerIPEP=new IPEndPoint(IPAddress.Any, 11000);
try
{
CurSocket.Bind(ServerIPEP);
}
catch (Exception e)
{
Console.Write(e.ToString());
}
CurThread = new Thread(new ThreadStart(Listen));
CurThread.Start();
}
private static void Listen()
{
while (true)
{
//try
//{
int _recv=0;
byte[] _data=new byte[10240];
IPEndPoint _sender=new IPEndPoint(IPAddress.Any, 0);
EndPoint _RemoteEP=_sender;
try
{
_recv = CurSocket.ReceiveFrom(_data,ref _RemoteEP);
}
catch (Exception e)
{
//saveLog("收到错误数据包"+e.ToString());
}
string a=Encoding.ASCII.GetString(_data, 0, _recv); Console.WriteLine(String.Format("Read {0} byte from socket" +
"data = {1} ", a.Length, a));
byte[] byteData = Encoding.ASCII.GetBytes(a);
// Begin sending the data to the remote device.
CurSocket.SendTo(byteData, _RemoteEP); //}
//catch (Exception e)
//{
// saveLog("产生错误:"+e.ToString());
//}
}
}
}}