I've already created an ad-hoc network connection between my Win-7 Laptop and Samsung smart-phone (Bada OS 2.0) through wifi. I've also checked the connectivity by veryifying the ip-addresses and pinging my smart-phone from the Laptop.
Now, I want to write a proxy-server that sits on my Win-7, accepts HTTP requests from smart-phone on port 8080, retrieves the webpage from internet, and passes it back to the smart-phone. The idea is twofold: to access internet on my smart-phone, and learn network programming with respect to mobile devices. I've written the following c-sharp code which implements the TCPListener to receive requests and send back the responses, which is happenning perfectly fine:
private void listen()
{
try
{
listener.Start();
while (true)
{
Socket client= listener.AcceptSocket();
ProcessClient(client);
}
}
catch (ThreadAbortException ex)
{
WriteLog("Error occured: " + ex.Message);
}
}
private void ProcessClient(Socket client)
{
//extract client data
byte[] bytes=new byte[1024];
int size = client.Receive(bytes, bytes.Length, SocketFlags.None);
string clientmessage = System.Text.Encoding.ASCII.GetString(bytes);
if (size == 0)
return;
int index1 = clientmessage.IndexOf(@" ");
int index2 = clientmessage.IndexOf(@" ", index1 + 1);
if (index1 == -1 || index2 == -1)
{
throw( new Exception());
}
//extract client url
string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
int index3 = part1.IndexOf(@"/", index1 + 8);
int index4 = part1.IndexOf(@" ", index1 + 8);
int index5 = index4 - index3;
string sURL= part1.Substring(index1 + 4, part1.Length - index5 - 8);
//send request to web server
IPHostEntry serverHost = Dns.GetHostEntry(sURL);
string[] aliases = serverHost.Aliases;
IPAddress[] addresses = serverHost.AddressList;
IPEndPoint serverEP = new IPEndPoint(addresses[0], 80);
Socket server = new Socket((addresses[0].AddressFamily), SocketType.Stream, ProtocolType.Tcp);
server.Connect(serverEP);
//server.Send(bytes, bytes.Length, SocketFlags.None);
byte[] sendbytes = new byte[1024];
size = System.Text.Encoding.ASCII.GetBytes(clientmessage, 0, clientmessage.Length, sendbytes, 0);
server.Send(sendbytes, sendbytes.Length, SocketFlags.None);
//server.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(clientmessage), System.Text.ASCIIEncoding.ASCII.GetByteCount(clientmessage), SocketFlags.None);
//read response from web server
byte[] rbytes = new byte[4096];
string strPage = "";
size = 1;
while (size > 0)
{
size = server.Receive(rbytes, rbytes.Length, SocketFlags.None);
strPage += System.Text.Encoding.ASCII.GetString(rbytes,0,size);
}
server.Shutdown(SocketShutdown.Both);
server.Close();
//send the page back to the client
//bytes = System.Text.Encoding.ASCII.GetBytes(strPage);
bytes = new byte[strPage.Length+1];
size = System.Text.Encoding.ASCII.GetBytes(strPage,0,strPage.Length,bytes,0);
client.Send(bytes, size, SocketFlags.None);
}
The above code fetches the request from the client (smart-phone browser), fetches the request headers part and passes it to the web-server, and relays the response back to the smart-phone through the same client socket. This is happening alright. What goes wrong is that the web-server is sending a response of "Bad Request - Status code 400". I'm surprised this is happening as all I'm doing is relaying the requests back and forth between two end-points. Could you share some light on this? Here are the request headers that are sent and response headers received that I got by fetching the variables clientmessage and strPage respectively:
Request:
GET http://prahlad-tosh/welcome.png HTTP/1.1
Proxy-Connection: Keep-Alive
Host: prahlad-tosh
Accept: text/html, application/xml, image/vnd.wap.wbmp, image/png, image/jpeg, image/gif, image/bmp, application/vnd.wap.xhtml+xml, application/xhtml+xml, application/vnd.wap.multipart.mixed, multipart/mixed, application/vnd.oma.dd+xml, text/vnd.sun.j2me.app-descriptor, application/java-archive, application/vnd.youtube, */*
Accept-Language: en
X-Wap-Proxy-Cookie: none
Accept-Charset: utf-8;q=1.0,iso-8859-1;q=0.6
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S5380K/S5380KDDKK6; U; Bada/2.0; en-us) AppleWebKit/534.20 (KHTML, like Gecko) Dolfin/3.0 Mobile HVGA SMM-MMS/1.2.0 OPN-B
Referer: `http://prahlad-tosh/`
Response:
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Tue, 19 Jun 2012 14:10:14 GMT
Connection: close
Content-Length: 326
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/htm/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>
EDIT: I think the problem can possibly be the server. How about using Apache or connecting to google.com instead?
Side-note: I don't think you need the part http://prahlad-tosh
in the GET request, just /welcome.png
is fine.