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();
}
}