• 검색 결과가 없습니다.

Socket Programming Laboratory - 1

N/A
N/A
Protected

Academic year: 2021

Share "Socket Programming Laboratory - 1"

Copied!
35
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

Socket Programming Laboratory - 1

Getting Started with C#

(2)

 Name: Ahmed Galib Reza

 Email: [email protected]

 Lab: Ubiquitous Computing Lab

 Nationality: Bangladeshi

 Education:

◦ B. IT (Hons.) Information System Engineering , Multimedia University, Malaysia.

◦ Master of Computer Engineering,

Major in Ubiquitous IT, Dongseo University, South Korea.

Self Introduction

(3)

 Introduction

 Understand the basic structure of a C# program.

 Obtain a basic familiarization of what a

"Namespace" is.

Obtain a basic understanding of what a Class is.

Learn what a Main method does.

 Learn about console input/output (I/O)

 Variables and C# Data types

 C# Operators

Objectives

(4)

 C# (C Sharp), a successor to C++, has been

released in conjunction with the .NET framework.

 C# design goals:

◦ Be comfortable for C++ programmer

◦ Fit cleanly into the .NET Common Language Runtime (CLR)

◦ Simplify the C++ model

◦ Provide the right amount of flexibility

◦ Support component-centric development

About C#

(5)

Create Project

(6)
(7)

using System;

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

Console.WriteLine("Hello World!!");

} }

}

Hello World in C#

•Uses the namespace System

•Entry point must be called Main

•Output goes to the console

•File name and class name need not to be identical

•C# is case sensitive,

therefore args and Args are different identifiers

•Uses the namespace System

•Entry point must be called Main

•Output goes to the console

•File name and class name need not to be identical

•C# is case sensitive,

therefore args and Args are different identifiers

Namespace Declaration is Optional

Namespace Declaration is Optional

(8)

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

System.Console.WriteLine("Hello World!!");

} }

}

Hello World without Namespace

must use qualified name

must use qualified

name

(9)

 Replace Console.WriteLine() statement of HelloWorld Program by Console.Write()

statement.

 What is your output?

Try yourself

(10)

 Console.Write()

◦ Write the specific string to the standard output stream.

 Console.WriteLine()

◦ Write the specific string, follow by current string terminator to the standard output stream.

Write() vs. WriteLine()

(11)

Exercise - 01

using System;

class Exercise {

static void Main() {

Console.WriteLine("One!!!");

Console.WriteLine("Two!!!");

Console.WriteLine("Three!!!");

}

}

(12)

Exercise - 02

What is your Output? Any changes?

using System;

class Exercise {

static void Main() {

Console.Write ("One!!!");

Console.Write ("Two!!!");

Console.Write ("Three!!!");

}

}

(13)

 Often we use special identifiers called keywords that already have a predefined meaning in the language

◦ Example: class

 A keyword cannot be used in any other way

C# Keywords

(14)

C# Keywords

All C# keywords are Lowercase

(15)

Variables

A variable is a typed name for a location in memory

A variable must be declared, specifying the variable's name and the type of information that will be held in it

int numberOfStudents;

int average, max;

data type variable name

int total;

Which ones are valid variable names?

myBigVar VAR1 _test @test 99bottles namespace It’s-all-over

9200 9204 9208 9212 9216 9220 9224 9228 9232

numberOfStudents:

average:

max:

total:

(16)

Assignment

An assignment statement changes the value of a variable

The assignment operator is the = sign total = 55;

 You can only assign a value to a variable that is consistent with the variable's declared type (more later)

 You can declare and assign initial value to a variable at the same time, e.g.,

int total = 55;

 The value on the right is stored in the variable on the left

The value that was in total is overwritten

int total;

(17)

Example

static void Main(string[] args) {

int total;

total = 15;

System.Console.Write(“total = “);

System.Console.WriteLine(total);

total = 55 + 5;

System.Console.Write(“total = “);

System.Console.WriteLine(total);

}

