Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. socket usage permission

socket usage permission

Scheduled Pinned Locked Moved C#
sysadminwpfwcfhelpquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mahraja
    wrote on last edited by
    #1

    Hi I recieve this exception: "Only one usage of each socket address (protocol/network address/port) is normally permitted" I should get several files from an ftp server. I implemented it by sockets so: create the datasocket, recieve the file and then close data socket. But the next time I do this for another file(create data socket with Endpoint (the same local address/another port)), i recieve this exception message when Binding the socket. Can anyone tell me why? And the way to solve that? (the code, if necessary)

    public void download(string remFileName, string locFileName)
    {
    if (!logined)
    {
    login();
    }

    setBinaryMode(true);
    Console.WriteLine("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
    
    if (locFileName.Equals(""))
    {
        locFileName = remFileName;
    }
    
    if (!File.Exists(locFileName))
    {
        Stream st = File.Create(locFileName);
        st.Close();
    }
    
    FileStream output = new FileStream(locFileName, FileMode.Open);
    
    createDataSocket(true);
    if (dataSocket != null)
    {
        throw new SocketException();
    }
    //\*\*\*\*
    sendCommand("RETR " + remFileName);
    
    if (!(retValue == 150 || retValue == 125))
    {
        throw new IOException(reply.Substring(4));
    }
    //\*\*\*\*
    
    dataSocket = listeningSocket.Accept();
    listeningSocket.Close();
    listeningSocket = null;
    if (dataSocket == null)
    {
        throw new Exception("Winsock error: " +
                Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
    }
    
    while (true)
    {
        bytes = dataSocket.Receive(buffer, buffer.Length, 0);
        output.Write(buffer, 0, bytes);
    
        if (bytes <= 0)
        {
            break;
        }
    }
    
    output.Close();
    
    if (dataSocket.Connected)
    {
        //dataSocket.Shutdown(SocketShutdown.Both);
        dataSocket.Close();
        dataSocket = null;
    }
    
    Console.WriteLine("");
    
    readReply();
    
    if (!(retValue == 226 || retValue == 250))
    {
        throw new IOException(reply.Substring(4));
    }
    

    }

    public void createDataSocket(bool flag)
    {
    try
    {
    listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    }
    catch (Exception e)
    {
    ftpLog(e.Message);
    throw new IOException(e.Message);
    }

    string localAddress = clientSocket.LocalEndPoint.To
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups