Client
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;
using System.Net.Sockets;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
this.Send();
}
public void Send()
{
try
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//IPAddress br = Dns.GetHostAddresses("000000")[0];
IPEndPoint ep = new IPEndPoint(IPAddress.Broadcast, 1984);//
byte[] sendbuf = Encoding.GetEncoding("gb2312").GetBytes("000000");
//IPEndPoint ep = new IPEndPoint(br, 1984);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
s.SendTo(sendbuf, ep);
}
catch { MessageBox.Show(""); }
}
}
}Server
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace WindowsApplication2
{
public class SSocket
{
bool done = false;
private const int listPort = 1984;//设置端口
IPEndPoint groupEP;
internal void StartListent()
{
UdpClient listent = new UdpClient(listPort);
groupEP = new IPEndPoint(IPAddress.Any, listPort);
try
{
//while (!done)
//{
listent.BeginReceive(new AsyncCallback(IAsyncResult), listent);
//}
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
//listent.Close();
}
private void IAsyncResult(IAsyncResult xx)
{
UdpClient by = (UdpClient)xx.AsyncState;
byte[] bys = by.Receive(ref groupEP);
//string EIP;
string strinfo = Encoding.GetEncoding("gb2312").GetString(bys, 0, bys.Length);
MessageBox.Show(strinfo);
by.Close();
StartListent();
}
}
}出现的问题是,Client需要点击botton1两次Server才监听得到~不知道是那里出错了,请教一下,刚刚学习不久
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;
using System.Net.Sockets;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
this.Send();
}
public void Send()
{
try
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//IPAddress br = Dns.GetHostAddresses("000000")[0];
IPEndPoint ep = new IPEndPoint(IPAddress.Broadcast, 1984);//
byte[] sendbuf = Encoding.GetEncoding("gb2312").GetBytes("000000");
//IPEndPoint ep = new IPEndPoint(br, 1984);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
s.SendTo(sendbuf, ep);
}
catch { MessageBox.Show(""); }
}
}
}Server
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace WindowsApplication2
{
public class SSocket
{
bool done = false;
private const int listPort = 1984;//设置端口
IPEndPoint groupEP;
internal void StartListent()
{
UdpClient listent = new UdpClient(listPort);
groupEP = new IPEndPoint(IPAddress.Any, listPort);
try
{
//while (!done)
//{
listent.BeginReceive(new AsyncCallback(IAsyncResult), listent);
//}
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
//listent.Close();
}
private void IAsyncResult(IAsyncResult xx)
{
UdpClient by = (UdpClient)xx.AsyncState;
byte[] bys = by.Receive(ref groupEP);
//string EIP;
string strinfo = Encoding.GetEncoding("gb2312").GetString(bys, 0, bys.Length);
MessageBox.Show(strinfo);
by.Close();
StartListent();
}
}
}出现的问题是,Client需要点击botton1两次Server才监听得到~不知道是那里出错了,请教一下,刚刚学习不久
不过用的是同步的,异步的没搞过,