错误:附加信息:通常只允许使用每个套接字地址(协议/网络地址/端口) [英] ERROR: additional information: only one usage of each socket address (protocol/network address/port) is normally permitted

查看:73
本文介绍了错误:附加信息:通常只允许使用每个套接字地址(协议/网络地址/端口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#进行WinSock套接字编程。我基本上使客户端/服务器应用程序在客户端和服务器之间发送数据。我在c#中使用winsock控件。



当我尝试在客户端和服务器之间发送数据时,我收到错误。我的服务器侦听端口514上的连接,



服务器



< pre lang =C#> 使用系统;
使用 System.Collections.Generic;
使用 System.Windows.Forms;
使用 System.Net;
使用 System.Threading;
使用 System.Net.Sockets;

命名空间服务器
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
const string DEFAULT_SERVER =& quot; xx.x.xx .XXX&安培; QUOT ;;
const int DEFAULT_PORT = 514 ;

System.Net.Sockets.Socket serverSocket;
System.Net.Sockets.SocketInformation serverSocketInfo;
private void Form1_Load( object sender,EventArgs e)
{


}
public string Startup()
{

IPHostEntry hostInfo = Dns.GetHostByName(DEFAULT_SERVER);
IPAddress serverAddr = hostInfo.AddressList [ 0 ];
var serverEndPoint = new IPEndPoint(serverAddr,DEFAULT_PORT);
serverSocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System .Net.Sockets.ProtocolType.Tcp);
serverSocket.Bind(serverEndPoint);
return serverSocket.LocalEndPoint.ToString();
}

public string Listen()
{
int backlog = 0 ;
尝试
{
serverSocket.Listen(backlog);
return & quot;服务器监听& quot ;;
}
catch (例外情况)
{
return & quot;听不到& quot; + ex.ToString();
}
}
public string ReceiveData()
{
System.Net.Sockets.Socket receiveSocket;
byte [] buffer = new byte [ 256 ];

receiveSocket = serverSocket.Accept();

var bytesrecd = receiveSocket.Receive(buffer);

receiveSocket.Close();

System.Text.Encoding encoding = System.Text.Encoding.UTF8;

return encoding.GetString(buffer);
}


私有 void button1_Click(< span class =code-keyword> object sender,EventArgs e)
{
string serverInfo = Startup();
textBox1.Text =& quot;服务器始于:& quot; + serverInfo;

serverInfo = Listen();
textBox1.Text = serverInfo;

serverInfo = ReceiveData();
textBox1.Text = serverInfo;

}

private void wsa_DataArrival(< span class =code-keyword> object sender,AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
ReceiveData();
听();
}

private void wsa_ConnectEvent( object sender,EventArgs e)
{
Listen();
}
}
}





客户



 使用系统; 
使用 System.Net.Sockets;
使用 System.Threading;
使用 System.Windows.Forms;

命名空间客户
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
布尔 isConnect = false ;
private void btnConnect_Click( object sender,EventArgs e)
{
try
{
win.Close();


win.Connect(txtIP.Text,txtPort.Text);

}

catch (System.Windows.Forms.AxHost.InvalidActiveXStateException g)
{
txtdata.Text + = \ n + g.ToString();
}
}

私有 void btnSend_Click( object sender,EventArgs e)
{
try
{
if (isConnect)
{
win.txtsenddata(txtdata.Text);
DataInput.Text + = \ nClent(Client :): + txtdata。文本;
txtsenddata.Text = ;
}
else
MessageBox.Show( 您没有连接到任何主机);
}
catch (AxMSWinsockLib.AxWinsock.InvalidActiveXStateException g)
{
txtdata.Text + = \ n + g.ToString();
}
catch (例外情况)
{
txtdata.Text + = \ n + ex.Message;
}
}

void win_Error( object sender,AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)
{
txtdata.Text + = \ n-错误: + e.description;
isConnect = false ;
}

