小弟我刚开始学习C# Socket,遇到了不解的问题,请各位高手指教,我的代码如下:
Socket socketSend = new Socket AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(txtSend.Text.Trim());
IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
IPAddress IPAD = IPHE.AddressList[0];
string dd = IPAD.ToString();
IPEndPoint IPEP = new IPEndPoint(IPAD,1979);
socketSend.Connect(IPEP);
int count = socketSend.Send(msg);
socketSend.Shutdown(SocketShutdown.Both);
socketSend.Close();
以上代码运行正常,但我想接受刚发出的消息时却遇到了"通常每个套接字地址(协议/网络地址/端口)只允许使用一次"的问题,不解,代码如下,请各位高手忙我看看,在此多谢了.
Socket socketRecieve = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
IPAddress IPAD = IPHE.AddressList[0];
IPEndPoint IPEP = new IPEndPoint(IPAD,1979);
socketRecieve.Bind(IPEP);
socketRecieve.Listen(10);//在该处出的弹出提示"....如上"
Socket hanlder = socketRecieve.Accept();
byte[] RecieveMsg = new byte[1024];
int count = hanlder.Receive(RecieveMsg);
string str = System.Text.Encoding.ASCII.GetString(RecieveMsg,0,count);
txtReceive.Text = str;
hanlder.Shutdown(SocketShutdown.Both);
hanlder.Close();小弟焦急等待,多谢各位高手了
Socket socketSend = new Socket AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(txtSend.Text.Trim());
IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
IPAddress IPAD = IPHE.AddressList[0];
string dd = IPAD.ToString();
IPEndPoint IPEP = new IPEndPoint(IPAD,1979);
socketSend.Connect(IPEP);
int count = socketSend.Send(msg);
socketSend.Shutdown(SocketShutdown.Both);
socketSend.Close();
以上代码运行正常,但我想接受刚发出的消息时却遇到了"通常每个套接字地址(协议/网络地址/端口)只允许使用一次"的问题,不解,代码如下,请各位高手忙我看看,在此多谢了.
Socket socketRecieve = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
IPAddress IPAD = IPHE.AddressList[0];
IPEndPoint IPEP = new IPEndPoint(IPAD,1979);
socketRecieve.Bind(IPEP);
socketRecieve.Listen(10);//在该处出的弹出提示"....如上"
Socket hanlder = socketRecieve.Accept();
byte[] RecieveMsg = new byte[1024];
int count = hanlder.Receive(RecieveMsg);
string str = System.Text.Encoding.ASCII.GetString(RecieveMsg,0,count);
txtReceive.Text = str;
hanlder.Shutdown(SocketShutdown.Both);
hanlder.Close();小弟焦急等待,多谢各位高手了
解决方案 »
- C#问题求助
- C#如何判断线程池所有的线程执行已结束?
- 求SQL语句
- C# 用串口控件发送命令时...怎么不稳定呢!!!???????????????????????
- C#中是否有显示一行数据的控件?左边是列名称,右边显示列值;是Windows程序
- 使用C#开发通用dll动态链接库
- 如何取得枚举类型结构的元素数量?
- Property Grid 的学习资料,哪个地方有?着急....
- 一个Checkboxgn与TreeView的问题?
- 请问在Beta2版中如何通过数据库控件得到数据库的记录条数?(请教C#问题)
- 微软Visual C# 组的项目经理出的小测验,大家都来试试。。。
- 安装程序的CustomActionData属性设置
Socket socketSend = new Socket AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(txtSend.Text.Trim());
IPHostEntry IPHE = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
IPAddress IPAD = IPHE.AddressList[0];
string dd = IPAD.ToString();
IPEndPoint IPEP = new IPEndPoint(IPAD,1979); //这句,改为
//socketSend.Connect(192.168.0.1(服务器IP),1979);
socketSend.Connect(IPEP);
int count = socketSend.Send(msg);
socketSend.Shutdown(SocketShutdown.Both);
socketSend.Close();
IPHostEntry iphost=Dns.Resolve(System.Environment.MachineName);
IPAddress ipadr=iphost.AddressList[0];
IPEndPoint ipendpoint=new IPEndPoint(ipadr,10000);//终端
this.statusBar1.Panels[0].Text=" 正在侦听....";
this.button1.Enabled=false;
int number=10;
//create a tcp/ip socker
Socket socket1=new Socket(System.Net.Sockets.AddressFamily.InterNetwork,SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp); try
{
//绑定端口
socket1.Bind(ipendpoint);
//开始侦听
socket1.Listen(number);
while(true)
{
//program is suspended while waiting for an incoming connection
Socket mys=socket1.Accept();
string data=null;
Application.DoEvents();
//接受数据
while(true)
{
Application.DoEvents();
byte [] box=new byte[1024];
int len=mys.Receive(box);//len为字节数
data+=Encoding.Default.GetString(box,0,len);
if(data.IndexOf("<TheEnd>")>=0)
{
break;
}
}
data=data.Substring(0,data.Length-8);
this.richTextBox1.Text=data;
data=null;
string s_reply="has recevied";
byte []msg=Encoding.Default.GetBytes(s_reply);
mys.Send(msg);
MessageBox.Show("已经接受到!");
mys.Shutdown(SocketShutdown.Both);
Application.DoEvents();
mys.Close();
}
}
catch(SocketException e0)
{
MessageBox.Show(e0.Message.ToString());
}
catch(ArgumentNullException e1)
{
MessageBox.Show(e1.Message.ToString());
}
catch(Exception e2)
{
MessageBox.Show(e2.Message.ToString());
}