473,396 Members | 1,933 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,396 software developers and data experts.

Operation not allowed on non-connected sockets

Hi,

I am using the following code to transfer messages between client and
server.

Upon running that code I am getting the following error message in
method ReceiveFromClient on line Stream s =....
"An unhandled exception of type 'System.InvalidOperationException'
occurred in system.dll

Additional information: Operation not allowed on non-connected
sockets."

The message "Welcome from client" is shown in console.

Can someone help me solve the problem.
Thanks in Advance
class Class1
{
public static string ReceiveFromClient(ref TcpClient clientSocket)
{
XmlSerializer ser = new XmlSerializer(typeof(string));
Stream s = clientSocket.GetStream();
string dt = (string) ser.Deserialize(s);
s.Close();

return dt;
}

public static void SendToClient(string transfer, ref TcpClient
clientSocket)
{
XmlSerializer ser = new XmlSerializer(typeof(string ));
Stream s = clientSocket.GetStream();
ser.Serialize(s, transfer);
s.Close();
} // end method SendToClient

public static void Server()
{
TcpListener listener = new TcpListener(new
IPEndPoint(IPAddress.Parse("127.0.0.1"), 5500));
listener.Start();
TcpClient client = listener.AcceptTcpClient();

string msg = ReceiveFromClient(ref client);
Console.WriteLine("1 - {0}", msg);
SendToClient("Hello World from server", ref client);
}

[STAThread]
static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(Class1.Server));
t.IsBackground = true;
t.Start();

TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5500));

SendToClient("Welcome from client", ref client);
string msg = ReceiveFromClient(ref client);
Console.WriteLine("2 - {0}", msg);

Console.ReadLine();
}
}

Feb 11 '06 #1
3 20775
You closed the stream in ReceiveFromClient and then tried to use it in
SendToClient (and vise versa).

"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

I am using the following code to transfer messages between client and
server.

Upon running that code I am getting the following error message in
method ReceiveFromClient on line Stream s =....
"An unhandled exception of type 'System.InvalidOperationException'
occurred in system.dll

Additional information: Operation not allowed on non-connected
sockets."

The message "Welcome from client" is shown in console.

Can someone help me solve the problem.
Thanks in Advance
class Class1
{
public static string ReceiveFromClient(ref TcpClient clientSocket)
{
XmlSerializer ser = new XmlSerializer(typeof(string));
Stream s = clientSocket.GetStream();
string dt = (string) ser.Deserialize(s);
s.Close();

return dt;
}

public static void SendToClient(string transfer, ref TcpClient
clientSocket)
{
XmlSerializer ser = new XmlSerializer(typeof(string ));
Stream s = clientSocket.GetStream();
ser.Serialize(s, transfer);
s.Close();
} // end method SendToClient

public static void Server()
{
TcpListener listener = new TcpListener(new
IPEndPoint(IPAddress.Parse("127.0.0.1"), 5500));
listener.Start();
TcpClient client = listener.AcceptTcpClient();

string msg = ReceiveFromClient(ref client);
Console.WriteLine("1 - {0}", msg);
SendToClient("Hello World from server", ref client);
}

[STAThread]
static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(Class1.Server));
t.IsBackground = true;
t.Start();

TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5500));

SendToClient("Welcome from client", ref client);
string msg = ReceiveFromClient(ref client);
Console.WriteLine("2 - {0}", msg);

Console.ReadLine();
}
}

Feb 11 '06 #2
If I do not close both streams, application blocks and no message
arrives.
If I do not close in ReceiveFromClient, error as previous is given.
If I do not close in SendToClient, application blocks and no message
arrives.

Can some further help be given.

Feb 11 '06 #3

"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
If I do not close both streams, application blocks and no message
arrives.
If I do not close in ReceiveFromClient, error as previous is given.
If I do not close in SendToClient, application blocks and no message
arrives.

Can some further help be given.


1) You probably need to flush the stream rather than close it.

2) You are confusing yourself and everyone else by calling functions called
ReceiveFromClient and SendToClient in the client. Thus making it unclear
whether it is the client or the server that is having the problem.

3) All use of ref is unnecessary - It is only needed if you need to write
"clientSocket=xxxx" in the method.

4) There is no gaurantee that the server thread has even created the socket
let alone started listening on it when you first send.
Feb 12 '06 #4

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

Similar topics

6
by: Matthew Louden | last post by:
The following ASP code yields the following error, but actually the new record is stored in database. The same error happens when the application deletes a record, such as sqlStmt ="delete from...
3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
1
by: Joško Šugar | last post by:
I have a data transfer between an c# application and web service. The application is in local network and connects to the internet thru a gateway. Connection is ADSL. Web service is on the remote...
1
by: amitbadgi | last post by:
Hello guys I am getting this error while converting an asp applicayion to asp.net Exception Details: System.Runtime.InteropServices.COMException: Operation is not allowed when the object is...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
2
by: Robinson | last post by:
I can start an Asynchronous operation against a data source with SQLCommand.BeginExecuteReader, allowing me to loop, checking for user cancellation before the operation has completed, but how then...
6
by: Joe | last post by:
I've been getting this message at all different times since a few days ago. I understand the message but not why I get it. I don't have any other threads. This happens when closing dialog boxes,...
64
by: Mika | last post by:
Hello, we understand you guys may be able to help. We have a page which has been working great for over a year and gets many hits. However recently something got changed that we cannot seem to...
8
by: Steven | last post by:
Hello, everyone! I find a version of strcpy(), I don't know why it return the unsigned char value. Can I change it into return *s1-*s2? int strcmp(const char *s1, const char *s2) { while...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.