日期:2014-05-17  浏览次数:20833 次

求助C#大牛!!在 socket通信中,多客户端与一个服务器端异步通信时,服务器监听客户端套接字后,如何分别开启一个线程去进行数据传送??
小弟新人,初来报道,求各位大神指导~~

最近在做一个课设,是有关C#中socket异步通信的,要求两台以上客户端向服务器端请求传送图片和视频信息,服务器端在接到请求后分别向每个客户端发送它们需要的信息,在调试过程中,发现一对一的话可以实现基本功能,但只要多客户端的话,另一台倒是显示“已连接”,但请求后得不到回复。故猜测:是在服务器端监听并产生新线程这段代码出问题,没有把两个服务器请求区分开,即没有产生新的套接字去建立通信连接。

但只是个人臆测,还望c#大牛指导~~
现贴上我们主机端和客户端代码,请各位大牛们不吝赐教~~

server端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using Shell32;
using AviFile;

namespace media

    public partial class Show : Form
    {
        public static ManualResetEvent allDone = new ManualResetEvent(false);
        Thread serverThread;
        Thread clientThread;
        Socket serverSocket;
        Socket clientSocket;
        
        public Show()
        {
            InitializeComponent();
        }


        private Thread threadWatch = null;//负责监听客户端连接请求的线程
        
     
        private Socket socketWatch = null;//负责监听的套接字
       
        private void ServerStart()
        {
            IPEndPoint localipep = new IPEndPoint(IPAddress.Any, 6000);
            
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //将所创建的套接字与IPEndPoint绑定
            serverSocket.Bind(localipep);