Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a small server which is receive and send the message back to the client.
this is the client side
when i open the client it will connect to server through Connect()
public Form1()
        {
            InitializeComponent();
            Connect();
            CheckForIllegalCrossThreadCalls = false;
        }

this is my connect
void Connect()
        {
            ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                server.Connect(ipep);
            }
            catch (SocketException e)
            {
                MessageBox.Show(Convert.ToString(e));           
            }
            Thread listen = new Thread(Receive);
            listen.IsBackground = true; ;
            listen.Start();
        }

and i have a receive like this
void Receive()
        {
            datarec = new byte[1024];
            try
            {
                while (true)
                {
                    string StringData;
                    rec = server.Receive(datarec);
                    StringData = Encoding.ASCII.GetString(data, 0, rec);
                    txtShow.Text = StringData;
                }
            }
            catch
            {
                Close();
            }
        }

and i send data through a button have Send method
void Send(string s)
        {
            data = new byte[1024];
            data = Encoding.ASCII.GetBytes(s);
            server.Send(data, data.Length, SocketFlags.None);
            
        }

this is the server side
i have a thread server
public static void Process(Socket client)
        {
            byte[] data = new byte[1024];
            int recv;
            string dataInput;
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);             
            
            while (true)
            {
                try
                {                   
                     recv = client.Receive(data);
                    dataInput = Encoding.ASCII.GetString(data, 0, recv);
                    Console.WriteLine(dataInput);
                    client.Send(data);
                }
                catch (SocketException e)
                {

                    Console.WriteLine(e);
                }
            }
        }

the server receive message but when it send message back it has an error "An existing connection was forcibly closed by the remote host"
Can you guys tell me where i was wrong and how can i fix it?
Sorry for missing the Send Button. Here is it
private void button1_Click(object sender, EventArgs e)
        {
            string s = txtText.Text;
            Send(s);          
        }


What I have tried:

I have searching this error for 2 days but still not understand how to fix it. Can you guys tell me or give me a source where i can find how to fix it ? thanks so much
Posted
Updated 11-Jun-20 23:45pm
v2

1 solution

Look at your code:
You are trying to call Recieve from two different threads:
Once when you set up the Socket:
void Connect()
        {
            ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
...
            Thread listen = new Thread(Receive);
            listen.IsBackground = true; ;
            listen.Start();
        }

And once every time you try to send data:
void Send(string s)
        {
            data = new byte[1024];
            data = Encoding.ASCII.GetBytes(s);
            server.Send(data, data.Length, SocketFlags.None);
            Receive();
        }
You don't show us where the client calls Send, but you can't have two "listeners" on the same Socket - the system wouldn't know where to process it.
So the chances are each time you call Send it throws an Exception, and you call Close to end the client.

And then the Server has no endpoint to respond to ... so the server fails as well.
 
Share this answer
 
Comments
Member 14851077 12-Jun-20 5:47am    
thanks for your comment. I updated it and delete the second Receive() too. But it still has new error "An established connection was aborted by the software in your host machine"
OriginalGriff 12-Jun-20 6:01am    
Well, it took you 2 days to not spot that, so I'd guess that you are doing something equally silly elsewhere in your code - so you need to go through the whole of your client and server software with a fine tooth comb and find out exactly what you are doing, and how it might be affecting you.

We can't do that - we don't have access to your code, just the fragments you paste here. And you wrote it, so you know what you expect it to do, we don't!
Member 14851077 12-Jun-20 6:11am    
sorry for your angry but the second Receive() is a mistake. I wrote it there for checking about the problem but cannot solve. Then i rewrite the first Receive() and forgot to delete the second one. Sorry for not checking before send it here
OriginalGriff 12-Jun-20 6:49am    
I'm not angry - heck, I'm not even annoyed! :laugh:

But I am serious: you need to check your code - all of it - and see what you are doing, and we can't do that for you!
Member 14851077 12-Jun-20 7:10am    
thanks for your advise, i'll remember to check my code before putting it on

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