本文整理汇总了C#中Data.toByte方法的典型用法代码示例。如果您正苦于以下问题:C# Data.toByte方法的具体用法?C# Data.toByte怎么用?C# Data.toByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Data
的用法示例。
在下文中一共展示了Data.toByte方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: bt_connect_Click
private void bt_connect_Click(object sender, EventArgs e)
{
string serverip = Interaction.InputBox("Type in the IP where you want to join.", "Connect to a server", "csoty.ddns.net");
if (serverip.Length > 0)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = Dns.GetHostEntry(serverip).AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 3333);
clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
tBox_msg.Enabled = true;
bt_send.Enabled = true;
Button bt_dc = new Button();
bt_dc.Text = "Disconnect";
bt_dc.Top = bt_connect.Top + bt_connect.Height + 5;
bt_dc.Left = bt_connect.Left;
bt_dc.Click += new EventHandler(bt_dc_Click);
this.Controls.Add(bt_dc);
bt_dc.Show();
try
{
Data sendMsg = new Data();
sendMsg.cmd = Command.List;
sendMsg.user = user;
sendMsg.Msg = null;
byteData = sendMsg.toByte();
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnRecieve), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
示例2: OnRecieve
public void OnRecieve(IAsyncResult ar)
{
try
{
Socket clientSocket = (Socket)ar.AsyncState;
//MessageBox.Show(ar.AsyncState.ToString());
clientSocket.EndReceive(ar);
// Welp at least lcient doesn't die
//MessageBox.Show(clientSocket.ToString() + "\n" + clientSocket.SocketType.ToString() + "\n" + ar.AsyncState.ToString() + "\n" + ar.ToString());
Data recMsg = new Data(byteData);
Data sendMsg = new Data();
byte[] msg;
sendMsg.cmd = recMsg.cmd;
sendMsg.user = recMsg.user;
switch (recMsg.cmd)
{
case Command.Login:
ClientInfo clientInfo = new ClientInfo();
clientInfo.socket = clientSocket;
clientInfo.user = recMsg.user;
clientList.Add(clientInfo);
sendMsg.Msg = ">> " + recMsg.user + " has joined the server \\o/";
break;
case Command.Logout:
int id = 0;
foreach (ClientInfo client in clientList)
{
if (client.socket == clientSocket)
{
clientList.RemoveAt(id);
break;
}
++id;
}
clientSocket.Close();
sendMsg.Msg = ">> " + recMsg.user + " just left the server :c ";
break;
case Command.Message:
sendMsg.Msg = recMsg.user + ": " + recMsg.Msg;
break;
case Command.List:
sendMsg.cmd = Command.List;
sendMsg.user = recMsg.user;
sendMsg.Msg = null;
foreach (ClientInfo client in clientList)
{
sendMsg.Msg += client.user + "*";
}
msg = sendMsg.toByte();
clientSocket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
break;
}
if (sendMsg.cmd != Command.List)
{
msg = sendMsg.toByte();
foreach (ClientInfo c in clientList)
{
if (c.socket != clientSocket || sendMsg.cmd != Command.Login)
{
c.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), c.socket);
}
}
lBox_log.Items.Add(sendMsg.Msg); //2monitor.
}
if (recMsg.cmd != Command.Logout)
{
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnRecieve), clientSocket);
}
}
catch (Exception ex)
{
// Yeah async stuff sucks with windows controls. Gonna go eat, go solve stuff mewanwhile
MessageBox.Show(ex.ToString());
}
}
示例3: OnConnect
private void OnConnect(IAsyncResult ar)
{
try
{
clientSocket = (Socket)ar.AsyncState;
if (clientSocket != null)
{
clientSocket.EndConnect(ar);
}
Data sendMsg = new Data();
sendMsg.cmd = Command.Login;
sendMsg.user = tB_nick.Text;
sendMsg.Msg = null;
byte[] b = sendMsg.toByte();
clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnConnectSend), clientSocket);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
示例4: CClient_FormClosing
private void CClient_FormClosing(object sender, FormClosingEventArgs e)
{
if (tBox_msg.Enabled == true)
{
try
{
Data sendMsg = new Data();
sendMsg.cmd = Command.Logout;
sendMsg.user = user;
sendMsg.Msg = null;
byte[] b = sendMsg.toByte();
clientSocket.Send(b, 0, b.Length, SocketFlags.None);
clientSocket.Close();
}
catch (ObjectDisposedException)
{
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
示例5: bt_send_Click
private void bt_send_Click(object sender, EventArgs e)
{
try
{
Data sendMsg = new Data();
sendMsg.user = user;
sendMsg.Msg = tBox_msg.Text;
sendMsg.cmd = Command.Message;
byte[] buf = sendMsg.toByte();
clientSocket.BeginSend(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
tB_Enter(tBox_msg, null);
}
catch (Exception ex)
{
MessageBox.Show("Could not send message to server \n Exception: " + ex.ToString());
}
try
{
Data reqList = new Data();
reqList.user = user;
reqList.Msg = null;
reqList.cmd = Command.List;
byte[] req = reqList.toByte();
clientSocket.BeginSend(req, 0, req.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
示例6: bt_dc_Click
private void bt_dc_Click(object sender, EventArgs e)
{
try
{
Data sendMsg = new Data();
sendMsg.cmd = Command.Logout;
sendMsg.user = user;
sendMsg.Msg = null;
byte[] b = sendMsg.toByte();
clientSocket.Send(b, 0, b.Length, SocketFlags.None);
clientSocket.Close();
}
catch (ObjectDisposedException)
{
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Button bt = sender as Button;
bt.Dispose();
bt_send.Enabled = false;
tBox_msg.Enabled = false;
tB_nick.Enabled = true;
lb_ip.Text = "0.0.0.0";
lb_ip.ForeColor = Color.Red;
lb_constatus.Text = "Disconnected";
lb_constatus.ForeColor = Color.Red;
}
示例7: OnRecieve
public void OnRecieve(IAsyncResult ar)
{
try
{
Socket clientSocket = (Socket)ar.AsyncState;
clientSocket.EndReceive(ar);
MessageBox.Show(clientSocket.ToString() + "\n" + clientSocket.SocketType.ToString() + "\n" + ar.AsyncState.ToString() + "\n" + ar.ToString());
Data recMsg = new Data(byteData);
Data sendMsg = new Data();
byte[] msg;
sendMsg.cmd = recMsg.cmd;
sendMsg.user = recMsg.user;
switch (recMsg.cmd)
{
case Command.Login:
ClientInfo clientInfo = new ClientInfo();
clientInfo.socket = clientSocket;
clientInfo.user = recMsg.user;
clientList.Add(clientInfo);
sendMsg.Msg = ">> " + recMsg.user + " has joined the server \\o/";
break;
case Command.Logout:
int id = 0;
foreach (ClientInfo client in clientList)
{
if (client.socket == clientSocket)
{
clientList.RemoveAt(id);
break;
}
++id;
}
clientSocket.Close();
sendMsg.Msg = ">> " + recMsg.user + " just left the server :c ";
break;
case Command.Message:
sendMsg.Msg = recMsg.user + ": " + recMsg.Msg;
break;
case Command.List:
sendMsg.cmd = Command.List;
sendMsg.user = null;
sendMsg.Msg = null;
foreach (ClientInfo client in clientList)
{
sendMsg.Msg += client.user + "*";
}
msg = sendMsg.toByte();
clientSocket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
break;
}
if (sendMsg.cmd != Command.List)
{
msg = sendMsg.toByte();
foreach (ClientInfo c in clientList)
{
if (c.socket != clientSocket || sendMsg.cmd != Command.Login)
{
c.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), c.socket);
}
}
tBox_log.Text += sendMsg.Msg + "\r\n";
}
if (recMsg.cmd != Command.Logout)
{
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnRecieve), clientSocket);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}