我正在使用以下项目,以便在服务器和客户端套接字之间创建异步通信.当我运行这些项目时,我正在从客户端向服务器发送消息,因此我收到了消息:
数据:记录EOF,向客户端发送14个字节.
我想要实现的是使用套接字从服务器向客户端发送布尔变量.这样做是否可行,我想知道因为在代码中我有等待和监听的服务器以及发送数据的客户端,我可以这样做吗?一般来说,我想要的是向几个客户端发送一个布尔值.为什么我需要文件结束才能发送字符串?是否有必要将所有内容转换为字符串?
编辑:一般来说,我想要的是将一个变量从一台计算机发送到另外两台,以便在所有计算机上同时开始一个过程.事实上,要创建一个切换器,它可以同时在2-3台机器上发出信号.
我尝试将以下代码用于服务器:
class Program
{
const int PORT_NO = 2201;
const string SERVER_IP = "127.0.0.1";
static void Main(string[] args)
{
//---listen at the specified IP and port no.---
IPAddress localAdd = IPAddress.Parse(SERVER_IP);
TcpListener listener = new TcpListener(localAdd, PORT_NO);
Console.WriteLine("Listening...");
listener.Start();
//---incoming client connected---
TcpClient client = listener.AcceptTcpClient();
//---get the incoming data through a network stream---
NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
//---read incoming stream---
int bytesRead = …Run Code Online (Sandbox Code Playgroud) 我有服务器和客户端控制台应用程序,它们可以正常通信并发送一些字符串。这是代码...
服务器
public static void Main()
{
try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 1234);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; …Run Code Online (Sandbox Code Playgroud)