首先,如果项目有效,我的项目应该做什么:
注意:客户端/服务器端的接收/发送工作,我之前通过向服务器发送1来测试它,如果收到的字符串是1,则服务器返回0
现在我的编码: 这是用C#编写的服务器部分:
private static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static List<Socket> clientSockets = new List<Socket>();
private const int PORT = 11111;
private static byte[] buffer = new byte[100];
private void ReceiveCallback(IAsyncResult AR)
{
Socket current = (Socket)AR.AsyncState;
int received;
try
{
received = current.EndReceive(AR);
}
catch (SocketException)
{
Console.WriteLine("Client Disconnected! (Forced)");
current.Close();
clientSockets.Remove(current);
return;
}
//That's how my Example before worked:
/*byte[] recBuf = new byte[1];
Array.Copy(buffer, recBuf, received);
string text = Encoding.ASCII.GetString(recBuf);
if(text != "" && text.StartsWith("0"))
{
Console.WriteLine("Recieved: " + text);
string send = "1";
Console.WriteLine("Sending: " + send);
Byte[] data = ASCIIEncoding.ASCII.GetBytes(send);
current.Send(data);
Console.WriteLine("Sended!");
current.Shutdown(SocketShutdown.Both);
current.Close();
clientSockets.Remove(current);
Console.WriteLine("Client Disconnected!");
return;
}
current.BeginReceive(buffer, 0, 1, SocketFlags.None, ReceiveCallback, current);*/
//worked like a charm!
//That's my new Solution, that doesn't work:
byte[] recBuf = new byte[100];
Array.Copy(buffer, recBuf, received);
string text = Encoding.ASCII.GetString(recBuf);
while (text != "")
{
Console.WriteLine("Received Data: " + text);
if (text.StartsWith("0") && text.Length < 32)
{
Console.WriteLine("Client requests Random HexString...");
string rand = GetRandomHexNumber(32);
Byte[] data = ASCIIEncoding.ASCII.GetBytes(rand);
current.Send(data);
Console.WriteLine("Sended HexString! - " + rand);
}
if (text.StartsWith("1") && text.Length < 32)
{
Console.WriteLine("Client recieved the HexString!");
}
if (text.Length > 32 && !text.StartsWith("0") && !text.StartsWith("1"))
{
Console.WriteLine("Result recieved!");
//here will be some authing process but for simplicity just return "1"
byte[] data = ASCIIEncoding.ASCII.GetBytes("1");
current.Send(data);
current.Shutdown(SocketShutdown.Both);
current.Close();
clientSockets.Remove(current);
Console.WriteLine("Client Disconnected!");
return;
}
if (current.Connected)
current.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, current);
}
if (current.Connected)
current.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, current);
}
private void AcceptCallback(IAsyncResult AR)
{
Socket socket;
try
{
socket = serverSocket.EndAccept(AR);
}
catch (ObjectDisposedException)
{
return;
}
clientSockets.Add(socket);
socket.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, socket);
Console.WriteLine("Client Connected!");
serverSocket.BeginAccept(AcceptCallback, null);
}
private void setupServer()
{
serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT));
serverSocket.Listen(0);
Console.WriteLine("Server Started!");
Console.WriteLine("Listening for Clients...");
serverSocket.BeginAccept(AcceptCallback, null);
}
private void CloseAllSockets()
{
foreach (Socket socket in clientSockets)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
serverSocket.Close();
}
setupServer();在控制台启动时启动! C#部分是c ++方面唯一有问题的一切都很好! 我真的希望有人能够现在帮助我,因为我还没有在这个论坛上获得任何帮助,我真的需要这个工作,我相信这里有很多程序员可以帮助我(也许它只是一个逻辑问题,所以每个答案或评论都表示赞赏) 谢谢你的时间:))