111,131
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net .Sockets ;
namespace WindowsApplication39
{
public partial class Form1 : Form
{
delegate void General();
RegisterWebService.Service RWS = new WindowsApplication39.RegisterWebService.Service();
public Form1()
{
InitializeComponent();
listBox1.Items.Add("名称 连接者 时间");
button1.Text = "新广播";
this.Size = new Size(432, 305);
RWS.Timeout = 8000;
RWS.CookieContainer = new CookieContainer();
Thread ListenThread = new Thread(new ThreadStart(Listen));
ListenThread.IsBackground = true;
ListenThread.Start();
}
void Listen()
{
General G;
int Start = Environment.TickCount;
while (true)
try
{
int End = Environment.TickCount;
if (End - Start < 5000)
continue;
listBox1.Invoke(G = delegate()
{
listBox1.BeginUpdate();
listBox1.Items.Clear();
listBox1.Items.Add("名称 连接者 时间");
foreach (String S in RWS.GetRegisteredLinks())
listBox1.Items.Add(S); ;
listBox1.EndUpdate();
});
Start = Environment.TickCount;
}
catch (Exception e)
{
this.Invoke(G = delegate()
{
this.Text = e.Message;
});
}
}
void button1_Click(object sender, EventArgs e)
{
String Info = "名称 " + Dns.GetHostByName(Dns.GetHostName()).AddressList[0] + " Handle:" + this.Handle.ToString()
+ " " + DateTime.Now.ToString();
RWS.RegisterLink(Info).ToString();
}
}
}using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public int RegisterLink(String Info)
{
if (Application["Infos"] == null)
Application["Infos"] = new List<String>();
((List<String>)Application["Infos"]).Add(Info);
return ((List<String>)Application["Infos"]).Count;
}
[WebMethod]
public String[] GetRegisteredLinks()
{
return ((List<String>)Application["Infos"]).ToArray();
}
}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net .Sockets ;
namespace WindowsApplication39
{
public partial class Form1 : Form
{
delegate void SetInfo();
public Form1()
{
InitializeComponent();
button1.Text = "new game";
listBox1.Items.Add("gamename creator createtime mancount");
Thread ListenThread = new Thread(new ThreadStart(Listen));
ListenThread.IsBackground = true;
ListenThread.Start();
}
void Listen()
{
while (true)
{
Socket S = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
S.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 7890);
S.Bind(IPEP);
EndPoint EP = (EndPoint)IPEP;
Byte[] Bytes = new byte[1024];
int Count = S.ReceiveFrom(Bytes, ref EP);
string stringData = Encoding.Default.GetString(Bytes, 0, Count);
Bytes = new byte[1024];
Count = S.ReceiveFrom(Bytes, ref EP);
SetInfo SI;
listBox1.Invoke(SI = delegate() { listBox1.Items.Add(Encoding.ASCII.GetString(Bytes, 0, Count)); });
S.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
String Info = "111 " + Dns.GetHostByName(Dns.GetHostName()).AddressList[0] + " " + DateTime.Now.ToString() + " 1/1";
Byte[] Bytes = new Byte[1024];
Socket S = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint IPEP = new IPEndPoint(IPAddress.Broadcast, 7890);
IPEndPoint IPEP2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 7890);
String hostname = Dns.GetHostName();
Bytes = Encoding.Default.GetBytes(Info);
S.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
S.SendTo(Bytes, IPEP);
S.SendTo(Bytes, IPEP2);
S.Close();
}
}
}