Server client construction process

xiaoxiao2021-03-06  103

Server client construction process

I. Basic knowledge of data stream

1, network flow

method one:

Public NetworkStream (Socket Socket)

E.g:

NetWorkstream NewStream = New NetworkStream (MySock);

Method Two:

Public NetworkStream (Socket Socket, Bool Ownssocket)

E.g:

NetWorkStream NewStream = New NetworkStream (MySock, true);

Method 3:

Public NetworkStream (Socket Socket, FileAccess Access)

E.g:

NetWorkStream NewStream = New NetworkStream (MySock, System.IO.FileAccess.Read);

Method 4:

Public NetworkStream (Socket Socket, FileAccess Access, Bool Ownssocket)

E.g:

NetWorkstream NewStream = New NetworkStream (MySock, System.IO.FileAccess.Read, true);

Read method:

Public override int in (int Byte [] buffer, int offset, int size)

FLUSH method:

Public Override Void Flush ()

Write method:

Public override void write (int Byte [] buffer, int offset, int size)

2, text flow

Write text stream to the file:

E.g:

Streamwriter SW = NULL;

SW = new streamwriter ("E: //temp//aa.txt", false, system.text.encoding.utf8);

SW.WRITE ("Aaaaaaaaaaaaaaaa);

SW.CLOSE ();

Write text stream to the network:

E.g:

String str = "aaaaaaaaaaaaaaa";

Byte [] mybyte = system.text.encoding.utf8.getbytes (STR);

NetworkStream NetStream = New NetworkStream (MySock);

NetStream.write (Mybyte, 0, Mybyte.length);

Read text from the file:

E.g:

StreamReader SW = NULL;

SR = New StreamReader ("E: //TEMP//aa.txt", system.text.encoding.utf8);

String mystr = sr.readtoend ();

sr.close ();

Read text from the network:

E.g:

String str = "aaaaaaaaaaaaaaa";

Byte [] mybyte = system.text.encoding.utf8.getbytes (STR);

NetworkStream NetStream = New NetworkStream (MySock);

NetStream.read (mybyte, 0, mybyte.length);

3, file stream

Write network stream data into the file:

E.g:

FileStream FileStream = New FileStream (path, filemode.openorcreate, fileaccess.write); int = 0;

BYTE [] mybyte = new byte [8];

While ((readnumber = stream.read (mybyte, 0 ,8))> 0)

{

FileStream.write (Mybyte, 0, ReadNumber);

FileStream.flush ();

}

FileStream.close ();

Read the file and write to the network stream:

E.g:

FileStream FileStream = New FileStream (Path, Filemode.open, FileAccess.Read);

Int number;

BYTE [] mybyte = new byte [8];

NetworkStream NetStream = New NetworkStream (MySock);

While ((Number = filestream.read (mybyte, 0 ,8))! = 0)

{

NetStream.write (Mybyte, 0, 8);

NetStream.flush ();

mybyte = new byte [8];

}

FileStream.close ();

Second, thread basic knowledge

1, thread creation

Public Thread (ThreadStart Start)

E.g:

Thread thread = New Thread (New ThreadStart (ACCP));

Private Void ACCP ()

{

......

}

2, thread startup

Public void start ()

E.g:

Thread thread = New Thread (New ThreadStart (ACCP));

Thread.start ();

Private Void ACCP ()

{

......

}

3, thread pause and restart

Thread sleep:

Thread.Sleep (Timeout.infinite);

Thread hang:

Thread thread = New Thread (New ThreadStart (ACCP));

Thread.start ();

......

Thread.suspend ();

Thread restart:

Thread thread = New Thread (New ThreadStart (ACCP));

Thread.start ();

......

Thread.suspend ();

......

Thread.Resume ();

4, thread destruction

method one:

Public void abort ();

Method Two:

Public void Abort (Object StateInfo);

E.g:

Thread thread = New Thread (New ThreadStart (ACCP));

Thread.start ();

......

Thread.abort ();

Thread.join (10000);

Method 3:

Public void interrupt ()

E.g:

Thread thread = New Thread (New ThreadStart (ACCP));

Thread.start ();

......

Thread.join (10000);

Interrupt ();

Third, server construction process

1. Synchronous socket server development process

(1) Host resolution -> Host Binding -> Port Listening -> Data Receive -> Data Send

(2) Define host objects

(The following is a method of IpendPOINT class)

method one:

Public IpendPoint (iPaddress Address, INT Port)

E.g:

Ipaddress myip = ipaddress.parse ("IP address");

IpendPoint MyServer = New IpendPoint (MyIP, Myport);

Method Two:

Public IpendPoint (Long Address, INT Port)

E.g:

IpendPoint MyServer = New IpendPoint (12345, MyPort);

(3) Host analysis

(The following is a method of DNS class)

Public Static iphostentry resolve (String Hostname)

E.g:

IPHOSTENTRY MyHost = New iphostentry ();

MyHost = DNS.Resolve;

-------------------------------------------------- -------

MyHost.addressList.Length // Get the number of host information

-------------------------------------------------- -------

Get host information:

method one:

Public static iphostentry gethostbyname (String hostname)

Method Two:

Public Static iPhostentry GethostbyAddress (iPaddress Address)

Method 3:

Public Static iPhostentry GethostbyAddress (String Address)

(4) Port binding and monitoring

(The following is a Socket class method)

Port binding:

Public void bind (endpoint localep)

Port listening:

Public void listen (int Backlog)

Receive the client's connection request:

Public socket acception ()

E.g:

Ipaddress myip = ipaddress.parse ("127.0.0.1");

IpendPoint MyServer = New IpendPoint (MyIP, 2020);

Socket Sock = New Socket (AddressFamily.internetwork, Sockettype.Stream, ProtocolType.tcp);

Sock.bind (MyServer);

Sock.listen (50);

Socket news = Sock.Accept ();

(5) Send data

(Send data can use the SEND method of the Socket class or the Write method of the NetWorkstream class)

method one:

Public int send (byte [] buffer

Method Two:

Public int send (byte [], socketflags)

Method 3:

Public int desund (byte [], int, socketflags)

Method 4:

Public int send (Byte [], int, int, socketflags)

E.g:

(Connected to the above example)

Byte [] Bytee = New byte [64];

String send = "aaaaaaaaaaa";

Bytee = system.text.Encoding.biGendianunicode.getbytes (Send.Tochararray ());

Newsock.send (Bytee, Bytee.length, 0);

Method 5:

Public override void write (byte [] buffer, int offset, int size

E.g:

NetWorkStream Street; New NetworkStream (NewSock);

Byte [] Bytee = New byte [512];

String send = "aaaaaaaaaaa";

Bytee = system.text.Encoding.biGendianunicore.getbytes (send);

Street (bytee, 0, bytee.length);

(6) Receive data

(Receive data can be used with a Socket class Receive method or a NetWorkstream class Read method)

method one:

Public int received (Byte [] buffer

Method Two:

Public int respive (byte [], socketflags)

Method 3:

Public int received (Byte [], int, socketflags)

Method 4:

Public int respive (byte [], int, int, socketflags)

E.g:

Byte [] Bytee = New byte [512];

Newsock.receive (Bytee, Bytee.length, 0);

String recne = system.text.encoding.bigenDianunicoreode.getstring (bytee);

Method 5:

Public override void read (byte [] buffer, int offset, int size

E.g:

NetWorkStream Street; New NetworkStream (NewSock);

Byte [] Bytee = New byte [512];

Street (Bytee, 0, Bytee.Length);

String rece = system.text.Encoding.biGendianunicoreode.getbytes (bytee);

2, asynchronous socket server development process

(The binding of the asynchronous socket server host is the same as the port listening to the synchronization socket server)

(1) Receive the connection request of the client

Public IASYNCRESULT BeginAccept (asyncCallback Callback, Object State)

(2) Send data

Public IASYNCRESULT Beginsend (Byte [] Buffer, Int Offset, Int Size, SocketFlags Socketflags, AsyncCallback Callback, Object State

(3) Receive data

Public IasyncResult Beginreceive (Byte [] Buffer, Int Offset, Int Size, SocketFlags SocketFlags, AsyncCallback Callback, Object State

(The development process is more complicated and cumbersome, to be supplemented ...)

3, TCPListener server development process

(1) Port listening

(The following is a method of TCPListener class)

Port listening:

Public void start ()

Receive the client's connection request:

Public socket acceptsocket ()

(The transmitted data and reception data of the TCPListener server are the same as the above two types)

Fourth, client construction process

1. Synchronous socket client development process

(1) Establish connection with the server -> Data transmission and reception

(2) Establish connection with the server

(The following is a Socket class method)

Public Void Connect (EndPoint Remoteep) For example:

Ipaddress myip = ipaddress.parse (IP address);

IpendPoint MyServer = New IpendPoint (MyIP, 1234);

Socket Sock = New Socket (AddressFamily.internetwork, Sockettype.Stream, ProtocolType.tcp);

Sock.connect (MyServer);

2, asynchronous socket client development process

(1) Establish connection with the server

(The following is a Socket class method)

Public IASYNERSULT BeginConnect (Endpoint Remoteep, AsyncCallback Callback, Object State)

(Specific use reference asynchronous socket server development process)

3, TCPLISTENER client development process

(1) Establish a connection

(The following is a method of TCPCLIENT class)

method one:

Public TCPCLIENT (IpendPoint Localep)

E.g:

Ipaddress myip = ipaddress.parse (IP address);

IpendPoint MyServer = New IpendPoint (MyIP, 12345);

TcpClient myclient = new tcpclient (MyServer);

Method Two:

Public TcpClient (String Hostname.Int Port)

E.g:

TcpClient myclient = new tcpclient (www.263.net, "80");

Method 3:

Public void connectionP (endpoint remoteep)

E.g:

TcpClient myclient = new tcpclient ();

Ipaddress myip = ipaddress.parse (IP address);

IpendPoint MyServer = New IpendPoint (MyIP, 1234);

MyClient.connect (MyServer);

Method 3:

Public Void Connect (iPadDress Address, INT Port)

E.g:

Ipaddress myip = ipaddress.parse (IP address);

TcpClient myclient = new tcpclient ();

MyClient.connect (MyIP, 12345);

Method 4:

Public void connect (String Hostname, INT Port)

E.g:

TcpClient myclient = new tcpclient ();

MyClient.connect ("www.263.net", 12345);

(TCPListener client sends data and receipt data as the TCPListener server)

转载请注明原文地址:https://www.9cbs.com/read-126859.html

New Post(0)