473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(string m_s)
{
if (m_s=="m_start")
{

Encoding ASCII = Encoding.ASCII;

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

myListener.Start();
// Program blocks on Accept() until a client connects
Socket mySocket = myListener.AcceptSocket();
Byte[] RecvBytes = new Byte[256];
Int32 bytes = mySocket.Receive(RecvBytes, RecvBytes.Length, 0);
string s = ASCII.GetString(RecvBytes, 0, bytes);

{
DateTime now = DateTime.Now;
String strDateLine = now.ToShortDateString() + " " + now.ToLongTimeString();
Byte[] byteDateLine =
System.Text.Encoding.ASCII.GetBytes(strDateLine.To CharArray());
mySocket.Send(byteDateLine,byteDateLine.Length,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 1359
Move it to be an instance variable of your class:
class MyClass {
System.Net.Sockets.TcpListener myListener = null;

public void listen_func(string m_s) {
....
}
}

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

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.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(string m_s)
{
if (m_s=="m_start")
{

Encoding ASCII = Encoding.ASCII;

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

myListener.Start();
// Program blocks on Accept() until a client connects
Socket mySocket = myListener.AcceptSocket();
Byte[] RecvBytes = new Byte[256];
Int32 bytes = mySocket.Receive(RecvBytes, RecvBytes.Length, 0);
string s = ASCII.GetString(RecvBytes, 0, bytes);

{
DateTime now = DateTime.Now;
String strDateLine = now.ToShortDateString() + " " +
now.ToLongTimeString();
Byte[] byteDateLine =
System.Text.Encoding.ASCII.GetBytes(strDateLine.To CharArray());
mySocket.Send(byteDateLine,byteDateLine.Length,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
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...
3
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...
1
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...
0
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...
4
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
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...
3
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...
7
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.