I'm completely new to socket programming with C#, I'm trying to get two running .exes to talk to eachother:
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(); } } } At the moment though, both programs run without error, but they don't seem to connect (if (!sock.Connected) always is true).
Please help, thank you.
sock.Connect(ipe)statement)?Connectedis not really a state of the connection, but rather a flag saying that the last send or receive was successful or not