Recent Changes - Search:

Network Programming

This website demonstrates using wikis as teaching and learning tool.

The course instructor is happy to share the teaching materials here with those who find it readable.

Tutorial - Unix Network Programming - Socket System Calls

Overview : In this tutorial, you will install a Linux-like environment for Windows called Cygwin, and then use it to experiment network programming in C and Unix.


Activity 1: Install Cygwin

Cygwin is a Linux-like environment for Windows. It consists of two parts: a DLL which acts as a Linux API emulation layer providing substantial Linux API functionality, and a collection of tools which provide Linux look and feel.

Activity 2: Experiment network programming with Cygwin

Notes

How to compile a program
      $ gcc -o hello hello.c
How to run a program:
        $ ./hello
        Hello World!
(Unlike Windows, bash does not look for programs in . (the current directory) by default. Just tell bash where to find it, when you type it on the command line.)

Activity 3: Write a simple network program in C and Unix

  • Introduction
HTTP (Hyper Text Transfer Protocol) is the protocol used for the Web. It is a simple and text-based (i.e. use textual message to communicate) protocol. For example, when a HTTP client wants to retrieve a document named index.html from a Web server, it just needs to connect the server at TCP port 80 and send this textual command to the server:
GET /index.html HTTP/1.0
The server then replies by sending the document to the client. A popular HTTP client is the Web browser that you use almost everyday.
  • Programming task
Write a TCP client program that allows a user to connect to a HTTP server to download a file from the server. Your program should meet the following specification:
The HTTP server port is 80; the hostname of the server is specified by the user via the first command line argument (i.e. argv[1]); the HTML document to be downloaded is specified by the user via the second command line argument (i.e. argv[2]); and the downloaded file is just displayed on the screen (or saved in a local file if you like).
  • Hints: the following is a program skeleton for you
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MAXLINE 4096

int main(int argc, char **argv)
{
     int     sockfd, n;
     char    recvline[MAXLINE+1];
     struct  sockaddr_in servaddr;
     struct hostent *hp;  
     char **p;  
     struct in_addr in;  
     char *SERV_HOST_ADDR = NULL;
     char buff[MAXLINE+1];

     if (argc != 3)
        perror("usage: grabpage <IPaddress> <filename>");

     hp = gethostbyname(argv[1]);
     if (hp != NULL) {
         p = hp->h_addr_list;
         memcpy(&in.s_addr, *p, sizeof(in.s_addr));
         SERV_HOST_ADDR = inet_ntoa(in);
     }

     bzero(&servaddr, sizeof(servaddr));
     //
     //
     //

     //
     //

     //
     //

     /* send HTTP get command to the server */
     sprintf(buff, "GET /%s HTTP/1.0\r\n\n", argv[2]);

     if (fputs(buff, stdout) == EOF) perror("fputs error");
     write(sockfd, buff, strlen(buff), 0);

     while ( (n=read(sockfd, recvline, MAXLINE)) >0 ) {
        recvline[n] = 0;
        if (fputs(recvline, stdout) == EOF)
           perror("fputs error");
    }

    if (n<0) perror("read error");
    exit(0);
}

Submission

  • You need to demonstrate your work to Steven in a tutorial session. Steven may ask you some questions to test your knowledge.
Edit - History - Print - Recent Changes - Search
Page last modified on December 01, 2009, at 09:36 AM