# network protocol programming



## PC person (Feb 2, 2006)

I'm just wondering what kind of programming or programming language, (not sure what exactly I'm looking for) is used for networking? For example, when you send an email or open a web page what do the commands that routes your packets and tells it to use a protocol look like? 

I wonder if I can get a code snippet. I'm trying to think of an example. What, for example is the simplest thing you could do with the TCP/IP protocol. I would imagine sending email and pulling up a web page, though simple and easy to do in an application would probably utilize a lot of code.


----------



## MattBro (Nov 11, 2006)

Typically you may use any language for network programming, the bigger question is what underlying interface (example: Winsock) you intend to use.

To retrieve a webpage, you could do something like:


```
#define REQUEST  "GET\nHTTP1.1\nindex.html" // Note: can't remember the exact HTTP request format.

SOCKADDR_IN HostAddr;
SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

HostAddr.sin_family = AF_INET; // IP family.
HostAddr.sin_port   = htons(80); // Default port 80 for webservers (big-endian order)
HostAddr.sin_addr.s_addr = inet_addr("192.168.0.2"); // Webserver address.

// Establish TCP connection with the server.
connect(hSocket, (sockaddr*) &HostAddr, sizeof(HostAddr));

// request index.html
send(hSocket, REQUEST, sizeof(REQUEST), NULL);

// retrieve response
Byte Response[4096];

recv(hSocket, (char*) Response, 4096, NULL);

// By this time your response will be buffered in "Response".
```


----------

