thank you Dave1374,
i have tryed the substring and indexOf() but the variable data contains "user textuser http/1.1"....i have used again indexOf() on data and work fine...but when i try to write the text, the server web works fine but after 5-6 tests doesn't work....Why?
Help me please Dave...
i post the code that i have used:
class Server : IDisposable
{
private Socket socket = null;
public Server()
{
//Initialize Socket class
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Request and bind to an IP from DHCP server
socket.Bind(new IPEndPoint(IPAddress.Any, 9050));
//Start listen for web requests
socket.Listen(10);
ListenForRequest();
}
public void ListenForRequest()
{
while (true)
{
using (Socket clientSocket = socket.Accept())
{
Thread.Sleep(10);
//Get clients IP
IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
EndPoint clientEndPoint = clientSocket.RemoteEndPoint;
int bytesReceived = clientSocket.Available;
if (bytesReceived > 0)
{
//code html
string response3 = "<!DOCTYPE html>";
response3 += "<html>";
response3 +="<head>";
response3 +="<title>Netduino Plus</title>";
response3 +="</head>";
response3 +="<body>";
response3 +="<h1>Netduino Plus 2</h1>";
response3 += "<form name="+"input"+" action="+"localhost"+" method="+"get"+">"
+" Username: <input type="+"text"+" name="+"user"+">"
+"<input type=submit value=send>"+
"</form>";
response3 +="</body>";
response3 +="</html>";
clientSocket.Send(Encoding.UTF8.GetBytes(response3), response3.Length, SocketFlags.None);
//Get request
byte[] buffer = new byte[bytesReceived];
int byteCount = clientSocket.Receive(buffer);
//string request = new string(Encoding.UTF8.GetChars(buffer));
string request = Encoding.ASCII.GetString(buffer,0,byteCount);
int startIndex = request.IndexOf("user=");
int endIndex = request.IndexOf("HTTP/1.1");
string dati = request.Substring(startIndex,endIndex);
if (dati.IndexOf("archimede") != -1 )
{
Debug.Print("text contains your name");
}
else
{
if (dati.IndexOf("read") != -1 )
Debug.Print("user wants to read");
}
else
{
Debug.Print("text doesn't contain your name ");
Debug.Print(request);
Debug.Print(dati);
}
}
}
}
}
}
#region IDisposable Members
~Server()
{
Dispose();
}
public void Dispose()
{
if (socket != null)
socket.Close();
}
#endregion
}