(18)

Constants

 A constant is similar to a variable except that it holds one value for its entire existence

 The compiler will issue an error if you try to change a constant

 In C#, we use the constant modifier to declare a constant

constant int numberOfStudents = 42;

 Why constants?

◦ give names to otherwise unclear literal values

◦ facilitate changes to the code

◦ prevent inadvertent errors

(19)

C# Data Types

There are 15 data types in C#

Eight of them represent integers:

byte, sbyte, short, ushort, int, uint, long,ulong

Two of them represent floating point numbers

float, double

One of them represents decimals:

decimal

One of them represents boolean values:

bool

One of them represents characters:

char

One of them represents strings:

string

One of them represents objects:

object

(20)

Numeric Data Types

The difference between the various numeric types is their size, and therefore the values they can store:

Range 0 - 255 -128 - 127

-32,768 - 32767 0 - 65537

-2,147,483,648 – 2,147,483,647 0 – 4,294,967,295

-910

18

to 910

18

0 – 1.810

19

1.010

-28

; 7.910

28

with 28-29 significant digits

1.510

-45

; 3.410

38

with 7 significant digits

5.010

-324

; 1.710

308

with 15-16 significant digits Type

byte sbyte short ushort int

uint long ulong decimal float double

Storage

8 bits

8 bits

16 bits

16 bits

32 bits

32 bits

64 bits

64 bits

128 bits

32 bits

64 bits

(21)

Examples of Numeric Variables

int x = 1;

short y = 10;

float pi = 3.14f; // f denotes float float f3 = 7E-02f; // 0.07

double d1 = 7E-100;

// use m to denote a decimal

decimal microsoftStockPrice = 28.38m;

(22)

Boolean

 A bool value represents a true or false condition

 A boolean can also be used to represent any two states, such as a light bulb being on or off

 The reserved words true and false are the only valid values for a boolean type

bool doAgain = true;

(23)

Characters

 A char is a single character from the a character set

 Character literals are represented in a program by delimiting with single quotes, e.g.,

'a‘ 'X‘ '7' '$‘ ',‘

char response = ‘Y’;

(24)

Common Escape

Sequences

(25)

String

 A string represents a sequence of characters, e.g.,

string message = “Hello World”;

(26)

using System;

class Exercise {

static void Main() {

int CoordX;

int CoordY;

CoordX = 12;

CoordY = -8;

Console.Write("Cartesian Coordinate System: ");

Console.Write("P(");

Console.Write(CoordX);

Console.Write(", ");

Console.Write(CoordY);

Console.WriteLine(")\n");

} }

Example (Signed Integers)

(27)

using System;

class Exercise {

static void Main() {

uint DayOfBirth;

uint MonthOfBirth;

uint YearOfBirth;

DayOfBirth = 8;

MonthOfBirth = 11;

YearOfBirth = 1996;

Console.WriteLine("Red Oak High School");

Console.Write("Student Date of Birth: ");

Console.Write(MonthOfBirth);

Console.Write("/");

Console.Write(DayOfBirth);

Console.Write("/");

Console.Write(YearOfBirth);

Console.WriteLine();

} }

Example (Unsigned

Integers)

(28)

using System;

class NumericRepresentation {

static void Main() {

long CountryArea;

CountryArea = 5638648L;

Console.Write("Country Area: ");

Console.Write(CountryArea);

Console.Write("km2\n");

} }

Example (Long Integers)

(29)

using System;

class NumericRepresentation {

static void Main() {

float Distance = 248.38F;

//Distance = 248.38F;

Console.Write("Distance = ");

Console.Write(Distance);

Console.WriteLine("km\n");

} }

Exercise (Float)

(30)

using System;

class Exercise {

static void Main() {

string Team = "Real Madrid";

string Country = "Spain";

Console.WriteLine("Welcome to the World of C# Programming!");

Console.Write("Team: ");

Console.WriteLine(Team);

Console.Write("Country: ");

Console.WriteLine(Country);

Console.WriteLine();

} }

