01-Review C Programming
01-Review C Programming
NETWORK
PROGRAMMING
Assoc. Prof. Truong Dieu Linh
Data Communications & Computer Networks
SoICT, HUST
2
Course information
• IT4062E: Network programming
• Webpage of the course
• https://users.soict.hust.edu.vn/linhtd/courses/NetworkProg/
• Instructor email: [email protected]
• For making appointment or brief discussion.
• What we study in this course
• How to build network applications using socket programming
paradigm.
• Socket programming using C (in details)
• Socket programming in Java (introduction and self study)
• Reference:
• UNIX® Network Programming Volume 1, Third Edition: The Sockets
Networking API, W. Richard Stevens,Bill Fenner,Andrew M. Rudoff
• https://notes.shichao.io/unp/ch7/
3
Course contents
• Lecture contents
• Review of C programming language
• Review of related concept in Computer Networks
• Introduction to Socket API
• Basic TCP socket: server side, client side
• UDP socket
• Multi-process TCP server
• Application protocol
• Socket programming with Java.
• Exercises in class
• After each lecture
• Final project
• Development of network applications in groups
• 2-3 members/ group.
• Used for mid-term and final evaluations
4
REVIEW C
PROGRAMMING
Content
• Data type
• Condition and Loop statement
• Function
• Command line argument
• Pointer
• Structure
• Link listed
• I/O function
6
Data type
• Integer
• int, char, short, long
• Floating
• double, float
• Array
• Collection of A data type
• Declaration : int a[10];
7
Size of Type
size of char: 1 bytes
s -128~+127
size of short: 2 bytes 76 0
s
63 62 0
-1.7E-308 ~1.7E+308
8
Condition
• a == b
• b equals to a
• a != b
• b is different to a
• a>b
• b is smaller than a
• a >= b
• b isn’t greater than a
• a<b
• b is greater than a
• a <= b
• b isn’t smaller than a
10
if … else
if (condition){
statement1;
…
}
else{
statement2;
…
}
Example :
if (x == 1){
y = 3;
z = 2;
}
else{
y = 5;
z = 4;
}
11
switch
switch (condition)
{
case value1: statement1;…; break;
case value2: statement2;…; break;
…
default: statementn;…; break;
}
Example :
int monthday( int month ){
switch(month)
{
case 1: return 31;
case 2: return 28;
…
case 12: return 31;
}
}
12
for
for (condition1;condition2;condition3)
{
statements;
…
}
Example:
for (x = 0; x < 10; x = x +1)
{
printf(“%d\n”, x);
}
13
while
while(condition){
statement;
…
}
Example:
x = 0;
while( x < 10 ){
printf(“%d\n”,x);
x = x + 1;
}
14
Function
• A function is a group of statements that is executed when it is
called from some point of the program.
• Function format:
type function_name ( parameter1, parameter2, ...) {
statements }
• where:
• type is the type of the data returned by the function.
• function_name.
• parameters
• Statements: function's body.
16
Example of function
#include <stdio.h>
Data type of function
int squaresub(int a)
{ Return value statement
return a*a;
}
int main()
{
Use function
int b = 10;
printf("%d\n", squaresub(5));
return 0;
}
17
Pointer
Address
• Pointer variable
• "Variable" refers to
variable 108 i
• Value of the pointer
104 j
is the address of
100 ptr
the variable in the
memory
• int i = 10;
• int j = 20;
0 Variables and addresses
• int *ptr
19
Pointer (cont)
Address
• int i = 10;
• int j = 20;
• int *ptr = &i; 108 i 10
104 j 20
• printf(“i=%d\n”, &i)
100 ptr 108
• printf(“ptr=%d\n”, ptr)
• printf(“i=%d\n”, i)
• printf(“*ptr=%d\n”,*ptr)
• Ptr refers to the pointer variable 0
Variables and addresses
20
Pointer (cont)
int x=1, y=5;
int z[10];
int *p;
p=&x; /* p refers to x */
y=*p; /*y is assigned the value of x*/
*p = 0; /* x = 0 */
p=&z[2]; /* p refer to z[2] */
21
int main(){
int a = 5;
int b = 3;
swap (a,b);
printf(“a=%d\n”, a);
printf(“b=%d\n”,b);
return 0;
}
22
int main(){
int a = 5;
int b = 3; 94 x 108
swap (&a,&b);
printf(“a=%d\n”, a); 90 y 104
printf(“b=%d\n”,b);
return 0;
}
0
24
Structure
• Structure is a collection of variables under a single name.
Variables can be of any type: int, float, char etc.
• Declaring a Structure:
25
int a;
struct Customer John;
26
Example (Structure)
struct student{
int id;
int score;
};
int main()
{
int i;
struct student students[5];
for(i=0; i<5; i++){
students[i].id = i;
students[i].score = i;
}
for(i=0;i<5;i++){
printf("student id:%d, score:%d\n",
students[i].id, students[i].score);
}
}
29
Use ‘typedef’
typedef struct student{
int id;
int score;
} STUDENT;
STUDENT students[5];
30
Link list
• Store a pointer to the next structure in the structure
struct student {
int id;
int score;
struct student *next;
}
• Warning : allocate memory before use and release
memory after use
*top
32
(1)
cp = (char *)malloc(64);
sp = (struct student *)malloc(64);
(2)
cp = (char *)malloc(sizeof(ch));
sp = (struct student *)malloc(sizeof(struct student)*10);
→ struct student sp[10]
33
I/O function
All I/O calls
ultimately go to the App #2
kernel
App
#1 std. I/O
I/O library helps with
buffering, formatting, Library
interpreting (esp. text
strings & Kernel
conversions)
34
• <fstream.h>
• fread
• fwrite
36
Example
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
char buf[1024];
int c;
fp = fopen(argv[1],"r");
while((fgets(buf, sizeof(buf),fp)) != NULL){
fputs(buf,stdout);
}
fclose(fp);
exit(0);
}
37
Exercise
Notice: It is important to do this exercise properly. The program produced by this exercise will be used for subsequent
lectures.
We need a small Study Schedule Management program for students in the university. The program works should allow
students to:
- login (using student ID and password)
- Read schedule of one weekday by providing the weekday. For example: student provides “Thursday” and the program
return list of all courses and schedule of the courses of the day.
Internally, the program store the list of registered courses (by students) and their schedules in 3 text files. The structure of the file
is as following:
course_schedule.txt
119747 IT3080 Computer Network 523,526,22,25-31,33-40,TC-502;
119748 IT4560 Computer Literacy 221,224,22,25-31,33-40,TC-211;
119749 IT4590 Database 524,526,22,25-31,33-40,D6-101;
119750 IT4935 Database Lab 615,616,22,25-31,D6-303;
Ví dụ với môn Computer Network, buổi học diễn ra vào thứ 5, buổi chiều, bắt đầu từ tiết 3, kết thúc ở tiết 6, môn học kéo dài các
tuần 22, 25-31, 33-40, Địa điểm TC-502
student_registration.txt
20191121 119747
20191121 119750
20191121 119748
20203121 119748
20191121 119747
User-account.txt
20203121 passwd1
20191121 passwd2
38
Exercise
Required functionalities:
Internal:
- Represent courses by structures,
- Represent relationship student -registered classes by structures.
- Once the program starts, read study schedule from files and represent the
information under the form of a list of courses (structure), list of registration
(structure).
Human interface:
- Login
- Read schedule:
- Read week day from student
- Return schedule of the day to students as in the following:
========================================================
Code |Course | Week Day| AM/PM |Period |Week | Room
IT3080 |Computer Network | Thursday| Afternoon| 3-6 |22,25-31, 33-40 |TC-502
39
Exercise
- Read week schedule (when user enter “ALL”):
• Display the busy schedule in the following format for a given student.
• ===========================================
• |Monday | Tuesday |Wednesday |Thursday |Friday
• ----------------------------------------------------------------------------
• 1| | | | |
• 2| | | | |
• 3| | | | |
• 4| | | | |
• 5| | | | |
• 6| | | | |
• 7| TC-201 | | | |
• 8| TC-201| | | |
• 9| TC-201 | | |TC-502 |
• 10 | TC-201 | | |TC-502|
• 11 | | | |TC-502 |
• 12 | | | |TC-502 |