Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following error in my code: Only one usage of each socket address (protocol/network address/port) is normally permitted
Please Help!!


Server side coding

C#
class FTServerCode
    {
        IPEndPoint ipEnd;
        Socket sock;
        public FTServerCode()
        {
           ipEnd = new IPEndPoint(IPAddress.Any, 5656);
           sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
           sock.Bind(ipEnd);
           
        }
        public static string receivedPath;
        public static string curMsg = "Stopped";
        public  void StartServer()
        {
            try
            {
                curMsg = "Starting...";
                sock.Listen(100);

                curMsg = "Running and waiting to receive file.";
                Socket clientSock = sock.Accept();

                byte[] clientData = new byte[1024 * 5000];
                
                int receivedBytesLen = clientSock.Receive(clientData);
                curMsg = "Receiving data...";

                int fileNameLen = BitConverter.ToInt32(clientData, 0);
                string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

                BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath +"/"+ fileName, FileMode.Append)); ;
                bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

                curMsg = "Saving file...";

                bWrite.Close();
                clientSock.Close();
                curMsg = "Recieved & Saved file; Server Stopped.";


                
            }
            catch (Exception )
            {
                curMsg = "File Receving error.";
            }
        }
    }
}


Client side coding

C#
ipEnd = new IPEndPoint(IPAddress.Any, 5656);
           sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
           sock.Bind(ipEnd);

           while (true)
           {
               try
               {
                   fileini = "";
                   //curMsg = "Starting...";
                   sock.Listen(100);

                   //curMsg = "Running and waiting to receive file.";
                   Socket clientSock = sock.Accept();

                   byte[] clientData = new byte[1024 * 5000];

                   int receivedBytesLen = clientSock.Receive(clientData);
                   //curMsg = "Receiving data...";

                   int fileNameLen = BitConverter.ToInt32(clientData, 0);
                   fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
                  sq.open();
                  cmd = new SqlCommand("select * from valid",sq.cn);
                  dr = cmd.ExecuteReader();
                  while (dr.Read())
                  {
                      if (dr.HasRows)
                      {
                          fileini = dr[0].ToString();
                      }
                  }
                  if (fileini == "" || fileName != fileini)
                  {

                      receivedPath = "";
                      db();
                  }
                  else
                  {
                      receivedPath = "D:\\Recieve";
                      db();
                  }
                  dr.Close();
                  sq.close();
                  if (fileName.Contains("&"))
                  {
                      encry();

                  }



                   BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + "/" + fileName, FileMode.Append)); ;
                   bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

                 //lblMess.Text = "Saving file...";

                   bWrite.Close();
                   clientSock.Close();

                   //curMsg = "Reeived & Saved file; Server Stopped.";
               }
               catch (Exception ex)
               {
               //    curMsg = "File Receving error.";
               }
           }
       }
Posted
Updated 3-Feb-15 23:18pm
v4
Comments
Richard MacCutchan 4-Feb-15 5:19am    
Your server and client both appear to be listening for each other to start the conversation.
Prabhu Sridharan 5-Feb-15 5:05am    
Thanks a lot!! I will try using

1 solution

Your client should use Socket.Connect. Not both the server and client start listening. Not very usefull because, as in the real world, there would not be any conversation if all people would do is listen, waiting for someone to speak. There must be someone (in this case a client) that starts the communication by connecting to a willing other (in this case a listening server).

https://msdn.microsoft.com/en-us/library/4xzx2d41%28v=vs.110%29.aspx[^]

Good luck!
 
Share this answer
 
Comments
Prabhu Sridharan 5-Feb-15 4:14am    
Thanks a lot. Really a good information.
Prabhu Sridharan 5-Feb-15 5:13am    
Can you tell me how to write socket.connect in the above code.

Thanks in advance.
E.F. Nijboer 5-Feb-15 9:53am    
You can find examples here on codeproject, for example this one:
http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
E.F. Nijboer 5-Feb-15 9:53am    
http://www.codeproject.com/Articles/12286/Simple-Client-server-Interactions-using-C
Prabhu Sridharan 5-Feb-15 23:44pm    
Thanks a lot!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900