Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one Biometric device for Attendence,and the device is push device,So I want to retrieve data from the server port where this device is pushing the data,How can I do that.Inmy below mention code the page keeps on loading and giving the error as
"Error: Only one usage of each socket address (protocol/network address/port) is normally permitted ".

What I have tried:

C#
private void ReadServerData()
{
  TcpClient tcpClient = new TcpClient();
  TcpListener tcpListener = new TcpListener(IPAddress.Any,7005);
  tcpListener.Start();
  Socket mySocket = tcpListener.AcceptSocket();
  NetworkStream stream = new NetworkStream(mySocket);
  StreamReader reader;
  StreamWriter writer;
  string received = "";
  while (true)
  {
    tcpClient = tcpListener.AcceptTcpClient();

    stream = tcpClient.GetStream();
    reader = new StreamReader(stream);
    writer = new StreamWriter(stream);
    writer.NewLine = "\r\n";
    writer.AutoFlush = true;
    byte[] bytes = new byte[tcpClient.SendBufferSize];
    int recv = 0;
    while (true)
    {
      recv = stream.Read(bytes, 0, tcpClient.SendBufferSize);
      received += System.Text.Encoding.ASCII.GetString(bytes, 0, recv);

      if (received.EndsWith("\n\n"))
      {
        break;
      }
    }
  }
}
Posted
Updated 30-Jul-17 3:22am
v2
Comments
Michael_Davies 30-Jul-17 5:43am    
That error would usually mean that some other application is already hooked onto the port for listening, close that application.

To see which application open a cmd prompt and type netstat -an
Member 12749751 30-Jul-17 6:24am    
after performing netstat -an command the above mention port is not there and at debugging when it comes to cpListener.AcceptTcpClient() line the page keeps on loading
Michael_Davies 30-Jul-17 6:30am    
Are you calling ReadServerData more than once?

You attach to the port, execute your loop and break on \n\n but do not disconnect the listener, which means if you execute it again it will try to attach a new listener.
Member 12749751 30-Jul-17 6:54am    
Sir,Actually I am not using Read Server Data,this code i have taken from some where else I have one biometric device connect with my LAN and i want to fetch the data from that machine ,So how can i do that??Please help me with this
Michael_Davies 30-Jul-17 9:11am    
So you have an application that gives an error and you show code that is relevant to that error but it is not the code and you are not using it, people can only help with the question you ask and the code you show otherwise you are just wasting other peoples time.

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