1 Network Programming. 2 Background Important guidelines –Use conductor.tamucc.edu or any LINUX machine to develop your network applications –Do not use.

Slides:



Advertisements
Similar presentations
Socket Programming CS3320 Fall 2010.
Advertisements

Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C Intro.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Name and Address Conversions© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice.
#include DatatypeDescription int8_t uint8_t int16_t uint16_t int32_t uint32_t Signed 8-bit integer Unsigned 8-bit integer Signed 16-bit integer Unsigned.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Unix Network Programming Part 2: Elementary Sockets Jani Peusaari.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Netprog: DNS and name lookups1 Address Conversion Functions and The Domain Name System Refs: Chapter 9 RFC 1034 RFC 1035.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Elementary Name and Address Conversions
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Sirak Kaewjamnong Computer Network Systems
Socket 实验汇总 1. 2 Lab1( 需验收 ) Run all the sample programs today and answer the mentioned questions. Learn about the O_APPEND flag used in open(). Question:
Elementary Name and Address Conversions
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.
Network Programming.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Socket address structures Byte ordering IP address conversion
Sockets Introduction Socket address structures Value-result arguments
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Computer networks Name: K.SUDHA Designation: Lecturer Department: Electrical and Electronics Engineering Subject code: CS2361 Year: III Unit: V Title:
Name and Address Conversions
Name/Address conversion:
Socket programming Péter Verhás August 2002
Onward with Chat! Networking CS 3470, Section 1.
Building an Internet Router
Network Programming CSC- 341
Chapter 2 Overview of C.
Socket Programming (Cont.)
CS 105 “Tour of the Black Holes of Computing”
C Basics.
Network Programming with Sockets
Recitation 11 – 4/29/01 Outline Sockets Interface
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Introduction to CS Your First C Programs
Basic Internet Components
Network Programming Chapter 12
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
in unistd.h: int gethostname(char * name, int maxlen)
Presentation transcript:

1 Network Programming

2 Background Important guidelines –Use conductor.tamucc.edu or any LINUX machine to develop your network applications –Do not use penguin.tamucc.edu to develop or test your network applications –You may use any text editor to write your program. There is a GUI-based text editor available for LINUX environment. Check our lab machines. –Always save your program with extension.c (example: test.c). Use lowercase letters for file names. –Compile your program using gcc [GNU c ] compiler. Example: gcc -o test test.c [Press Enter]

3 Background (continued) Important guidelines (cont’d) –To run your program in foreground you may use: test [Press Enter] Or./test [Press Enter] –To run your program in background, you may use: test & [Press Enter] Or./test & [Press Enter] –To find the process id number of your program running in background, use ps [Press Enter] –To terminate your background program, use: kill process-id of your program [Press Enter]

4 Some C Basics for C++ Programmers For comments, use /* */. (gcc also accepts // for in-line comments) For screen output, use printf function. For keyboard input, use scanf function. C does not allow function arguments to have default values. C does not support arguments passed by reference. Use pointers, instead. Use malloc, calloc, realloc, and free for memory allocation and deallocation.

5 Example on printf function #include int main() { int age = 40; float dollar = 49.95; char response = ‘Y’; char message[10] = “Thanks”; printf(“This is a test program.\n”); /* print one line */ printf(“This\nis\na\ntest\nprogram.\n”; /* five lines */ printf(“My message: %s\n”, message); /* print string */ printf(“age: %d\n”, age); /* print integer */ printf(“Balance: $%.2f\n”, dollar); /* print float */ printf(“My Response: %c\n”, response); /*print character */ return 0; }

6 Example on scanf #include int main() { int age; float dollar; char response; char message[10]; scanf(“%s”, message); /* read string */ scanf(“%d”, &age); /* read integer */ scanf(“%f”, &dollar); /* read float */ scanf(“%c”, &response); /*read char */ return 0; }

7 Question: How can we find the DNS name of our machine using a C/C++ program? Solution: Use gethostname() function DNS Name of Local Machine

8 Function gethostname() Used to obtain the primary name of the computer on which it is invoked #include int gethostname(char *name, size_t len); Calling form: retvalue = gethostname (name, namelen); retvalue is 0 if successful name is a character string into which the name is placed if successful namelen is length of name

9 Example Program to Find the Name of Local Machine /* Example program to find the host name of the local machine */ #include #include /* for gethostname() */ int main(void) { char name[81]; const int namelen = 80; int returncode; returncode = gethostname(name, namelen); if (returncode == -1) printf("*** Error ***\n") ; else printf("The official name of the local host is: %s", name); return 0; }

10 Question: How can we find the IP address of a host given its DNS name? Answer:Use gethostbyname() function. DNS Name to IP Address Conversion DNS Name struct of IP Addresses, etc. gethostbyname()

11 Function gethostbyname() It finds IP address from some host name using DNS search #include struct hostent *gethostbyname(const char *name); Returns a pointer to a hostent structure if successful. Returns a NULL if unsuccessful

12 Structure of hostent The hostent structure is defined in as follows: struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */ } #define h_addr h_addr_list[0] /* for backward compatibility */

13 Illustration of hostent structure h_name h_aliases h_addrtype h_length h_addr_list [0] [1] [2] ….. NULL [0] [1] [2] ….. NULL String (official name) string 4Bytes (AF_INET) (4) (IP addrs) (Other names)

