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;
using System.Threading;namespace UDPclient//收到ECHO 就应答EOO1
{
public partial class Form1 : Form
{
UdpClient udp; public Form1()
{
CheckForIllegalCrossThreadCalls = false;
udp = new UdpClient(11000);
InitializeComponent();
Thread startThread = new Thread(new ThreadStart(listen));
startThread.IsBackground = true;
startThread.Start(); } private void Form1_Load(object sender, EventArgs e)
{ }
public void listen()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.1.80"),11000);
while(true)
{
string strTemp = System.Text.Encoding.UTF8.GetString(udp.Receive(ref ip));//出错了,
String[] strArray= strTemp.Split('O') ;
strTemp = strArray[0];
//strTemp += "0";
listBox1.Items.Add(strTemp);
if (strTemp == "ECH")
{
string iii = strArray[1];
byte[] byteTemp = System.Text.Encoding.UTF8.GetBytes("E001");
udp.Send(byteTemp, byteTemp.Length,IPAddress.Parse(strArray[1]).ToString(), 11000);// }
}
}
}
}
服务端 发送数据ECHO,
namespace UDP001
{
public partial class Form1 : Form
{
static string sttemp="";
UdpClient udp ;
public Form1()
{
udp = new UdpClient();
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void btnSend_Click(object sender, EventArgs e)
{
//提取本地的IP地址。
/*
IPAddress[] ips = Dns.GetHostAddresses(Environment.MachineName);
string strIps = "";
for (int i = 0; i < ips.Length;i++ )
{
strIps += ips[i].ToString();
}
string temp = txtSend.Text + strIps;
*/ //提取本地的计算机名
string strHostName = Dns.GetHostName(); string temp = txtSend.Text + strHostName;
byte[] tempbyte = System.Text.Encoding.UTF8.GetBytes(temp);
udp.Send(tempbyte, tempbyte.Length,"192.168.1.80",11000);
Thread startThread = new Thread(new ThreadStart(listen));
startThread.IsBackground = true;
startThread.Start();
Thread.Sleep(100);
}
public void listen()
{
string strTemp="";
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.1.80"), 11000);
while (true)
{
strTemp = System.Text.Encoding.UTF8.GetString(udp.Receive(ref ip));
txtReceive.Text=strTemp;
this.Close();
}
sttemp = strTemp;
}
}
}
帮我看看错在哪里
我服务器能收到ECHO就是不能应答EOO1
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;
using System.Threading;namespace UDPclient//收到ECHO 就应答EOO1
{
public partial class Form1 : Form
{
UdpClient udp; public Form1()
{
CheckForIllegalCrossThreadCalls = false;
udp = new UdpClient(11000);
InitializeComponent();
Thread startThread = new Thread(new ThreadStart(listen));
startThread.IsBackground = true;
startThread.Start(); } private void Form1_Load(object sender, EventArgs e)
{ }
public void listen()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.1.80"),11000);
while(true)
{
string strTemp = System.Text.Encoding.UTF8.GetString(udp.Receive(ref ip));//出错了,
String[] strArray= strTemp.Split('O') ;
strTemp = strArray[0];
//strTemp += "0";
listBox1.Items.Add(strTemp);
if (strTemp == "ECH")
{
string iii = strArray[1];
byte[] byteTemp = System.Text.Encoding.UTF8.GetBytes("E001");
udp.Send(byteTemp, byteTemp.Length,IPAddress.Parse(strArray[1]).ToString(), 11000);// }
}
}
}
}
服务端 发送数据ECHO,
namespace UDP001
{
public partial class Form1 : Form
{
static string sttemp="";
UdpClient udp ;
public Form1()
{
udp = new UdpClient();
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void btnSend_Click(object sender, EventArgs e)
{
//提取本地的IP地址。
/*
IPAddress[] ips = Dns.GetHostAddresses(Environment.MachineName);
string strIps = "";
for (int i = 0; i < ips.Length;i++ )
{
strIps += ips[i].ToString();
}
string temp = txtSend.Text + strIps;
*/ //提取本地的计算机名
string strHostName = Dns.GetHostName(); string temp = txtSend.Text + strHostName;
byte[] tempbyte = System.Text.Encoding.UTF8.GetBytes(temp);
udp.Send(tempbyte, tempbyte.Length,"192.168.1.80",11000);
Thread startThread = new Thread(new ThreadStart(listen));
startThread.IsBackground = true;
startThread.Start();
Thread.Sleep(100);
}
public void listen()
{
string strTemp="";
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.1.80"), 11000);
while (true)
{
strTemp = System.Text.Encoding.UTF8.GetString(udp.Receive(ref ip));
txtReceive.Text=strTemp;
this.Close();
}
sttemp = strTemp;
}
}
}
帮我看看错在哪里
我服务器能收到ECHO就是不能应答EOO1
while (true)
{
strTemp = System.Text.Encoding.UTF8.GetString(udp.Receive(ref ip));
txtReceive.Text=strTemp;
this.Close();
}你这里需要循环接受吗?