/****************************************************************** * purpose: Implement tcp server, listen and receive the tcpip data, * then send data to com port * parameter: ********************************************************************/ public void Server() { // Buffer for reading data Byte[] bytes = new Byte[256]; listener = new TcpListener(IPAddress.Any, _port); listener.Start(); // Enter the listening loop. while (true) { try { _serialPort.readTCPListener(this); // Perform a blocking call to accept requests. // You could also use server.AcceptSocket() here. client = listener.AcceptTcpClient(); // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { string strReceiveIPData = System.Text.Encoding.ASCII.GetString(bytes, 0, i); if (_serialPort != null && _serialPort.IsPortOpen()) { _serialPort.port_write(bytes, i); } } } catch (Exception ex) { Trace.WriteLine(DateTime.Now + " TCP/IP Exception: " + ex.Message); listener.Stop(); listener = new TcpListener(IPAddress.Any, _port); listener.Start(); } } }
private void ReadCallback(IAsyncResult ar) { String content = String.Empty; // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesRead = 0; try { // Read data from the client socket. bytesRead = handler.EndReceive(ar); } catch (Exception exp) { Console.WriteLine("Read port exception " + exp.Message); bytesRead = 0; } if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString( state.buffer, 0, bytesRead)); // Check for end-of-file tag. If it is not there, read // more data. content = state.sb.ToString(); Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content); remoteIpEndPoint = (IPEndPoint)handler.RemoteEndPoint; string remoteIdEndPointAddress = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString(); string remoteIpEndPointPort = ((IPEndPoint)handler.RemoteEndPoint).Port.ToString(); //localIpEndPoint = handler.LocalEndPoint as IPEndPoint; // Using the RemoteEndPoint property. Console.WriteLine("I am connected to " + remoteIdEndPointAddress + " on port number " + remoteIpEndPointPort); // Using the LocalEndPoint property. Console.WriteLine("My local IpAddress is :" + IPAddress.Parse(((IPEndPoint)handler.LocalEndPoint).Address.ToString()) + " I am connected on port number " + ((IPEndPoint)handler.LocalEndPoint).Port.ToString()); Send(handler, " Echo " + content); //test ok //Send1(content + " Send back2"); if (_serialPort != null && _serialPort.IsPortOpen()) { _serialPort.port_write(content); } else { //Console.WriteLine("COM PORT not open!!!"); // Not all data received. Get more. handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } state.sb.Clear(); //handler.Shutdown(SocketShutdown.Both); // handler.Close(); } }