Exercise (String)

(31)

Type conversion

  Number Number

◦ Example :

 double int

 int float

 short int

 String/Number Number

◦ Example :

 string int

 double string

(32)

using System;

class Exercise {

static void Main() {

const double PI = 3.14;

int integer = (int)PI;

string str = Convert.ToString(PI);

double doubleValue = Convert.ToDouble(str);

Console.WriteLine("const PI is {0}", PI);

Console.WriteLine("integer is {0}", integer);

Console.WriteLine("str is {0}", str);

Console.WriteLine("doubleValue is {0}", doubleValue);

} }

Example

(33)

C# Operators

Category (by

precedence) Operator(s) Associativi

ty Primary x.y  f(x)  a[x]  x++  x-- new  typeof  default  checked 

unchecked delegate left

Unary +  -  !  ~  ++x  --x  (T)x left

Multiplicative *  /  % left

Additive +  - left

Shift <<  >> left

Relational <  >  <=  >=  is as left

Equality ==  != right

Logical AND & left

Logical XOR ^ left

Logical OR | left

Conditional AND && left

Conditional OR || left

Null Coalescing ?? left

Ternary ?: right

Assignment =  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=  => right

(34)

Binary

Operator

using System;

class Binary {

public static void Main() {

int x, y, result;

float floatresult;

x = 7;

y = 5;

result = x+y;

Console.WriteLine("x+y: {0}", result);

result = x-y;

Console.WriteLine("x-y: {0}", result);

result = x*y;

Console.WriteLine("x*y: {0}", result);

result = x/y;

Console.WriteLine("x/y: {0}", result);

floatresult = (float)x/(float)y;

Console.WriteLine("x/y: {0}", floatresult);

result = x%y;

Console.WriteLine("x%y: {0}", result);

result += x;

Console.WriteLine("result+=x: {0}", result);

} }

(35)

Unary Operators

using System;

class Unary {

public static void Main() {

int unary = 0;

int preIncrement;

int preDecrement;

int postIncrement;

int postDecrement;

int positive;

int negative;

bool logNot;

preIncrement = ++unary;

Console.WriteLine("pre-Increment: {0}", preIncrement);

preDecrement = --unary;

Console.WriteLine("pre-Decrement: {0}", preDecrement);

postDecrement = unary--;

Console.WriteLine("Post-Decrement: {0}", postDecrement);

postIncrement = unary++;

Console.WriteLine("Post-Increment: {0}", postIncrement);

Console.WriteLine("Final Value of Unary: {0}", unary);

positive = -postIncrement;

Console.WriteLine("Positive: {0}", positive);

negative = +postIncrement;

Console.WriteLine("Negative: {0}", negative);

logNot = false;

logNot = !logNot;

Console.WriteLine("Logical Not: {0}", logNot);

} }

참조

관련 문서

생각열기 신기한 과학 마술 알아보기 생각펼치기 오목렌즈와 볼록렌즈의 비밀.. 생각다지기 찰칵

hasSensorType String 차량 센서 Type을 표현. hasState String 클래스

indicated input stream fgets( char *s, int n, FILE *) Address of the string/NULL pointer. fputs() Writes the character string pointed to

indicated input stream fgets( char *s, int n, FILE *) Address of the string/NULL pointer. fputs() Writes the character string pointed to

 Assuming a cache hit, the new data is written from the store buffer into the cache on the next unused cache access cycle.  Many write-back caches also include write buffers

We define what rules are safe, because it is possible to write rules that can generate an infinite number of tuples in the view relation.. It is possible to write

Ø mass per unit length is constant; string is perfectly elastic and no resistance to bending.. Ø tension in string is sufficiently large so that gravitational forces may

• Calculate a throttling delay by using the predicted write demand and the remaining lifetime. Lifetime Issues &amp; Techniques