1

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.

7
  • 1
    what's with the !true and !false. Totally pointless. Commented Sep 7, 2010 at 9:54
  • Ah, yeah, well... I was testing it in visual studio, but I wanted to test the other 'half' of the program with the debugger, so instead of a lot of fuss I just flipped the true/false. Commented Sep 7, 2010 at 9:57
  • Do you get an exception when they are unable to connect (at the sock.Connect(ipe) statement)? Connected is not really a state of the connection, but rather a flag saying that the last send or receive was successful or not Commented Sep 7, 2010 at 10:02
  • Ah, interesting. And no, no exception. It rolls onto the while block. Commented Sep 7, 2010 at 10:03
  • @Motig - I've re-formatted your code to un-indent it a little so it's a bit easier to read (even if you have used "opening brace on same line" ;-), as more of it's on screen rather than scrolled off now =) Commented Sep 7, 2010 at 10:07

2 Answers 2

2

Edit: Noticed that you don't have a sock.Accept() in your listener. You need to get a incoming socket that you can "talk on". Place a Socket c = sock.Accept() before your while(true) loop and use the c socket to send and receive data

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 8221); sock.Bind(ipe); sock.Listen(4); Socket c = sock.Accept(); // added while (true) { if (!c.Connected) continue; byte[] buffer = new byte[1024]; if (c.Receive(buffer) > 0) Console.WriteLine(Encoding.UTF32.GetString(buffer)); } 

And; As a recommendation, whenever I make socket programs, I usually send the number of bytes that I want to send before the actual buffer. If you are able to make your sender and listener connect this might be the fix to have them exchange data.

public void send(byte[] buf) { socket.Send(BitConverter.GetBytes(buf.Length), SocketFlags.None); socket.Send(buf, buf.Length, SocketFlags.None); } public byte[] receive() { byte[] lengthBytes = new byte[4]; int read = socket.Receive(lengthBytes); // read contains the number of read bytes, so we can check it if we want int length = BitConverter.GetInt32(lengthBytes); byte[] buf = new byte[length]; socket.Receive(buf); return buf; } 
Sign up to request clarification or add additional context in comments.

7 Comments

I was just constructing an answer - you you got there first :) - critically, you need the Recieve function call ...
@Motig: Yes, but instead of just receiving data without knowing what it is, you can send the number of bytes first, so your listener knows how much data it should receive.
Oh, and as a security note, don't trust user input without sanitation. Doing so can end in Denial of Service attacks for instance.
But Patrick, that doesn't solve the issue that I'm not getting anything at all... Or do you have to know the length to avoid some sort of error?
@Motig: But have you tried sending something as simple as an int32? Then you know the length of the receiving buffer (instead of receiving 1024 bytes, which will block until it's filled).
|
1

No where in your server code you are accepting the incoming connections. You will have to accept and create a socket at the server end for the new incoming clients.

Socket newSock = sock.Accept(); if (!newSock.Connected) continue; else {...} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.