简体   繁体   English

在端口服务器端运行网络应用程序给我错误,我该如何解决?

[英]Running Network Application on port Server side give me error how can i solve it?

if i run Server App. 如果我运行服务器应用程序。 Exception occurs: on Dinle.Start() 发生异常:在Dinle.Start()上

System.Net.SocketException - Only one usage of each socket address (protocol/network address/port) is normally permitted System.Net.SocketException-每个套接字地址(协议/网络地址/端口)仅允许使用一种

How can i solve this error? 我该如何解决这个错误?

替代文字 Server.cs Server.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()); } } } }

TcpListener.Start is being called multiple times. TcpListener.Start被多次调用。

1- Called when you start your thread in the Server constructor 1-在服务器构造函数中启动线程时调用
2- Via the call to Dinle in the Server_Load event handler 2-通过调用Server_Load事件处理程序中的Dinle
3- Again if you click the button in the btn_Listen_Click event handler 3-再一次,如果您单击btn_Listen_Click事件处理程序中的按钮

I do not claim to have a complete grasp of what you are trying to do but I think this can be simplified. 我并不声称完全了解您要做什么,但是我认为这可以简化。

First you should create and start the listener once, lets say when the code starts running. 首先,您应该创建并启动一次侦听器,让我们说一下代码何时开始运行。 After that you can enter into a loop that calls AcceptTcpClient to accept connection and handle the communication. 之后,您可以进入一个循环,该循环调用AcceptTcpClient接受连接并处理通信。

You also seem to be mixing Socket and TcpClient which should not be needed. 您似乎还混用了不需要的Socket和TcpClient。 Take a look at the following like for a basic example of using TcpListener and TcpClient. 看一下以下内容,作为使用TcpListener和TcpClient的基本示例。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决“/”应用程序索引尚未初始化的服务器错误? - How can I solve Server Error in '/' Application Index has not been initialized? 如何使用udp端口停止从一个节点到另一个节点的网络应用程序? - how can i stop a network application from one node to other using udp port? XOR 字符串解密给我的 output 低于 0 错误。 我怎样才能绕过它? - XOR String Decryption give me as output below 0 Error. How can I bypass it? 我怎样才能得到 WCF Routing 给我一个比没有匹配的 MessageFilter 更详细的错误消息? - How can I get WCF Routing to give me a more detailed error message than - No matching MessageFilter? 如何将连接字符串提供给网络上的服务器? - How do I give a connection string to a server on a network? 如何从服务器端应用程序访问客户端dll? - How can I access a client side dll from a server side application? 串口读取结果包含响应但是我给出的命令,如何解决? - Serial port read result contains response but also the command I give, how to solve this? 动态数据Web应用程序给我错误? - Dynamic Data Web Application give me error? 如何使用MVC Web应用程序中的服务器端代码重定向到远程服务器上的网站? - How can I redirect to a website on a remote server using server side code in an MVC web application? 500内部服务器错误。 我如何在服务器上的 asp.net MVC 3 中解决它? - 500 - Internal server error. how i can solve it in asp.net MVC 3 on server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM