【问题标题】:Running Network Application on port Server side give me error how can i solve it?在端口服务器端运行网络应用程序给我错误我该如何解决?
【发布时间】:2010-05-08 17:56:48
【问题描述】:

如果我运行服务器应用程序。发生异常:在 Dinle.Start() 上

System.Net.SocketException - 每个套接字地址(协议/网络地址/端口)通常只允许使用一次

我该如何解决这个错误?

服务器.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Server
{
    public partial class Server : Form
    {
        Thread kanal;
        public Server()
        {
            InitializeComponent();

            try
            {
                kanal = new Thread(new ThreadStart(Dinle));
                kanal.Start();
                kanal.Priority = ThreadPriority.Normal;
                this.Text = "Kanla Çalıştı";
            }
            catch (Exception ex)
            {
                this.Text = "kanal çalışmadı";
                MessageBox.Show("hata:" + ex.ToString());
                kanal.Abort();
                throw;
            }
        }

        private void Server_Load(object sender, EventArgs e)
        {
            Dinle();
        }
        private void btn_Listen_Click(object sender, EventArgs e)
        {

            Dinle();
        }

        void Dinle()
        {
          //  IPAddress localAddr = IPAddress.Parse("localhost");
            // TcpListener server = new TcpListener(port);
           // server = new TcpListener(localAddr, port);
            //TcpListener Dinle = new TcpListener(localAddr,51124);
            TcpListener Dinle = new TcpListener(51124);
            try
            {

                while (true)
                {
                   

Dinle.Start();

Exception is occured. Socket Baglanti = Dinle.AcceptSocket(); if (!Baglanti.Connected) { MessageBox.Show("Baglanti Yok"); } else { TcpClient tcpClient = Dinle.AcceptTcpClient(); if (tcpClient.ReceiveBufferSize > 0) { byte[] Dizi = new byte[250000]; Baglanti.Receive(Dizi, Dizi.Length, 0); string Yol; saveFileDialog1.Title = "Dosyayi kaydet"; saveFileDialog1.ShowDialog(); Yol = saveFileDialog1.FileName; FileStream Dosya = new FileStream(Yol, FileMode.Create); Dosya.Write(Dizi, 0, Dizi.Length - 20); Dosya.Close(); listBox1.Items.Add("dosya indirildi"); listBox1.Items.Add("Dosya Boyutu=" + Dizi.Length.ToString()); listBox1.Items.Add("İndirilme Tarihi=" + DateTime.Now); listBox1.Items.Add("--------------------------------"); } } } } catch (Exception ex) { MessageBox.Show("hata:" + ex.ToString()); } } } }

【问题讨论】:

    标签: c# .net visual-studio visual-studio-2008 network-programming


    【解决方案1】:

    TcpListener.Start 被多次调用。

    1- 在服务器构造函数中启动线程时调用
    2- 通过在 Server_Load 事件处理程序中调用 Dinle
    3- 如果您再次单击 btn_Listen_Click 事件处理程序中的按钮

    我并没有声称完全掌握您要做什么,但我认为这可以简化。

    首先,您应该创建并启动一次侦听器,假设代码何时开始运行。之后,您可以进入一个调用 AcceptTcpClient 来接受连接并处理通信的循环。

    您似乎还混合了不应需要的 Socket 和 TcpClient。使用 TcpListener 和 TcpClient 的基本示例请看下面的样例。

    http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

    【讨论】:

    • @Phsika,您是否看过我链接到的页面上的示例?该示例为构建代码提供了一个合理的起点。
    猜你喜欢
    • 2019-09-28
    • 2022-12-20
    • 2012-01-17
    • 2015-07-18
    • 2017-07-27
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多