server

volleyballbeginnerΔίκτυα και Επικοινωνίες

27 Οκτ 2013 (πριν από 3 χρόνια και 10 μήνες)

63 εμφανίσεις

UDP Client
-
Server

The Socket Class

The

Socket

class

provides

a

set

of

methods

and

properties

for

network

communications
.

The

Socket

class

allows

you

to

perform

both

synchronous

and

asynchronous

data

transfer

using

any

of

the

communication

protocols

listed

in

the

ProtocolType

enumeration
.



The

Socket

class

follows

the

.
NET

Framework

naming

pattern

for

asynchronous

methods
.

For

example,

the

synchronous

Receive

method

corresponds

to

the

asynchronous

BeginReceive

and

EndReceive

methods
.

One

of

the

simplest

types

of

machine
-
to
-
machine

communication

is

the

client
-
to
-
server

with

a

static

IP

address
.



In

the

typical

application,

the

server

accepts

a

connection

from

any

client
,

but

the

client

must

know

the

IP

address

of

the

server
.


The

UdpClient

class

provides

simple

methods

for

sending

and

receiving

connectionless

UDP

datagrams

in

blocking

synchronous

mode
.



Because

UDP

is

a

connectionless

transport

protocol,

you

do

not

need

to

establish

a

remote

host

connection

prior

to

sending

and

receiving

data
.

You

do,

however,

have

the

option

of

establishing

a

default

remote

host

in

one

of

the

following

two

ways
:



1.
Create an instance of the
UdpClient

class using the remote host name and
port number as parameters.


2.
Create an instance of the
UdpClient

class and then call the
Connect

method.

The UdpClient Class

The

Connect

method

establishes

a

default

remote

host

using

the

values

specified

in

the

port

and

hostname

parameters
.

Once

established,

you

do

not

have

to

specify

a

remote

host

in

each

call

to

the

Send

method
.


Establishing

a

default

remote

host

is

optional
.

Specifying

a

default

remote

host

limits

you

to

that

host

only
.

If

you

want

to

send

datagrams

to

a

different

remote

host,

you

must

make

another

call

to

the

Connect

method

or

create

another

UdpClient

without

a

default

remote

host
.

UdpClient.Connect Method

The

IPEndPoint

class

contains

the

host

and

local

or

remote

port

information

needed

by

an

application

to

connect

to

a

service

on

a

host
.


By

combining

the

host's

IP

address

and

port

number

of

a

service
,

the

IPEndPoint

class

forms

a

connection

point

to

a

service
.


IPAddress
.
Any

provides

an

IP

address

that

indicates

that

the

server

must

listen

for

client

activity

on

all

network

interfaces
.

This

field

is

read
-
only
.

The

Any

field

is

equivalent

to

0
.
0
.
0
.
0

in

dotted
-
quad

notation
.

IPEndPoint

Communicating with a Byte Array

ProtocolType enumeration

Ggp

Gateway To Gateway Protocol

Icmp

Internet Control Message Protocol

Idp

Internet Datagram Protocol

Igmp

Internet Group Management Protocol

IP

Internet Protocol

IPv6

Internet Protocol version 6 (IPv6)

Ipx

Internet Packet Exchange Protocol

ND

Net Disk Protocol (unofficial)

Pup

PARC Universal Packet Protocol

Raw

Raw IP packet protocol

Spx

Sequenced Packet Exchange protocol

SpxII

Sequenced Packet Exchange version 2 protocol

Tcp

Transmission Control Protocol

Udp

User Datagram Protocol

Unknown

Unknown protocol

Unspecified

Unspecified protocol

Protocol types supported by the .NET Framework

using

System;

using

System.Text;

using

System.Net;

using

System.Net.Sockets;


namespace

UDPClient

{


class

Class1


{


static

void

Main(
string
[] args)


{


string

sendStr =
""
;


UdpClient

theClient =
new

UdpClient
(
"
IP here
"
, 9050);


while

(!sendStr.Trim().ToUpper().Equals(
"END"
))


{


Console
.Write(
"# "
);


sendStr =
Console
.ReadLine();


byte
[] myData =
new

byte
[1024];


myData =
Encoding
.ASCII.GetBytes(sendStr);


theClient.Send(myData, myData.Length);


}


theClient.Close();


}


}

}

UDP Client

using

System;

using

System.Text;

using

System.Net;

using

System.Net.Sockets;


namespace

UDPServer

{


class

Class1


{


static

void

Main(
string
[] args)


{


string

rcvData =
""
;


IPEndPoint

IPEP =
new

IPEndPoint
(
IPAddress
.Any, 9050);


UdpClient

theSock =
new

UdpClient
(IPEP);


IPEndPoint

fromClient;


while

(!rcvData.Trim().ToUpper().Equals(
"END"
))


{


byte
[] myData =
new

byte
[1024];


fromClient =
new

IPEndPoint
(
IPAddress
.Any, 0);


myData = theSock.Receive(
ref

fromClient);


rcvData =
Encoding
.ASCII.GetString(myData);


Console
.WriteLine(fromClient.ToString() +
" "

+ rcvData);


}


theSock.Close();


}


}

}

UDP Server (Listener)

Peer
-
to
-
Peer Communications

using

System;

using

System.Threading;

using

System.Net;

using

System.Net.Sockets;

using

System.Text;


namespace

ClientServer_1

{


class

Chat1


{



private

static

Thread

clientThread;



private

static

Thread

serverThread;




static

void

Main(
string
[] args)



{




CreateThreads();



}




private

static

void

CreateThreads()



{




clientThread =
new

Thread
(
new

ThreadStart
(RunClientThread));




clientThread.Start();





serverThread =
new

Thread
(
new

ThreadStart
(RunServerThread));




serverThread.Start();



}



private

static

void

RunClientThread()



{




string

sendStr=
""
;




UdpClient

theClient=
new

UdpClient
(
"127.0.0.1"
,9050);





while

(!sendStr.Trim().ToUpper().Equals(
"END"
))




{





sendStr=
Console
.ReadLine();





byte
[] myData=
new

byte
[10];





myData=
Encoding
.ASCII.GetBytes(sendStr);





theClient.Send(myData,myData.Length);








}




theClient.Close();



}

Client Thread



private

static

void

RunServerThread()



{




string

rcvData=
""
;




IPEndPoint

IPEP=
new

IPEndPoint
(
IPAddress
.Any,9051);




UdpClient

theSock=
new

UdpClient
(IPEP);




IPEndPoint

fromClient;








while

(!rcvData.Trim().ToUpper().Equals(
"END"
))




{





byte
[] myData=
new

byte
[10];





fromClient=
new

IPEndPoint
(
IPAddress
.Any,0);





myData= theSock.Receive(
ref

fromClient);





rcvData=
Encoding
.ASCII.GetString(myData);





Console
.WriteLine(fromClient.ToString() +
" "

+ rcvData);




}




theSock.Close();



}


}

}


Server Thread

Summary

User

Datagram

Protocol

(UDP)

-

permits

a

simple

method

of

communication

between

two

computers

UDP

does

not

check

for

transmission

errors

Client

must

know

the

IP

address

of

the

intended

Server

Server

accepts

connection

from

any

Client

The

.
NET

Socket

provides

a

set

of

methods

for

network

communication

Multi
-
Threaded

Programming

can

be

used

for

Peer
-
to
-
Peer

Communication