views:

38

answers:

1

if i run Server App. Exception occurs: on Dinle.Start()

System.Net.SocketException - Only one usage of each socket address (protocol/network address/port) is normally permitted

How can i solve this error?

alt text 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()); } } } }
+1  A: 

TcpListener.Start is being called multiple times.

1- Called when you start your thread in the Server constructor
2- Via the call to Dinle in the Server_Load event handler
3- Again if you click the button in the btn_Listen_Click event handler

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.

You also seem to be mixing Socket and TcpClient which should not be needed. Take a look at the following like for a basic example of using TcpListener and TcpClient.

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

Chris Taylor
How can i rearrange my codes?
Phsika
@Phsika, did you take a look at the sample that is on the page that I linked to? That sample provides a reasonable starting point for structuring your code.
Chris Taylor