Click here to Skip to main content
15,917,568 members
Home / Discussions / C#
   

C#

 
GeneralRe: Directory Stream Pin
biggy116-Apr-09 10:21
biggy116-Apr-09 10:21 
AnswerRe: Directory Stream Pin
Xmen Real 15-Apr-09 18:39
professional Xmen Real 15-Apr-09 18:39 
QuestionWatching for Removeable Drives Pin
Jammer15-Apr-09 12:20
Jammer15-Apr-09 12:20 
AnswerRe: Watching for Removeable Drives Pin
Luc Pattyn15-Apr-09 12:42
sitebuilderLuc Pattyn15-Apr-09 12:42 
AnswerRe: Watching for Removeable Drives Pin
Nilesh Hapse15-Apr-09 20:16
Nilesh Hapse15-Apr-09 20:16 
QuestionSocket Connection Shutdown and Close Pin
kikeman15-Apr-09 11:41
kikeman15-Apr-09 11:41 
AnswerRe: Socket Connection Shutdown and Close Pin
Colin Angus Mackay15-Apr-09 12:12
Colin Angus Mackay15-Apr-09 12:12 
GeneralRe: Socket Connection Shutdown and Close Pin
kikeman16-Apr-09 4:45
kikeman16-Apr-09 4:45 
Hi,

Ok, Thanks for the explanation of the IDisposable, however, I get the same error Frown | :(
The code is simple, I have a form which instantiate the socket owner class CBaseMsg in the constructor:

public partial class FrmMain : Form
{
    CBaseMsg BaseMsg;   //SOCKET INSTANCE CREATED HERE

    //CONSTRUCTOR:
    public FrmMain()
    {
        InitializeComponent();
        BaseMsg = new CBaseMsg();
    }

    //TEST BUTTON
    private void buttonTest_Click(object sender, EventArgs e)
    {
        BaseMsg.SendSomething("Test");
    }
}


The class that instantiates the socket:
What I basically do is to call Dispose() from the Finalizer. I tried also to use the dispose alone, but if I put there a break point it seems that never runs.
As you can see I am not closing nor diposing the Sender instance before calling the dispose method or finalizer. The instances should be "alive" while the form exist.

Should I call dispose of the CBaseMsg from the Dispose of the FrmMain?
Another Question:
Should I really need to Shutdown() the socket? (Close() works just fine in the Finalizer and Dispose()...)

Maybe it is already automatic and I am just investing unnecessary time ...

class CBaseMsg : IDisposable
{
    //Socket Related Connection.
    IPHostEntry ipHostInfo;
    IPAddress ipAddress;
    IPEndPoint remoteEP;
    Socket Sender;

    //CONSTRUCTOR
    public CBaseMsg()
    {
        //Get connected to the server when the object is created.
        // Establish the remote endpoint for the socket.
        ipHostInfo = Dns.GetHostEntry("127.0.0.1");
        ipAddress = ipHostInfo.AddressList[0];
        remoteEP = new IPEndPoint(ipAddress, 55065);

        // Create a TCP/IP  socket.
        Sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        Sender.ReceiveTimeout = 5000;

        //Get Connected.
        Sender.Connect(remoteEP);
    }

    //FINALIZER
    ~CBaseMsg()
    {
        Dispose();
    }

    public void Dispose()
    {
        if (Sender != null)
        {
            Sender.Shutdown(SocketShutdown.Both);   //EXCEPTION !!
                                                    //ObjectDisposeException was handled !!
            Sender.Close();
        }
    }

    public void SendSomething(string Msg)
    {
        try
        {
            //GET BYTES.
            byte[] buffMsg = new byte[Msg.Length];
            buffMsg = Encoding.ASCII.GetBytes(Msg);

            //SEND TELEGRAM; SYNCHRONOUS
            int bytesSent = Sender.Send(buffMsg);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

}

GeneralRe: Socket Connection Shutdown and Close Pin
Colin Angus Mackay16-Apr-09 5:05
Colin Angus Mackay16-Apr-09 5:05 
GeneralRe: Socket Connection Shutdown and Close Pin
kikeman16-Apr-09 5:39
kikeman16-Apr-09 5:39 
GeneralRe: Socket Connection Shutdown and Close Pin
Colin Angus Mackay16-Apr-09 5:48
Colin Angus Mackay16-Apr-09 5:48 
AnswerRe: Socket Connection Shutdown and Close Pin
kikeman16-Apr-09 5:55
kikeman16-Apr-09 5:55 
QuestionTask Scheduler Pin
BlitzPackage15-Apr-09 11:40
BlitzPackage15-Apr-09 11:40 
AnswerRe: Task Scheduler Pin
Pete O'Hanlon15-Apr-09 11:45
mvePete O'Hanlon15-Apr-09 11:45 
GeneralRe: Task Scheduler Pin
BlitzPackage15-Apr-09 15:59
BlitzPackage15-Apr-09 15:59 
AnswerRe: Task Scheduler Pin
Rolando CC15-Apr-09 12:52
professionalRolando CC15-Apr-09 12:52 
GeneralRe: Task Scheduler Pin
BlitzPackage15-Apr-09 15:58
BlitzPackage15-Apr-09 15:58 
AnswerRe: Task Scheduler Pin
Giorgi Dalakishvili15-Apr-09 19:45
mentorGiorgi Dalakishvili15-Apr-09 19:45 
QuestionVS2008 Startup, Environment, Options Dialog Box missing Pin
Neville Franks15-Apr-09 10:51
Neville Franks15-Apr-09 10:51 
AnswerRe: VS2008 Startup, Environment, Options Dialog Box missing Pin
Neville Franks15-Apr-09 11:00
Neville Franks15-Apr-09 11:00 
JokeWhat are trees doing in your pants? Pin
Naruki15-Apr-09 15:18
Naruki15-Apr-09 15:18 
GeneralRe: What are trees doing in your pants? Pin
Neville Franks15-Apr-09 16:26
Neville Franks15-Apr-09 16:26 
JokeRe: What are trees doing in your pants? Pin
Naruki15-Apr-09 16:54
Naruki15-Apr-09 16:54 
GeneralRe: What are trees doing in your pants? Pin
Neville Franks15-Apr-09 18:00
Neville Franks15-Apr-09 18:00 
QuestionProgress while reading a file Pin
nike_arh15-Apr-09 10:38
nike_arh15-Apr-09 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.