Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:46:52+00:00 2026-06-02T21:46:52+00:00

I want to test my server, so i execute N clients in my computer.

  • 0

I want to test my server, so i execute N clients in my computer. The problem is that the first client works but the others’ connections were lost and their sockets were closed immediately!!!!!! Any idea how can I resolve this problem???

this is my code:

Server:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using System.Diagnostics;
namespace server
{
    public partial class server : Form
    {
        public static byte[] data;
        public static byte[] data1;
        public static Socket sock;
        public delegate void operation(string s);
        public delegate void operation2();
        public delegate bool verifier();
        public server()
        {InitializeComponent();
          this.Show();
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress adress = IPAddress.Parse("127.0.0.1");
            IPEndPoint iep = new IPEndPoint(adress, 4000);
            EndPoint ep = (EndPoint)iep;
            sock.Bind(iep);
            sock.Listen(1000);
            sock = sock.Accept();
            Thread lis = new Thread(listenning);
            lis.Start();
          }
        public void listenning()
        {
            data1 = new byte[1024];
            data = new byte[1024];
        repeter: 
            while (sock.Receive(data) > 0)
            {
                 String s = ASCIIEncoding.ASCII.GetString(data);
                if (this.InvokeRequired) Invoke((operation)effectuer4, s);
                else effectuer4(s);
                goto repeter;
              }
        }
        private void effectuer(String s)
        {
            textBox1.Text += "serveur:  " + s + "\r\n";
              message.Text  = "";
        }
        private void effectuer4(String s)
        {
            textBox1.Text += "Client:  " + s + "\r\n"; message.Text = "";
        }
      private void buttonDisconnect_Click(object sender, EventArgs e)
        {
            sock.Close();
            Application.Exit();
             }
    private void buttonSend_Click(object sender, EventArgs e)
        {
           String s = message.Text ;
            data1 = System.Text.Encoding.ASCII.GetBytes(s);
              sock.Send(data1);Invoke((operation)effectuer, s);
           }
        }
     }

Client:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.Xml;
namespace client
{
    public partial class Form1 : Form
    {
        public static TcpClient SocketPourClient = null;
        public static string ClientMessage;
        public static string ServerMessage;
        Socket sock;
        public static byte[] data;
        public static byte[] data1;
        public delegate void operation(String s);
        public delegate void lancer();
        public delegate bool verifier();
        public IPEndPoint ipEnd = null;
        public int Num = 1;
        public Form1(string ip, int port)
        {
            InitializeComponent();
                   IPAddress adress = IPAddress.Parse("127.0.0.1");
                     ipEnd = new IPEndPoint(adress, 4000);
                  sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                  sock.Connect(ipEnd);
                  Thread th = new Thread(listenning);
                  th.Start();
        }
        public void listenning()
        {
            try
            {

                data = new byte[1024];

             repeter: 
            if (sock.Receive(data) > 0)
            {

                String s = ASCIIEncoding.ASCII.GetString(data);
                if (this.InvokeRequired) Invoke((operation)effectuer4, s);
                else effectuer4(s);
                goto repeter;

            }
            }
            catch (SocketException e)
            {
                MessageBox.Show(e.ToString());
                sock.Close();
            }

        }


  private void effectuer(String s)
    {
        textBox1.Text += "client:  " + s + "\r\n";
      message.Text = "";
    }
   private void effectuer4(String s)
     {
         textBox1.Text += "Server:  " + s + "\r\n";
         message.Text = "";
       }
 private void buttonDisconnect_Click(object sender, EventArgs e)
    {
        sock.Close();
        Application.Exit();
    }
private void buttonSend_Click_1(object sender, EventArgs e)
    {
        String s = message.Text ;
        data1 = System.Text.Encoding.ASCII.GetBytes(s);
        sock.Send(data1);
        Invoke((operation)effectuer, s);
      }
    }
}

Any idea how I can execute N clients in the same machine with the same socket?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:46:53+00:00Added an answer on June 2, 2026 at 9:46 pm

    In server code you call sock = sock.Accept(); only once. Move this code to a thread and surround it with while(true). You may also want to process the client’s request in other threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use prosody or maybe another xmpp server to test my xmpp
I want to test my part of code that returns the users password question.
Problem I want to send https request to the site https://10.2.20.20/fido/EzPay/login.php my own server
I want test a FIX gateway for our company and was wondering if anything
I want to test whether a certain string is contained in a short list
I want to test my app for my app launched with a URL scheme
I want to test the REST api on my Rails site. What is the
I want to test a Ajax based web application. I want to write the
I want to test whether two languages have a string in common. Both of
I want to test my application (especially SQL statements) against different databases. Actually I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.