I'm trying to set up a listener. I've confirmed that the port we are using is not blocked, the IP address is correct, I am sending data to the server, the server is replying to my message requests (verified via ethereal). My problem is that in attempting to grad that data from the server, I cannot accept the socket. Here is my code for my listener class:
using
System;using
System.Collections;using
System.Collections.Generic;using
System.ComponentModel;using
System.IO;using
System.Net;using
System.Net.Sockets;using
System.Text;using
System.Threading;using
System.Windows.Forms;namespace
TPCP4{
public class Listener{
private TcpListener myListener ; private Socket mySocket; private Thread th ; private IPAddress[] array; private int port = 2700 ; //The constructor which make the TcpListener start listening on the //given port. //It also calls a Thread on the method StartListen(). public Listener(){
try{
//start listing on the given port string hostname = System.Net.Dns.GetHostName();array = System.Net.
Dns.GetHostAddresses(hostname); if (array != null){
myListener =
new TcpListener(array[0], port); MessageBox.Show("Starting Listener");myListener.Start();
th =
new Thread(new ThreadStart(StartListen));th.Start();
}
}
catch (Exception e){
MessageBox.Show("Exception 1: " + e.ToString());}
}
//This method Accepts new connection and //First it receives the welcome massage from the client, //Then it sends the Current date time to the Client. private void StartListen(){
MessageBox.Show("Entering While"); while (true){
// Accept blocks until a client connects MessageBox.Show("Accepting Socket");mySocket = myListener.AcceptSocket();
//Accept a new connection if (mySocket.Connected){
MessageBox.Show("IN THE LOOP!");}
mySocket.Close();
}
}
}
}
The issue occurs on the code:
mySocket = myListener.AcceptSocket();
Stepping through on the debugger, I step to this line of code, attempt to step over, but at that point the debugger doesn't step over, but leaves this class entirely and continues to execute the rest of my application. I should also mention that this class is being instantiated by a main window form. The application does not crash, no exception is thrown.
I've been working on this for two days and I'm at my wits end. The code seems pretty straight forward, but I am a C# newbie.

Why can't I accept a socket?
AGuther
hi,
why do you use client as socket , its easier if you used tcpClient for me i use something like this
TcpClient clnt = srvr.AcceptTcpClient();
NetworkStream ns = clnt.GetStream();
i start using this stream to convey data
hope this helps
Jenny Maria Cuenca
Using a Socket can be just as easy and provides some additional functionality that you can't do with a TcpClient.
Trying to debug while accepting sockets has never worked for me. Try running it without debugging (ctrl + f5) and put a messagebox after the accept socket. When working with a listener thread I never bother trying to debug in debug mode.
Michael_B
hi,
i'm a little bit confused , you said the server reply for your client that mean it accepted connection , yet you say you can't accept connection
or do you mean you can't debug it
best regards