只要发送数据就抛这个异常:由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。无语了,好多年不用了,真的是要学而实习知,老以为自己什么都学会了,其实是你还没有遇到问题,哎,救命啊!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 ThreadSocketServer
{
public partial class Form1 : Form
{
Socket skt=null ;
IPEndPoint ep; byte[] buf = new byte[1024]; public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} void Form1_Load(object sender, EventArgs e)
{ } private void btn_start_Click(object sender, EventArgs e)
{
this.text_receive.Text ="服务正在启动...";
this.text_receive.Text += Environment.NewLine;
this.text_receive.Text += Environment.NewLine;
this.btn_start.Enabled = false; //创建一个Socket,
skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 创建一个结束点,包含一个IP
ep = new IPEndPoint(IPAddress.Parse("192.168.0.170"),6666);
//socket绑定到这个结束点
skt.Bind(ep);
//侦听端口
skt.Listen(6); //一有连接,就做相应处理
//Socket worker = skt.Accept(); skt.BeginAccept(new AsyncCallback(Accept), skt);
this.text_receive.Text += "服务启动成功";
this.text_receive.Text += Environment.NewLine;
} void Accept(IAsyncResult ir)
{
Socket work = ir.AsyncState as Socket; Socket soc = work.EndAccept(ir); try
{
//做个缓存字节数组 //接受内容,放在数组里面 soc.BeginReceive(buf, 0, 1024, SocketFlags.None, new AsyncCallback(Receive), soc);
}
catch (Exception ex)
{ throw ex;
}
finally
{ }
}
void Receive(IAsyncResult ir)
{
//ArrayList al = ir.AsyncState as ArrayList;
//Socket worker = al[0] as Socket;
//byte[] buf = al[1] as byte[]; Socket std = ir.AsyncState as Socket; try
{ int btcout = std.EndReceive(ir);
string result = std.RemoteEndPoint.ToString() + ":" + Encoding.GetEncoding("gb2312").GetString(buf, 0, btcout); text_receive.Text += result;
text_receive.Text += Environment.NewLine;
Send("0001"); std.BeginReceive(buf, 0, 1024, SocketFlags.None, new AsyncCallback(Receive), std);
}
catch (Exception ex)
{
MessageBox.Show("此链接被强迫关闭" + ex.Message);
} // 从数组里读出内容
} private void btn_send_Click(object sender, EventArgs e)
{ //this.text_receive.Text += Environment.NewLine;
//this.text_receive.Text += this.text_send.Text; string connect = text_send.Text; byte[] bconnect = Encoding.GetEncoding("gb2312").GetBytes(connect); //skt.BeginSend(bconnect, 0, bconnect.Length, SocketFlags.None, new AsyncCallback(Send), skt);
Send(connect);
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="str"></param>
public void Send(string str)
{
try
{
byte[] bytesdata = Encoding.Default.GetBytes(str);
skt.BeginSend(bytesdata, 0, bytesdata.Length, SocketFlags.None, new AsyncCallback(SendCallback), skt);
skt.BeginReceive(buf, 0, 1024, SocketFlags.None, new AsyncCallback(Receive), skt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} /// <summary>
/// 发送数据回调函数
/// </summary>
/// <param name="iar"></param>
private void SendCallback(IAsyncResult iar)
{
try
{
Socket s = (Socket)iar.AsyncState;
int sent = s.EndSend(iar);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void Send(IAsyncResult ir)
{
Socket soc = ir.AsyncState as Socket;
int icount = soc.EndSend(ir);
} private void text_send_TextChanged(object sender, EventArgs e)
{ }
}
}
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 ThreadSocketServer
{
public partial class Form1 : Form
{
Socket skt=null ;
IPEndPoint ep; byte[] buf = new byte[1024]; public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} void Form1_Load(object sender, EventArgs e)
{ } private void btn_start_Click(object sender, EventArgs e)
{
this.text_receive.Text ="服务正在启动...";
this.text_receive.Text += Environment.NewLine;
this.text_receive.Text += Environment.NewLine;
this.btn_start.Enabled = false; //创建一个Socket,
skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 创建一个结束点,包含一个IP
ep = new IPEndPoint(IPAddress.Parse("192.168.0.170"),6666);
//socket绑定到这个结束点
skt.Bind(ep);
//侦听端口
skt.Listen(6); //一有连接,就做相应处理
//Socket worker = skt.Accept(); skt.BeginAccept(new AsyncCallback(Accept), skt);
this.text_receive.Text += "服务启动成功";
this.text_receive.Text += Environment.NewLine;
} void Accept(IAsyncResult ir)
{
Socket work = ir.AsyncState as Socket; Socket soc = work.EndAccept(ir); try
{
//做个缓存字节数组 //接受内容,放在数组里面 soc.BeginReceive(buf, 0, 1024, SocketFlags.None, new AsyncCallback(Receive), soc);
}
catch (Exception ex)
{ throw ex;
}
finally
{ }
}
void Receive(IAsyncResult ir)
{
//ArrayList al = ir.AsyncState as ArrayList;
//Socket worker = al[0] as Socket;
//byte[] buf = al[1] as byte[]; Socket std = ir.AsyncState as Socket; try
{ int btcout = std.EndReceive(ir);
string result = std.RemoteEndPoint.ToString() + ":" + Encoding.GetEncoding("gb2312").GetString(buf, 0, btcout); text_receive.Text += result;
text_receive.Text += Environment.NewLine;
Send("0001"); std.BeginReceive(buf, 0, 1024, SocketFlags.None, new AsyncCallback(Receive), std);
}
catch (Exception ex)
{
MessageBox.Show("此链接被强迫关闭" + ex.Message);
} // 从数组里读出内容
} private void btn_send_Click(object sender, EventArgs e)
{ //this.text_receive.Text += Environment.NewLine;
//this.text_receive.Text += this.text_send.Text; string connect = text_send.Text; byte[] bconnect = Encoding.GetEncoding("gb2312").GetBytes(connect); //skt.BeginSend(bconnect, 0, bconnect.Length, SocketFlags.None, new AsyncCallback(Send), skt);
Send(connect);
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="str"></param>
public void Send(string str)
{
try
{
byte[] bytesdata = Encoding.Default.GetBytes(str);
skt.BeginSend(bytesdata, 0, bytesdata.Length, SocketFlags.None, new AsyncCallback(SendCallback), skt);
skt.BeginReceive(buf, 0, 1024, SocketFlags.None, new AsyncCallback(Receive), skt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} /// <summary>
/// 发送数据回调函数
/// </summary>
/// <param name="iar"></param>
private void SendCallback(IAsyncResult iar)
{
try
{
Socket s = (Socket)iar.AsyncState;
int sent = s.EndSend(iar);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void Send(IAsyncResult ir)
{
Socket soc = ir.AsyncState as Socket;
int icount = soc.EndSend(ir);
} private void text_send_TextChanged(object sender, EventArgs e)
{ }
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货