我用C#写了一个客户端服务器程序,程序在同一台机器上运行,客户端发送消息,服务器显示te消息,如何编辑我的程序,以便服务器和客户端在不同的机器上运行,此外,还有10个客户端可以连接到服务器
这是我的客户代码:
public partial class ClientForm : Form
{
const int BUFFER_SIZE = 1024;
byte[] sendBuffer = new byte[BUFFER_SIZE];
byte[] rcvBuffer = new byte[BUFFER_SIZE];
Socket clientSocket;
const int PORT = 3333;
public ClientForm()
{
InitializeComponent();
}
void btnConnect_Click(object sender, EventArgs e)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the local host
clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, PORT));
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
EnableSendButton();
}
void btnSend_Click(object sender, EventArgs e)
{
try
{
// Serialize the textBox text before sending
sendBuffer = Encoding.ASCII.GetBytes(textBoxInput.Text);
// Sends contents of textbox to the server
clientSocket.Send(sendBuffer);
// Prepares to receive something, i.e. the echo
clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);//.Receive(rcvBuffer);
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
}
// About Asynchronous Callbacks: http://stackoverflow.com/questions/1047662/what-is-asynccallback
void ReceiveCallback(IAsyncResult AR)
{
try
{
int bytesReceived = clientSocket.EndReceive(AR); // Number of bytes received
byte[] rcvBufferTrim = new byte[bytesReceived];
Array.Copy(rcvBuffer, rcvBufferTrim, bytesReceived); // Removes trailing nulls
string text = Encoding.ASCII.GetString(rcvBufferTrim); // Convert bytes into string
AppendToTextBox(DateTime.Now.ToString("HH:mm:ss") + ": " + text); // Displays buffer contents as text
// Starts receiving data again
clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
}
}
和服务器代码:
public partial class ServerForm : Form
{
const int BUFFER_SIZE = 1024;
byte[] rcvBuffer = new byte[BUFFER_SIZE]; // To recieve data from the client
Socket serverSocket;
Socket clientSocket; // We will only accept one client socket for now
const int PORT = 3333;
public ServerForm()
{
InitializeComponent();
StartServer();
}
void StartServer()
{
try
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT));
serverSocket.Listen(10); // Listen(1) means a maximum of one client pending
serverSocket.BeginAccept(AcceptCallback, null);
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
}
// About Asynchronous Callbacks: http://stackoverflow.com/questions/1047662/what-is-asynccallback
void AcceptCallback(IAsyncResult AR)
{
try
{
clientSocket = serverSocket.EndAccept(AR); // Client connected successfully, waiting for requests
AppendToTextBox("Client connected successfully...");
clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);
}
catch (Exception ex) { AppendToTextBox(ex.Message); }
}
void ReceiveCallback(IAsyncResult AR)
{
try
{
int bytesReceived = clientSocket.EndReceive(AR); // Number of bytes received
byte[] rcvBufferTrim = new byte[bytesReceived];
Array.Copy(rcvBuffer, rcvBufferTrim, bytesReceived); // Removes trailing nulls
string text = Encoding.ASCII.GetString(rcvBufferTrim); // Convert bytes into string
AppendToTextBox("Received Text: " + text);
if (text == "getdate")
text = "\t"+ System.DateTime.Now.ToString("dd.MM.yyyy");
else if (text == "gettime")
text = "\t"+ System.DateTime.Now.ToString("h:mm:ss ");
///////////////////////////////////////////////////////////////////////////////////
// Reply with echo
byte[] echoBuffer = Encoding.ASCII.GetBytes("Echo... " + text);
clientSocket.Send(echoBuffer);
// Starts receiving data again
clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);
}
catch (Exception ex) { AppendToTextBox("Exception: " + ex.Message + "..."); }
}
// Provides a thread-safe way to append text to the textbox
void AppendToTextBox(string text)
{
MethodInvoker invoker = new MethodInvoker(delegate
{
textBox.Text += text + "\r\n\r\n";
textBox.SelectionStart = textBox.TextLength;
textBox.ScrollToCaret();
}); // "\r\n" are for new lines
this.Invoke(invoker);
}
}
答案 0 :(得分:0)
要允许服务器和客户端位于两台不同的计算机上,您需要更改clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, PORT));
到
IPAddress ipAd = IPAddress.Parse(YOUR IP HERE);
clientSocket.Connect(ipAd, YOUR PORT HERE);
如果你想查看它,我也在我的图书馆里建了一个简单的客户:https://github.com/TheRealNOIG/TylerGregorcyksLibrary/blob/master/TylerGregorcyksLibrary/Main/SimpleClient.cs
现在制作一台可以处理多个客户的服务器有点困难,但幸运的是我已经做了一个例子
class Server
{
private static int bufferSize = 2048;
private static byte[] buffer = new byte[bufferSize];
private static List<Socket> clientSockets = new List<Socket>();
private static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
Console.Title = "Server";
SetupServer();
Console.ReadKey();
CloseAllSockets();
}
private static void SetupServer()
{
Console.WriteLine("Settings Up Server Plz Wait");
serverSocket.Bind(new IPEndPoint(IPAddress.Any, YOUR PORT HERE));
serverSocket.Listen(10);
serverSocket.BeginAccept(new AsyncCallback(CallBack), null);
Console.WriteLine("Server Made");
}
private static void CallBack(IAsyncResult e)
{
try
{
Socket socket = serverSocket.EndAccept(e);
clientSockets.Add(socket);
Console.WriteLine("Client Connected");
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
serverSocket.BeginAccept(new AsyncCallback(CallBack), null);
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
private static void ReceiveCallBack(IAsyncResult e)
{
try
{
Socket socket = (Socket)e.AsyncState;
int received;
try
{
received = socket.EndReceive(e);
}
catch (SocketException)
{
Console.WriteLine("Client forcefully disconnected");
socket.Close();
clientSockets.Remove(socket);
return;
}
byte[] dataBuf = new byte[received];
Array.Copy(buffer, dataBuf, received);
string text = Encoding.ASCII.GetString(dataBuf);
Console.WriteLine("Client request: " + text);
string response = string.Empty;
try
{
if (command.ToLower() == "What Time is it")
{
response = DateTime.Now.ToString();
}
else if (command.ToLower() == "Whats your name")
{
response = "Tyler Gregorcyk";
}
else
{
response = "Invaled";
}
}
catch (Exception et) { Console.WriteLine(et.Message); socket.Close(); clientSockets.Remove(socket); }
if(response == string.Empty)
{
response = "Invaled";
}
if (response != string.Empty)
{
Console.WriteLine("Sent To Client: " + response);
byte[] data = Encoding.ASCII.GetBytes(response);
socket.Send(data);
serverSocket.BeginAccept(new AsyncCallback(CallBack), null);
}
conn.Close();
socket.BeginReceive(buffer, 0, bufferSize, SocketFlags.None, ReceiveCallBack, socket);
}
catch (Exception) { }
}
private static void CloseAllSockets()
{
foreach (Socket socket in clientSockets)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
serverSocket.Close();
}
}
BTW这是YouTube上教程的编辑版本:https://www.youtube.com/watch?v=xgLRe7QV6QI