Click here to Skip to main content
15,887,964 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I created Client(Windows) and Server(Console...and trying to make connection between these two and send message from client to server.

The following is the Error:
C#
InvalidOperationException was unhandled.
An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll


What I have tried:

Server:
C#
static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8281);
            int requestCount = 0;
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();
            Console.WriteLine(" >> Server Started");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" >> Accept connection from client");
            requestCount = 0;
            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    byte[] bytesFrom = new byte[10025];
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine(" >> Data from client - " + dataFromClient);
                    string serverResponse = "Last Message from client" + dataFromClient;
                    Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();
                    Console.WriteLine(" >> " + serverResponse);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> exit");
            Console.ReadLine();

        }

Client:
C#
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            msg("Client Started");
            clientSocket.Connect("127.0.0.1", 8281);
            label1.Text = "Client Socket Program - Server Connected ...";
        }


        public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[10025];
            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            msg(returndata);
            textBox2.Text = "";
            textBox2.Focus();
        }
}
Posted
Updated 29-Apr-16 1:44am
v4
Comments
Sergey Alexandrovich Kryukov 28-Apr-16 15:11pm    
In what line?
Will you format the code properly?

1) Don't mix UI and communication code, 2) don't mix it all in UI thread, 3) have at least 2 communication threads on server side: one for listening for connections, another for I/O. I did not mean I can see your bugs, by why not starting from doing right thing?

—SA

1 solution

There are some good "getting-started" examples on MSDN.
Socket Code Examples[^]

It is better that you start looking there and kind of forget about the code you have written so far.
Especially look in the the Asynchronous Server Socket Example.
(This is what Sergey is talking about)
The client can be either or, depending on your needs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900