For example, the client??™s Receive and the server??™s Send
methods handle floating point numbers (the temperature samples) rather than arrays
of bytes, as the .NET socket API does. Although these classes are more specialized
than the .NET Socket class, they still support other applications and protocols
besides the ones described here (Exercise 1).
16 Why We Need Model-Based Testing
SERVER
Socket
Bind
Listen
Accept
Receive
Send
Close connection
Close listener
CLIENT
Socket
Connect
Send
Receive
Close
Figure 2.2. Socket API calls for client/server protocol.
Overview 17
using System;
using System.Net;
using System.Net.Sockets;
namespace ClientServerImpl
{
// Wrapper for .NET server socket
public class Server
{
Socket listenerSocket; // assigned by Socket
Socket connectionSocket; // assigned by Accept, used by Send etc.
const int BUFLEN = 40;
byte[] receiveBuf = new byte[BUFLEN];
// method Client.Socket assigns instance of System.Net.Sockets.Socket
public void Socket()
{
listenerSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}
public void Bind(string ipAddr, int port)
{
listenerSocket.Bind(new IPEndPoint(IPAddress.Parse(ipAddr), port));
}
public void Listen()
{
const int backlog = 0;
listenerSocket.
Pages:
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48