Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用套接字测试服务器_C#_Sockets_Client - Fatal编程技术网

C# 使用套接字测试服务器

C# 使用套接字测试服务器,c#,sockets,client,C#,Sockets,Client,我想测试我的服务器,所以我在我的计算机上执行N个客户端。问题是,第一个客户端工作,但其他客户端的连接丢失,其套接字立即关闭!!!!!!你知道我该如何解决这个问题吗 这是我的代码: 服务器: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

我想测试我的服务器,所以我在我的计算机上执行N个客户端。问题是,第一个客户端工作,但其他客户端的连接丢失,其套接字立即关闭!!!!!!你知道我该如何解决这个问题吗

这是我的代码:

服务器:

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);
           }
        }
     }
客户:

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

知道我如何在同一台机器上用同一个套接字执行N个客户端吗?

在服务器代码中调用
sock=sock.Accept()仅一次。将此代码移动到线程并用
while(true)
将其环绕。您可能还希望在其他线程中处理客户端的请求。

在服务器代码中,您调用
sock=sock.Accept()仅一次。将此代码移动到线程并用
while(true)
将其环绕。您可能还希望在其他线程中处理客户端的请求。

是否使用
goto
s?我想我可能会哭。
goto
是从循环结束到循环开始的吗?是的,goto这样做,但不建议我可以替换它。您使用的是
goto
s吗?我想我可能会哭。
转到
是指从循环结束到循环开始?是的,转到这样做,但不建议我可以替换它。我这样做:线程li=新线程(接受);li.Start();线程lis=新线程(列表);lis.Start();}public void accepting(){while(!sock.Connected){sock=sock.Accept();}}}但是出现了一个异常:socket已关闭并且条件(while(sock.Receive(data)>0))有问题吗?我这样做了:线程li=新线程(接受);li.Start();线程lis=新线程(列表);lis.Start();}public void accepting(){while(!sock.Connected){sock=sock.Accept();}}}但出现异常:套接字已关闭,并且条件(while(sock.Receive(data)>0))存在问题