14 Example Program to Find IP Address #include int main(void) { struct hostent *hostPtr; char hostName[]="sci.tamucc.edu"; hostPtr = gethostbyname(hostName); if (hostPtr == NULL) printf( " Host %s not found \n ", hostName); else { /* Display IP address */ struct in_addr *addrPtr; addrPtr = (struct in_addr *) *hostPtr->h_addr_list; printf("%s\n",inet_ntoa(*addrPtr)); } return 0; }

15 Question: How can we convert a binary IP address (network byte order) in decimal dotted notation? Answer: Use inet_ntoa() function Binary IP Address to Dotted Decimal Notation Binary IP Address Dotted Decimal IP Address inet_ntoa()

16 Function inet_ntoa() Synopsis char *inet_ntoa(struct in_addr in); Converts Internet host address in given in network byte order to a string in decimal dotted notation.

17 Structure in_addr Structure in_addr is defined in netinet/in.h as: struct in_addr { unsigned long int s_addr; } s_addr ( long int) in_addr

18 Question: How can we find the DNS name of a host given its IP address? Answer: Use gethostbyaddr() function IP Address to DNS Name Conversion Binary IP Address struct with DNS Names, etc gethostbyaddr()

19 Function gethostbyaddr() SYNOPSIS #include #include /* for AF_INET */ struct hostent *gethostbyaddr(const char *addr, int len, int type); Returns a structure of type hostent for the given host address addr of length len and address type type. The only valid address type is currently AF_INET for LINUX.

20 Example to Find DNS Name #include int main(void) { struct hostent *hostPtr; char ip_addr_dot[]=" "; u_long ip_addr_long; char* addr_ptr; ip_addr_long = inet_addr(ip_addr_dot); addr_ptr = (char *) &ip_addr_long; hostPtr = gethostbyaddr(addr_ptr, 4, AF_INET); if (hostPtr == NULL) printf(" Host %s not found\n", ip_addr_dot); else { printf("The official name of the site is: %s\n", hostPtr->h_name); } return 0; }

21 Question: How can we convert an IP address in decimal dotted notation to binary (network byte order)? Answer: Use inet_addr() function Dotted Decimal Notation to Binary IP Address Dotted Decimal IP Address Binary IP Address inet_addr()

22 Function inet_addr() Synopsis #include unsigned long int inet_addr(const char *cp); Converts the Internet host address cp from numbers- and-dots notation into binary data in network byte order. If the input is invalid, INADDR_NONE (usually -1) is returned.

23 How to Display All DNS Names and IP Addresses of A Host Given Its One DNS Name

24 Example

25 How to Display All DNS Names and IP Addresses of A Host Given Its One IP Address

26 Example

27 Passing Arguments to main() Function

28 Question: How can we pass command line arguments to our program? Solution: Define the header of your main() function as: –void main(int argcount, char* argvector[]) –argcount is the number of arguments including the program name –argvector[0] holds the program name as a string –argvector[1] holds the first argument as a string –: –argvector[argcount - 1] holds the last argument as a string

29 Illustration of Passing Arguments to main() Function argcount (int) argvector[0] [1] [2]... [argcount -1] string (program name) string (argument 1) string (argument 2) string (last argument) void main(int argcount, char* argvector[])

30 Example on how to access command line arguments

31 Byte Ordering Memory locations are addressed byte by byte A data unit can span more than one byte. What is the order of bytes from most significant (MS) to least significant (LS) in memory? We call it host byte order. Example –On most computers, a long integer is four bytes. Assume a long integer in binary: Case 1. Big endian byte orderCase 2. Little endian byte order Memory A A+1 A+2 A+3 A A+1 A+2 A+3 (MS Byte)(LS Byte) (MS) (LS) (MS)

32 Network Byte Order TCP/IP supports only big endian byte order, we call it network byte order. For portability of your source code, always use conversion functions: –htons() : (Host to network short) Converts 16-bit integer from host byte order to network byte order –ntohs(): (Network to host short) Converts network 16-bit integer to host byte order – htonl() : (Host to network long) Converts 32-bit integer from host byte order to network byte order –ntohl(): (Network to host long) Converts network 32-bit integer to host byte order 16-bit host 16-bit net ntohs()htons() 32-bit host 32-bit net ntohl()htonl()

33 htonl, htons, ntohl, and ntohs Functions For LINUX System #include uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort);

34 Example #include // It checks whether host byte order is same as net byte order // in this machine int main() { long int num1 = 0x0a170e06; // in dotted decimal // Output in decimal dotted form without conversion struct in_addr* addr_ptr; // needed for call addr_ptr = (struct in_addr *) &num1; printf("%s\n", inet_ntoa(*addr_ptr)); // Output after conversion long int num2; num2 = htonl(num1); // Convert addr_ptr = (struct in_addr *) &num2; printf("%s\n", inet_ntoa(*addr_ptr)); return 0; } Output What is your conclusion?

35 Review Functions –gethostname() –gethostbyname() –gethostbyaddr() –inet_ntoa() –inet_addr() –htons() –ntoh() –htonl() –ntohs() Structures –hostent –in_addr