C#发送自定义套接字字符串

时间:2015-07-21 11:04:29

标签: c#

我有这段代码:

public static List<Socket> samp = new List<Socket>();

IPHostEntry host = null;
Socket sock;
host = Dns.GetHostEntry(sending ip..);

foreach (IPAddress address in host.AddressList)
{
    IPEndPoint ipe = new IPEndPoint(address, 7777);
    sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    sock.Connect(ipe);
    if (sock.Connected)
    {
        samp.Add(sock);
    }
}

此代码的工作好的。它发送我的IP。它在控制台中的外观如何: enter image description here

如何发送自定义字符串?例如,我试过这段代码:

if (sock.Connected)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    int i = sock.Send(msg);
    MessageBox.Show(string.Format("Sent {0} bytes.", i));
    samp.Add(sock);
}

显示消息:已发送14个字节。但在控制台中没有打印..如何将此文本发送到控制台?我也试过这个:

sock.Send(Encoding.UTF8.GetBytes("This is a test"));

这不打印:( 提前谢谢!

0 个答案:

没有答案