473,728 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ReceiveFromClie nt on line Stream s =....
"An unhandled exception of type 'System.Invalid OperationExcept ion'
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 ReceiveFromClie nt(ref TcpClient clientSocket)
{
XmlSerializer ser = new XmlSerializer(t ypeof(string));
Stream s = clientSocket.Ge tStream();
string dt = (string) ser.Deserialize (s);
s.Close();

return dt;
}

public static void SendToClient(st ring transfer, ref TcpClient
clientSocket)
{
XmlSerializer ser = new XmlSerializer(t ypeof(string ));
Stream s = clientSocket.Ge tStream();
ser.Serialize(s , transfer);
s.Close();
} // end method SendToClient

public static void Server()
{
TcpListener listener = new TcpListener(new
IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 5500));
listener.Start( );
TcpClient client = listener.Accept TcpClient();

string msg = ReceiveFromClie nt(ref client);
Console.WriteLi ne("1 - {0}", msg);
SendToClient("H ello World from server", ref client);
}

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

TcpClient client = new TcpClient();
client.Connect( new IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 5500));

SendToClient("W elcome from client", ref client);
string msg = ReceiveFromClie nt(ref client);
Console.WriteLi ne("2 - {0}", msg);

Console.ReadLin e();
}
}

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

"Curious" <th****@mail.gl obal.net.mt> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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 ReceiveFromClie nt on line Stream s =....
"An unhandled exception of type 'System.Invalid OperationExcept ion'
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 ReceiveFromClie nt(ref TcpClient clientSocket)
{
XmlSerializer ser = new XmlSerializer(t ypeof(string));
Stream s = clientSocket.Ge tStream();
string dt = (string) ser.Deserialize (s);
s.Close();

return dt;
}

public static void SendToClient(st ring transfer, ref TcpClient
clientSocket)
{
XmlSerializer ser = new XmlSerializer(t ypeof(string ));
Stream s = clientSocket.Ge tStream();
ser.Serialize(s , transfer);
s.Close();
} // end method SendToClient

public static void Server()
{
TcpListener listener = new TcpListener(new
IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 5500));
listener.Start( );
TcpClient client = listener.Accept TcpClient();

string msg = ReceiveFromClie nt(ref client);
Console.WriteLi ne("1 - {0}", msg);
SendToClient("H ello World from server", ref client);
}

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

TcpClient client = new TcpClient();
client.Connect( new IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 5500));

SendToClient("W elcome from client", ref client);
string msg = ReceiveFromClie nt(ref client);
Console.WriteLi ne("2 - {0}", msg);

Console.ReadLin e();
}
}

Feb 11 '06 #2
If I do not close both streams, application blocks and no message
arrives.
If I do not close in ReceiveFromClie nt, 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.gl obal.net.mt> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
If I do not close both streams, application blocks and no message
arrives.
If I do not close in ReceiveFromClie nt, 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
ReceiveFromClie nt 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=x xxx" 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
46902
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 test where username='2323'" Any ideas? Thanks! <% Dim objRS, sqlStmt Set objRS = Server.CreateObject ("ADODB.Recordset") sqlStmt = "insert into test VALUES ('2341', '2341');"
3
5895
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 Bytes=7063896) How can I correlate which part of the SQL statement is running on full table scan. Please see below for the code and explain plan SQL Code
43
26512
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
8196
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 server. Timeuots are configured as follows: local timeout: <add key="WebService.Timeout" value="300000" /> remote timeout: <httpRuntime executionTimeout="300" maxRequestLength="10000"/>
1
2689
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 closed. Source Error: Line 168: rs = Server.CreateObject("ADODB.recordset")Line 169:
8
4852
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 I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
2
4803
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 to cancel the SQLCommand if the user wishes to? The "Cancel" method states: ".......... The Cancel method cannot be used to cancel a pending asynchronous operation." The scenario I'm thinking about here concerns a potentially long-running...
6
32247
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, doing a drag and drop and a few other things which I don't remember right now. It only happens while I'm running within the IDE and only seems to be on my computer. We've tested the same thing on other machines running VS.2005 and there is no...
64
6061
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 find, and now *sometimes* if you refresh the page (generally while it is still loading) in IE7, we get the popup window error: Internet Explorer cannot open the Internet site... Operation aborted
8
2338
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 (*s1 == *s2) {
0
8760
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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
9280
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...
0
9139
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8140
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6719
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
6015
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2162
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.