C#服务器和客户端
你好, 我一直在开发一个简单的服务器/客户端 但有一个问题,我无法在一个cmd(客户端/服务器)上依次键入2条消息,因此我使用send/receive命令创建了一个计时器,但当我启动它时,它会在同一秒内停止 这是服务器的代码 这是客户代码C#服务器和客户端,c#,.net,client,C#,.net,Client,你好, 我一直在开发一个简单的服务器/客户端 但有一个问题,我无法在一个cmd(客户端/服务器)上依次键入2条消息,因此我使用send/receive命令创建了一个计时器,但当我启动它时,它会在同一秒内停止 这是服务器的代码 这是客户代码 希望有人能帮助我,谢谢您已经创建了计时器,但从未启动它们,程序在启动后退出。代码中存在更多错误,将计划操作与事件驱动操作混合在一起。每1000毫秒尝试发送一次,但正在等待Console.ReadLine()您能更具体地回答您的问题吗?我们不会调试您的代码……听
希望有人能帮助我,谢谢您已经创建了计时器,但从未启动它们,程序在启动后退出。代码中存在更多错误,将计划操作与事件驱动操作混合在一起。每1000毫秒尝试发送一次,但正在等待Console.ReadLine()您能更具体地回答您的问题吗?我们不会调试您的代码……听起来您希望在没有设计协议的情况下获得应用程序级消息。TCP没有应用程序级消息的概念——因此,如果您需要它们,您必须对它们进行设计并对其进行编码。是否有一种方法可以使接收函数线程化,因为除非轮到客户端/服务器接收,否则它不会接收。除非轮到您接收,否则不要调用
receive
。这是协议将指定的内容之一——谁发送、谁接收、何时、如何识别完整的消息,等等。您忘记了设计协议,因此无法正确实现,因为您不知道应该实现什么。(你的receive
函数接收什么?消息?你没有这样的东西。)你需要指定哪一方发送以及何时发送。您需要指定什么是应用程序级消息,以及接收方如何知道何时收到了整个消息。应在字节级别指定消息。您还应该指定如何设置和关闭连接,何时关闭连接,等等。您应该指定足够详细的信息,以便如果两段代码符合规范,它们将能够成功地进行互操作。我已经制作了一个windows应用程序,并且工作正常,还缺少一件事,我如何指定这是一条消息或一个图像等…在Winforms应用程序中,您有一个循环,它绘制表单,处理消息循环。在控制台应用程序中,您必须对循环进行编程。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Timers;
namespace Server
{
class Program
{
//--------------------------------------------------------------------------------------------
static TcpClient connected;
//--------------------------------------------------------------------------------------------
static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Any, 1980);
listener.Start();
connected = listener.AcceptTcpClient();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(timerTick);
timer.Interval = 100;
Thread.Sleep(1000);
timer.Enabled = true;
}
//--------------------------------------------------------------------------------------------
static byte[] buffer = new byte[4096];
//--------------------------------------------------------------------------------------------
static void send()
{
NetworkStream stream = connected.GetStream();
byte[] data = Encoding.ASCII.GetBytes(Console.ReadLine());
stream.Write(data, 0, data.Length);
send();
}
//--------------------------------------------------------------------------------------------
public static void timerTick(object source, ElapsedEventArgs e)
{
receive();
}
//--------------------------------------------------------------------------------------------
static void sendCP()
{
NetworkStream stream = connected.GetStream();
byte[] data = Encoding.ASCII.GetBytes("Connected To Server");
stream.Write(data, 0, data.Length);
}
static void receive()
{
NetworkStream stream = connected.GetStream();
int data = stream.Read(buffer, 0, 4096);
string rec = Encoding.ASCII.GetString(buffer, 0, data);
Console.WriteLine("Client: " + Encoding.ASCII.GetString(buffer, 0, data));
for (; ; ) { }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Timers;
namespace Client
{
class Program
{
static TcpClient client;
static void Main(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(timerTick);
timer.Interval = 1000;
client = new TcpClient();
client.Connect("127.0.0.1", 1980);
if (client.Connected)
{
sendCP();
timer.Enabled = true;
}
}
public static void timerTick(object source,ElapsedEventArgs e)
{
while (client.Connected)
{
receive();
}
}
static byte[] buffer = new byte[4096];
static void send()
{
NetworkStream stream = client.GetStream();
byte[] data = Encoding.ASCII.GetBytes(Console.ReadLine());
stream.Write(data, 0, data.Length);
send();
}
static void sendCP()
{
NetworkStream stream = client.GetStream();
byte[] data = Encoding.ASCII.GetBytes("Connected To Client");
stream.Write(data, 0, data.Length);
}
static void receive()
{
NetworkStream stream = client.GetStream();
int data = stream.Read(buffer, 0, 4096);
string rec = Encoding.ASCII.GetString(buffer, 0, data);
Console.WriteLine("Server: " + Encoding.ASCII.GetString(buffer, 0, data));
}
}
}