473,809 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

turning sockets off

I'm have an app the opens a socket and I want the socket to stay open till
the user clicks a button. I pass a string of "m_start" to start a listener,
and "m_end" to the function listen_func to stop the code. The problem is when
I try to compile the code I receive the error, "The type or namespace name
'myListener' could not be found." The code is as follows,

public void listen_func(str ing m_s)
{
if (m_s=="m_start" )
{

Encoding ASCII = Encoding.ASCII;

// Create a listener on port 8227
System.Net.Sock ets.TcpListener myListener = new
System.Net.Sock ets.TcpListener (8227);

myListener.Star t();
// Program blocks on Accept() until a client connects
Socket mySocket = myListener.Acce ptSocket();
Byte[] RecvBytes = new Byte[256];
Int32 bytes = mySocket.Receiv e(RecvBytes, RecvBytes.Lengt h, 0);
string s = ASCII.GetString (RecvBytes, 0, bytes);

{
DateTime now = DateTime.Now;
String strDateLine = now.ToShortDate String() + " " + now.ToLongTimeS tring();
Byte[] byteDateLine =
System.Text.Enc oding.ASCII.Get Bytes(strDateLi ne.ToCharArray( ));
mySocket.Send(b yteDateLine,byt eDateLine.Lengt h,0);
}

}
else{

myListener.Stop ();
}

How do I reference the myListener.Stop () in the else of the if statement to
avoid the error?
Nov 16 '05 #1
1 1377
Move it to be an instance variable of your class:
class MyClass {
System.Net.Sock ets.TcpListener myListener = null;

public void listen_func(str ing m_s) {
....
}
}

Just make sure you test it against null before calling Stop on it.

"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:92******** *************** ***********@mic rosoft.com...
I'm have an app the opens a socket and I want the socket to stay open till
the user clicks a button. I pass a string of "m_start" to start a
listener,
and "m_end" to the function listen_func to stop the code. The problem is
when
I try to compile the code I receive the error, "The type or namespace
name
'myListener' could not be found." The code is as follows,

public void listen_func(str ing m_s)
{
if (m_s=="m_start" )
{

Encoding ASCII = Encoding.ASCII;

// Create a listener on port 8227
System.Net.Sock ets.TcpListener myListener = new
System.Net.Sock ets.TcpListener (8227);

myListener.Star t();
// Program blocks on Accept() until a client connects
Socket mySocket = myListener.Acce ptSocket();
Byte[] RecvBytes = new Byte[256];
Int32 bytes = mySocket.Receiv e(RecvBytes, RecvBytes.Lengt h, 0);
string s = ASCII.GetString (RecvBytes, 0, bytes);

{
DateTime now = DateTime.Now;
String strDateLine = now.ToShortDate String() + " " +
now.ToLongTimeS tring();
Byte[] byteDateLine =
System.Text.Enc oding.ASCII.Get Bytes(strDateLi ne.ToCharArray( ));
mySocket.Send(b yteDateLine,byt eDateLine.Lengt h,0);
}

}
else{

myListener.Stop ();
}

How do I reference the myListener.Stop () in the else of the if statement
to
avoid the error?

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3898
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add support for sockets to the system. Thread needs to unblock when: - there is socket ready to be read, or
3
1567
by: Freddy | last post by:
How do you turn a string into a callable function? I'm writing a program which takes user input and then looks up the associated function to run from a shelved dictionary. So, I have a dictionary with entries like this: myDict = {"desc":"This is Case1", "func1": "Handler1", "func2" : "Handler2", "args": "ACCEPT"} Then my program gets the string "Case 1" from the user, and needs to assign func1 and func2 as handlers for certain events....
1
3800
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for pending connections every 500ms. I also have a vb6 application that uses the WinSock control at the other end of the communication tunel. I have to work with vb6 here because it uses less memory than .NET.
0
1833
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up now it's throwing this stream of errors right away. Does anyone have any idea what they mean. I havn't changed the source at all since it was working (which was for four days). It errors here saying An unhandled exception of type...
4
6326
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
4365
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly found out that .net uses system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse me... :o| and help would be really great! links, code, wrappers, tips, whateveryougot... one of my main problems, i guess, is: why don't sockets...
3
11596
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.ReceiveFrom(Byte buffer, Int32 offset, Int32 s
7
6732
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor `network_class::network_class()': ../../../sockets.cpp:64: error: aggregate `addrinfo hints' has incomplete type a nd cannot be defined ../../../sockets.cpp:69: error: `getaddrinfo' undeclared (first use this functio n)
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.