CS3212 計算機網路概論 Winsock Tutorial

Slides:



Advertisements
Similar presentations
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Advertisements

COEN 445 Communication Networks and Protocols Lab 4
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
VPN Lab 2 Zutao Zhu 04/02/2010. Outline How a packet traverse in VPN How to write socket program Certificate.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
ISP – 9 th Recitation Socket programming – Client side.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
1 Socket Programming A crash-course in network programming…
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
ECE 4110 – Internetwork Programming Client-Server Model.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Assignment 3 A Client/Server Application: Chatroom.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Chapter 1: Introduction to Web Applications. This chapter gives an overview of the Internet, and where the World Wide Web fits in. It then outlines the.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Discussion 2 Sockets Programming Applets TCP UDP HTTP Delay Estimation
Application Layer 2-1 ESERCITAZIONE SOCKET PROGRAMMING.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011.
The Inter-network is a big network of networks.. The five-layer networking model for the internet.
Network Programming By J. H. Wang Nov. 6, Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises.
CS 241 Section (04/19/12). MP8  Web Server  Due: Tuesday, May 1 st, 11:59pm  What will you be doing?  Creating a web-server in C that serves HTML.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Application Block Diagram III. SOFTWARE PLATFORM Figure above shows a network protocol stack for a computer that connects to an Ethernet network and.
Remote Shell CS230 Project #4 Assigned : Due date :
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
CS3212 計算機網路概論 Winsock Tutorial. Dev C++ environment setting Download Dev-C++ from
Network Programming By J. H. Wang Nov. 21, Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Vehicle Networking Networks Instruction 1 – Echo client/server in C Jeroen Voeten ES, 2012.
Assignment 3 A Client/Server Application: Chatroom
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Tutorial on Socket Programming
Things that are nice to know when you’re doing this project
27.
Chapter 2 Application Layer
Tutorial on Socket Programming
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Q/ Compare between HTTP & HTTPS? HTTP HTTPS
Presentation transcript:

CS3212 計算機網路概論 Winsock Tutorial

Dev C++ environment setting Download Dev-C++ from http://www.bloodshed.net/devcpp.html Scheme 1 Scheme 2

Scheme 1: No project Tools  Compiler Options  Add “-lws2_32” to the linker command line File  New  Source File #include <winsock2.h>

Scheme 2: Use project File  New  Project Choose “Console Application” Project  Project Options  Parameters Add Library or Object Choose C:\Dev-Cpp\lib\libws2_32.a #include <winsock2.h>

Socket programming TCP flow chart UDP flow chart Data structure of address Functions

Data structure of address The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian).

Domain name to IP address Use domain name instead of IP as the address struct sockaddr_in serverAddress struct hostent *h = gethostbyname(hostName) memcpy(&serverAddress.sin_addr, h->h_addr_list[0], h->h_length)

Functions WSAStartup(MAKEWORD(2,2),(LPWSADATA)&wsadata) Load winsock of version 2.2 WSACleanUp() Release winsock socket(AF_INET, SOCK_STREAM, 0) Create a TCP socket (SOCK_STREAM) or UDP socket (SOCK_DGRAM) bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress) Assign serverSocket serverAddress listen(serverSocket, 3) Prepare for incoming connections (maximum 3 connections)

accept(serverSocket, (struct sockaddr accept(serverSocket, (struct sockaddr *)&clientAddress, &clientAddressLen) Create a socket for the incoming connection, and the address of the target host is stored in clientAddress closesocket(serverSocket) Close serverSocket send(serverSocket, buf, len, 0) Send buf of size len (TCP socket) recv(serverSocket, buf, MAX_SIZE, 0) Receive data of maximum size MAX_SIZE, and store the data in buf (TCP socket) sendto(serverSocket, buf, len, 0, (struct sockaddr *)&clientAddress, &clientAddressLen) Send buf of size len to client (UDP socket) recvfrom(socket, buf, MAX_SIZE, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) Receive data of maximum size MAX_SIZE, and store the data in buf (UDP socket)

Examples TCP echo server/client UDP echo server/client

CS3212 計算機網路概論 Lab 1

Description Write a program (using Dev-C++) which can count the number of hyperlinks of a given web page. The main objective is to practice socket programming and try to use HTTP protocol.

Requirements Input: The users enter the URL of the desired web page without “http://”. Output: Print all hyperlinks and the number of hyperlinks. Note that only <a href=”xxx”> should be counted while <link href=”xxx”> shouldn’t.

Example 9 hyperlinks in http://can.cs.nthu.edu.tw/index.html

Hint Refer to the “winsock tutorial” document for information about Dev-C++ and socket programming. Divide the URL into a host name and a path www.cs.nthu.edu.tw /index.html

Hint Get the source code of a web page Send a request to the server (GET xxx …\r\n\r\n) Receive responses from the server (you may need to receive more than one responses) Count the number of hyperlinks <a href="xxxx"> Use strtok() to tokenize the source code by space characters (" \t\n") If the string starts with "href" and the previous token ends with "a", then you find a hyperlink