tags:

views:

93

answers:

3

Im getting a connection refused when I try to send some data to my server app using netcat.

server side:

IPAddress ip;
ip = Dns.GetHostEntry("localhost").AddressList[0];
IPEndPoint ipFinal = new IPEndPoint(ip, 12345);
Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipFinal);
socket.Listen(100);
Socket handler = socket.Accept(); ------> it stops here......nothing happens
A: 

Have you tried using a TcpListener instead?

TcpListener listener = new TcpListener(IPAddress.Any, 12345);
listener.Start();
TcpClient client = listener.AcceptTcpClient();

I've found it much easier to use TcpListner and TcpClient rather than Sockets.

Robert Davis
I'll give it a try next timethanks
HoNgOuRu
+2  A: 

There is absolutely nothing wrong with your code, it is functioning perfectly.

The call to Accept() method on the Socket class will block until a connection attempt is made from a client to your TCP port 12345.

"It stops here; nothing happens" is correct and expected behavior, but not accurate description.

What is happening is that your socket is waiting for a client connection.

See: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.accept.aspx

"In blocking mode, Accept blocks until an incoming connection attempt is queued. Once a connection is accepted, the original Socket continues queuing incoming connection requests until you close it."

To test your code, open a telent client (type "telnet" in a command prompt), and enter the command "open localhost 12345". Your code will 'wake up'.

Bill
+1  A: 

Problem solved, I had to move 1 position in the array, cause the first position points to an IPv6 address.

IPAddress ip;
ip = Dns.GetHostEntry("localhost").AddressList[1];
IPEndPoint ipFinal = new IPEndPoint(ip, 12345);
Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipFinal);
socket.Listen(100);
Socket handler = socket.Accept(); //------> it stops here......nothing happens
HoNgOuRu
Please learn to format your code. Just select the code and press Control-K.
John Saunders