473,791 Members | 2,947 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 20858
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
46912
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
5901
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
26540
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
8201
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
2695
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
4856
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
4808
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
32249
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
6078
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
2340
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
9669
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
10426
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...
1
10154
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,...
0
9993
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...
1
7537
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
6776
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
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4109
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
2913
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.