C# 如何使用C语言中的Client&Server指定变量中应存储哪些数据?
所以,我正在开发一款使用服务器的游戏。我试图添加用户名和密码系统,但遇到问题。当我将数据从客户端发送到服务器时,服务器会接收到数据,但我想不出一个系统来指定数据是什么以及应该存储在哪里 例如,假设我将用户名从客户端发送到服务器。服务器收到它,但我如何让它知道它是用户名,而不是密码 如果有人能帮忙,谢谢 注意:如果有帮助,我正在VisualStudio中使用C 服务器代码 客户端代码C# 如何使用C语言中的Client&Server指定变量中应存储哪些数据?,c#,database,sockets,C#,Database,Sockets,所以,我正在开发一款使用服务器的游戏。我试图添加用户名和密码系统,但遇到问题。当我将数据从客户端发送到服务器时,服务器会接收到数据,但我想不出一个系统来指定数据是什么以及应该存储在哪里 例如,假设我将用户名从客户端发送到服务器。服务器收到它,但我如何让它知道它是用户名,而不是密码 如果有人能帮忙,谢谢 注意:如果有帮助,我正在VisualStudio中使用C 服务器代码 客户端代码 正如rob所说,在发送数据之前,您可能希望使用数据交换格式格式化数据。我更喜欢使用Newtonsoft.JSON库
正如rob所说,在发送数据之前,您可能希望使用数据交换格式格式化数据。我更喜欢使用Newtonsoft.JSON库提供的JSON。你需要有一个参考资料 客户端代码 服务器代码
完全取决于您发送数据的方式。你可能想研究数据格式,如XML、JSON等。我想发送代码可能会有所帮助,leme将其添加到帖子中。随时都行!很乐意帮忙
int recv; // Holds how much data we are reading
byte[] data = new byte[1024]; // Byte Array of data, used for everything recived and sent to the server
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 904); // Listener for connections
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Stores connection from client
socket.Bind(endpoint); // Bind incoming connection to socket
Console.WriteLine(" >> Waiting for client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 904); // Wait for connection, once recived, sets sender to connected IP
EndPoint tmpRemote = (EndPoint)sender; // Stores client temp
recv = socket.ReceiveFrom(data, ref tmpRemote); // Stores all client info, get data it's sending (And how much data is in it)
Console.WriteLine(" >> Data received from {0}", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); // Convert data into string and display it
string welcome = " >> Welcome to AlphaNET!"; // Welcome Message
data = Encoding.ASCII.GetBytes(welcome); // Convert Welcome to sendable bytes
if (socket.Connected) //Checking if socket is still connected, if so, send welcome message
{
socket.Send(data); // Send welcome message
}
while (true) // Our loop to check for data, goes on forever
{
if (!socket.Connected) // Checks to see if client is not connected, if true, write to console and break the loop
{
Console.WriteLine(" >> Data Sent");
}
data = new byte[1024]; // Resets data var
recv = socket.ReceiveFrom(data, ref tmpRemote);
if (recv == 0) // If no info is recived, break loop
{
break;
}
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
Console.ReadLine();
socket.Close();
public void sendUsr()
{
String username = usrBox.Text;
byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes(username);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Makes socket to send data
socket.SendTo(packetData, ep); // Sends data to server
}
String dataToSend = JsonConvert.SerializeObject(new { username = usrBox.Text, password = pwdBox.Text });
recv = socket.ReceiveFrom(data, ref tmpRemote); // Stores all client info, get data it's sending (And how much data is in it)
dynamic receivedData = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(data, 0, recv));
String username = (string)receivedData.username;
String password = (string)receivedData.password;