Socket Programming

9/18/00
- BSD Unix (Berkeley Standard Distribution) was the first Unix to adopt TCP/IP

- BSD Unix integrated a API (Application Programming Interface) called
  "sockets" to be able to use TCP/IP in applications.

- FTP, Mail Program, finger etc. use sockets.

- In those days TCP/IP was not yet the "defacto standard" for network communication.
  Other alternatives: DECNET, SNA (ibm)

- Sockets API was designed to embrace all the different protocols.
  The network addresses and types in sockets can be variable.

- The generality of sockets makes it difficult to use.


Lab 3

You can use the client program in your textbook as a templete.

- Write a webdump program HTTP client that requests files to an HTTP server

- Use HTTP 1.0

- You can use telnet to connect to an HTTP server


 

winsock - windows sockets
       windows  makes a difference between file descriptors and socket descriptors
 
                    UNIX  read   <------>  receive   windows
write   <------>  send

       HTTP Client

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/ipent.h>
#include <netdb.h>

// when linking use libraries
// -lnsl -lsocket
// get a socket that is connected to an HTTP Server
int openHTTP(char *host, int port, char *request) {
    // IP address + port of Server
    Struct sockaddr_in sad;
    // clear this structure
    memset((char *)&sad, 0, sizeof(sad));
    // set family of internet protocols
    sad.sin_family = AF_INET;
    // set port number
    // need toa be in net byte order
    sad.sin_port = htons(port);

    // Get destination (server) host IP addr
    struct hostent * ptrh;
    ptrh = gethostbyname(host);
    if (ptrh == NULL) {
        // invalid host
        return -1;
    }
         

    // create socket
    if (sd == -1) {
        return -1;
    }
    if (connect(sd, (struct sockaddr *)&sad,
                sizeof(sad)) < 0) {
        return -1;
    }
    // socket is connected to sever
    // send GET request ....
            :
            :
 


9/20/00
int openHTTP(char *host, int port, char *request) {
        :
        :
    sd = socket
        :
    connect
        :
    // The client is connected to the server
    // sd can be used to write()
    // to the client and read()
    // An HTTP server expects a request of the form


    you can represent abitary ascii codes in a string
    by using "\0<octal representation of ascii>"

    // send GET command

    int n = write(sd, s, strlen(s));
    if (n != strlen(s)) return -1;
    n = write (sd, request, strlen(request));
    if (n != strlen(request)) return -1;

    n = write(sd, s, strlen(s));
    if (n != strlen(s)) return -1;
    return sd;
}

// main webdump
int
main(int argc, char **argv) {
    // from argc, argv
    // parse the URL passed as argument
    int ret = parseURL(argv, &host, &port, &request);
                           URL:
    if (ret != 0) {        http://<host>[:<port>][/request]
        exit(1);           if no request
    }                         -> request = "/"
                            if no port
                              -> port = 80
    const int  bufsize 1024;
    // connect to server
    int fd = openHTTP(host, port, request)
    char buf[bufsize];
    int n;
    if (fd < 0) {
        fprint(stderr, "Could not connect to server\n");
        perror("openHTTP");
        exit(-1);
    }
    // Read answer from server
    while ((n = read(fd, buf, bufsize)) > 0) {
        write(1, buf, n);
    }
    close(fd);
}

Uses
    webdump http://www.cs.purdue.edu > t.html

    HTTP Server Response


     Ethernet

CSMA/CD
    - Carrier Sense
    - Multiple access
    - Collision Detection

- Collision Detection
    + Simultaneous transmissions may interfere with each other. This is called a collision
    + The computers will wait until the medium is idle before transmitting
    + When they transmit they will also listen to the medium
    + If a computer detects that other signal interferes, it will stop transmission.
       and will wait for sometime before transmitting again.

- This is called backoff after collision:
    + During backoff, computers wait a random time t1
                        0 <= t1 <= d
    + Try transmission again using carrier sense.
    + If second collision occurs, wait a random time t2
                        0 <= t2 <= 2d
    + Double delay "d" (range) after each successive collision
    + This algorithm is called "exponential backoff".


9/22/00

Midterm

Friday Oct. 13th. during class

Ethernet
- It uses exponential backoff to recover from collision

On a wireless Network it is not possible to use collision detection
    - In a wireless network, the transmission range of a station is limited.
        - Not all the stations will receive the transmission:
            Assume that transmission range is "d" (distance)

            If comp "a" sends a message "b" will receive it but not "c"
    - Collision detection cannot be used because not all the computers
       will be able to detect a collision.

    - Wireless networks use CSMA/CA
        Carrier Sense Multiple Access Collision Avoidance
        - Assume x is going to send to y
            - x sends (broadcast) a short message telling everybody (in x's range)
              that is going to transmit to y.
            - y sends (broadcast) a short message telling everybody (in y's range)
              that y is expecting a message form x.
            - x sends data to y

            - This technique is called Collision Avoidance
            - Both messages (x -> y, all and y ->x, all) are necessary to reserve
               the shared media (air) around the stations in x and y's range

    - The responsibility of ethernet interface and wireless network interface
       is to try to deliver the packet in the best possible way.

    - It is possible that some packets are corrupted because of
        - Electrical interference
        - In collision avoidance, a collision may still happen and the packet received
          will be corrupted.
        - Other reasons.

    - It is the responsibility of higher-level protocols such as TCP to deal with
        - lost packets.
        - duplicated packets.

    - TCP deals with this problem using acknowledgments, retransmission of packets
       if acknowledgments are not received.

    Protocol Stack
      
    IP is unreliable (best-delivery-effort)
    TCP is reliable



        Identyfying a destination

- In ethernet all stations receive packet.
- The packet itself has a destination address.
- The ethernet address is 6 bytes long.
- Each interface in the world has a unique number.
- IEEE assigns ranges of addresses to companies.
- Interface hardware checks for the destination of packet and not the software.
  This prevents CPU overhead.