我完全新的插座用C#编程,我试图得到两个运行.EXE文件交谈海誓山盟:C#插座帮助
static void Main(string[] args) {
bool sender = !false;
if (args.Length > 0) sender = !true;
if (sender) {
try {
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8221);
sock.Connect(ipe);
while (true) {
string toSend = Console.ReadLine();
sock.Send(Encoding.UTF32.GetBytes(toSend));
}
}
catch (SocketException e) {
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
else {
try {
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 8221);
sock.Bind(ipe);
sock.Listen(4);
while (true) {
if (!sock.Connected) continue;
byte[] buffer = new byte[1024];
if (sock.Receive(buffer) > 0) Console.WriteLine(Encoding.UTF32.GetString(buffer));
}
}
catch (SocketException e) {
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
此刻虽然,这两个程序运行没有错误,但他们似乎并没有连接(如果(!sock.Connected)总是正确的)。
请帮忙,谢谢。
什么是!true和!false。完全没有意义。 – 2010-09-07 09:54:04
啊,是的,呃...我在视觉工作室测试它,但我想用调试器测试程序的另一半,所以不是大惊小怪,而是翻转了真实/错误。 – Xenoprimate 2010-09-07 09:57:36
当他们无法连接(在'sock.Connect(ipe)'声明)时,你会得到一个异常吗? “连接”不是真正的连接状态,而是一个标志,说上次发送或接收是否成功 – Patrick 2010-09-07 10:02:30