471,325 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 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 19620
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Mehta Shailendrakumar | last post: by
1 post views Thread by Joško Šugar | last post: by
2 posts views Thread by Robinson | last post: by
8 posts views Thread by Steven | last post: by
reply views Thread by rosydwin | last post: by

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.