我在C#中编写了一个异步TCP服务器客户端。当我在一台计算机上测试它们时,两个程序都成功运行(服务器和客户端在同一台机器上同时运行)。但是,当在两台不同的计算机上进行测试时,建立了服务器 - 客户端连接,客户端能够将数据发送到服务器,但服务器似乎没有向其客户端发送任何内容。
通过使用TCPView,我知道服务器和客户端都按预期接收/发送数据。我以为是阻止它们的防火墙,我已禁用它并且没用。我的代码出了什么问题?
服务器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
// Server code
namespace ChatConsole
{
class Program
{
static Socket serverSocket, clientSocket;
static byte[] buffer;
const int defaultBufferSize = 1024;
static void Main(string[] args)
{
try
{
Console.WriteLine("This is server.");
// Create a new server socket.
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Create IP End Point.
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 1001);
// Bind endpoint to socket.
serverSocket.Bind(ep);
// Start listening for incoming client.
serverSocket.Listen(4);
// Execute callback method when client request connection.
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
while (true) { }
}
catch(Exception ex)
{
Console.WriteLine("from Main(): " + ex.Message);
}
}
private static void AcceptCallback(IAsyncResult ar)
{
try
{
// Store client socket handle.
clientSocket = serverSocket.EndAccept(ar);
Console.WriteLine("Client {0} joined.", clientSocket.RemoteEndPoint);
// Welcome the client when connection established.
buffer = Encoding.ASCII.GetBytes("Hello from server!");
clientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
// Begin receiving data from connected client.
buffer = new byte[1024];
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (Exception ex)
{
Console.WriteLine("from AcceptCallback(): " + ex.Message);
}
}
private static void SendCallback(IAsyncResult ar)
{
// Terminate current send session.
clientSocket.EndSend(ar);
Console.WriteLine("Replied.");
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Store the length of received data.
int received = clientSocket.EndReceive(ar);
if (received == 0)
return;
// Convert the received data to string then print in the console.
Array.Resize<byte>(ref buffer, received);
string text = Encoding.ASCII.GetString(buffer);
Console.WriteLine(text);
Array.Resize<byte>(ref buffer, defaultBufferSize);
// Send back message from the client.
byte[] data = Encoding.ASCII.GetBytes("from server: " + text);
clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (Exception ex)
{
Console.WriteLine("from ReceiveCallback(): " + ex.Message);
}
}
}
}
客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
// Client code
namespace ChatConsoleClient
{
class Program
{
static Socket clientSocket;
static IPEndPoint ep;
static byte[] buffer;
const int defaultBufferSize = 1024;
static void Main(string[] args)
{
try
{
Console.WriteLine("This is client.");
// Create new socket for client.
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Ask for server IP address.
string IP = Console.ReadLine();
// Create endpoint to be connected.
ep = new IPEndPoint(IPAddress.Parse(IP), 1001);
// Start connecting to the server.
clientSocket.BeginConnect(ep, new AsyncCallback(ConnectCallback), null);
buffer = new byte[1024];
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
while (true)
{
string text = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(text);
clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
}
}
catch (Exception ex)
{
Console.WriteLine("from Main(): " + ex.Message);
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
int received = clientSocket.EndReceive(ar);
if (received == 0)
return;
// Convert the received data to string then print in the console.
Array.Resize<byte>(ref buffer, received);
string text = Encoding.ASCII.GetString(buffer);
Console.WriteLine(text);
Array.Resize<byte>(ref buffer, defaultBufferSize);
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
private static void SendCallback(IAsyncResult ar)
{
clientSocket.EndSend(ar);
}
private static void ConnectCallback(IAsyncResult ar)
{
clientSocket.EndConnect(ar);
}
}
}
答案 0 :(得分:0)
我检查了在两个不同系统中运行的客户端和服务器,但它们都具有相同的IP系列并且可以正常工作