Download using socket

  • Thread starter Thread starter Henrik
  • Start date Start date
H

Henrik

I want to download a file using socket programming (and
not httprequest) but the DNS keeps giving me errors "IP
is ok, but the attached data is not available"

I am doing like this and the address is ok. does any one
know what is wrong here?

IPHostEntry lipa =
Dns.Resolve("192.168.0.2/image.jpg");

IPEndPoint lep = new
IPEndPoint(lipa.AddressList[0], 80);
Socket s = new Socket
(lep.Address.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
try
{
s.Connect(lep);
}
catch (Exception e)
{
Console.WriteLine
("Exception Thrown: " + e.ToString());
}

/*
byte[] msg =
Encoding.ASCII.GetBytes("This is a test");

// Blocks until send
returns.
int i = s.Send(msg, 0,
msg.Length, SocketFlags.None);
*/

// Blocks until read
returns.
byte[] bytes = new byte
[1024];
byte[] inBuf = new byte
[HttpBuffer];
s.Receive(inBuf, 0,
s.Available, SocketFlags.None);

//Displays to the screen.
//Console.WriteLine
(Encoding.ASCII.GetString(bytes));
s.Shutdown
(SocketShutdown.Both);
s.Close();
 
Henrik said:
I want to download a file using socket programming (and
not httprequest) but the DNS keeps giving me errors "IP
is ok, but the attached data is not available"

I am doing like this and the address is ok. does any one
know what is wrong here?
[snip]

You can't do it like this... Socket is a connection to the process not to
the file. Once you establish your connection you need to use the protocol
the server is using - http, most likely, in this case.
 
Back
Top