private void win_DataArrival( object sender,AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{

String data = ;

对象 dat =( object )数据;
w1.GetData( ref dat);

data =( String )dat;


txtdata.Text + = \ nServer - + w1.RemoteHostIP + + data;

}


private void win_ConnectEvent( object sender,EventArgs e)
{
txtdata.Text + = \ n - Connect Event: + w1.RemoteHostIP;


isConnect = true ;
}







它第一次正常工作,但在第二次它给我一个错误

 '  System.Net.Sockets.SocketException'类型的未处理异常发生  System.dll附加信息:每个套接字地址(协议/网络地址/端口)只有一种用法是通常允许

有人帮我解决我的问题。



我尝试了什么:



我试图将数据从客户端多次提取到服务器但是第二次只有一次工作时它给我一个错误未处理System.dll中出现'System.Net.Sockets.SocketException'类型的异常附加信息:服务器程序中通常只允许使用每个套接字地址(协议/网络地址/端口)



serverSocket.Bind(serverEndPoint);

解决方案

你可以在MSDN上找到很好的例子:套接字代码示例 [ ^ ]



你会找到两个例子同步和异步服务器和客户端。

很快你可能会发现你想让服务器部分异步。



注意:您可以使用 Socket.Connected属性(System.Net.Sockets) [ ^ ]



Thi s没有直接回答你的问题,但是当你处于开发的初期时,重新开始使用一个有效的示例代码并从那里继续编码可能是一个好主意。


I'am working on WinSock Sockets Programming with C#. I'm basically making a Client/Server application sending an data between client and the server. I'm using winsock control in c#.

I get the error when I try sending an data between client and the server. My server listens for connections on port 514,

server

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Net.Sockets;

namespace server
{
    public partial class Form1 : Form
    {      
        public Form1()
        {
            InitializeComponent();        
        }
        const string DEFAULT_SERVER = &quot;xx.x.xx.xxx&quot;;
        const int DEFAULT_PORT = 514;

        System.Net.Sockets.Socket serverSocket;
        System.Net.Sockets.SocketInformation serverSocketInfo;
        private void Form1_Load(object sender, EventArgs e)
        {
           

        }
        public string Startup()
        {
           
            IPHostEntry hostInfo = Dns.GetHostByName(DEFAULT_SERVER);
            IPAddress serverAddr = hostInfo.AddressList[0];
            var serverEndPoint = new IPEndPoint(serverAddr, DEFAULT_PORT);            
            serverSocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
            serverSocket.Bind(serverEndPoint);
            return serverSocket.LocalEndPoint.ToString();
        }

        public string Listen()
        {
            int backlog = 0;
            try
            {
                serverSocket.Listen(backlog);
                return &quot;Server listening&quot;;
            }
            catch (Exception ex)
            {
                return &quot;Failed to listen&quot; + ex.ToString();
            }
        }
        public string ReceiveData()
        {
            System.Net.Sockets.Socket receiveSocket;
            byte[] buffer = new byte[256];

            receiveSocket = serverSocket.Accept();

            var bytesrecd = receiveSocket.Receive(buffer);

            receiveSocket.Close();

            System.Text.Encoding encoding = System.Text.Encoding.UTF8;

            return encoding.GetString(buffer);
        }
       

        private void button1_Click(object sender, EventArgs e)
        {
            string serverInfo = Startup();
            textBox1.Text = &quot;Server started at:&quot; + serverInfo;

            serverInfo = Listen();
            textBox1.Text = serverInfo;
            
            serverInfo = ReceiveData();
            textBox1.Text = serverInfo;

        }

        private void wsa_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            ReceiveData();
            Listen();
        }

        private void wsa_ConnectEvent(object sender, EventArgs e)
        {
            Listen();
        }
    }
}



Client

using System;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace CLIENT
{
    public partial class Form1 : Form
    {
public Form1()
        {
            InitializeComponent();
        }
       Boolean isConnect= false;
private void btnConnect_Click(object sender,EventArgs e)
{
    try
    {
        win.Close();

       
        win.Connect(txtIP.Text, txtPort.Text);

    }
   
    catch (System.Windows.Forms.AxHost.InvalidActiveXStateException g)
    { 
        txtdata.Text += "\n" + g.ToString(); 
    }
}

private void btnSend_Click(object sender, EventArgs e)
{
    try
    {
        if (isConnect)
        {          
            win.txtsenddata(txtdata.Text);          
            DataInput.Text += "\nClent(Client:) : " + txtdata.Text;   
            txtsenddata.Text = "";
        }
        else
            MessageBox.Show("You are not connect to any host ");
    }
    catch (AxMSWinsockLib.AxWinsock.InvalidActiveXStateException g)
    {
        txtdata.Text += "\n" + g.ToString(); 
    }
    catch (Exception ex) 
    { 
        txtdata.Text +="\n" + ex.Message; 
    }
}

void win_Error(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)
{
    txtdata.Text += "\n- Error : " + e.description;
    isConnect = false;
}

private void win_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{

    String data ="";

    Object dat = (object)data;
    w1.GetData(ref dat);

    data = (String)dat;


    txtdata.Text += "\nServer - " + w1.RemoteHostIP + " : " + data ;

}


private void win_ConnectEvent(object sender, EventArgs e)
{   
    txtdata.Text += "\n - Connect Event : " + w1.RemoteHostIP;


    isConnect = true;
}




It workes 1st time correctly but in second time it gives me an error

An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll  Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted

someone help me to solve my problem please.

What I have tried:

I'm trying to get the data from the client to server on many times but it worker only one time on second time it gives me an error An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted on the line in the server program

serverSocket.Bind(serverEndPoint);

解决方案

You can find pretty good examples on MSDN: Socket Code Examples[^]

You will find examples for both synchronous and asynchronous servers and clients.
Pretty soon you will probably find that you would like to make the server part asynchronous.

Note: Instead of using your own variable to keep track of the connection status, you can use Socket.Connected Property (System.Net.Sockets)[^]

This doesn't answer your question directly, but as you are in the beginning of your development it might be a good idea to start over with a working example code and continue your coding from there.


这篇关于错误:附加信息:通常只允许使用每个套接字地址(协议/网络地址/端口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