• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

socket网络编程问题

服务端:
using System;using System.IO;using System.Collections;using System.Collections.Generic;using System.Configuration;using System.Data.SqlClient;using System.Text.RegularExpressions;using MyLibrary;using System.ComponentModel;using System.Text;using System.Security.Cryptography;using Microsoft.International.Converters.PinYinConverter;using System.Reflection;using System.Net;using System.Net.Sockets;using System.Threading;namespace first_project{           static class Program    {                static Dictionary clientList = new Dictionary();        static void Main(string[] args)        {                        Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            serverSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 99));            Console.WriteLine("listening...");            serverSocket.Listen(10);            while (true)            {                Socket socketCommu = serverSocket.Accept();                clientList.Add(socketCommu.RemoteEndPoint.ToString().Split(':')[1], socketCommu);                Console.WriteLine(socketCommu.RemoteEndPoint + ": connected");                Thread threadReceive = new Thread(Receive);                threadReceive.IsBackground = true;                threadReceive.Start(socketCommu);                Thread threadSend = new Thread(Send);                threadSend.IsBackground = true;                threadSend.Start();            }        }                        static void Receive(object obj)        {                                        byte[] buffer;                Socket socketCommu = obj as Socket;                        while (true)                        {                                buffer = new byte[3 * 1024 * 1024];                                int r = socketCommu.Receive(buffer);                                if(r == 0) break;                                Console.WriteLine(socketCommu.RemoteEndPoint + ": " + Encoding.UTF8.GetString(buffer, 0, r));                        }        }                                static void Send()        {                        Console.WriteLine("input client port:");            string port = Console.ReadLine();            Socket socketCommu = clientList[port];            while (true)                socketCommu.Send(Encoding.UTF8.GetBytes(Console.ReadLine()));        }                                            }}客户端:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;using System.Threading;namespace client{    class Program    {        static Socket clientSocket;               static void Main(string[] args)        {                       clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            clientSocket.Connect(IPAddress.Parse("127.0.0.1"), 99);            Console.WriteLine("server connected");                                    Thread th = new Thread(Send);            th.IsBackground = true;            th.Start();                                    byte[] buffer;            while(true)           {                                buffer = new byte[3 * 1024 * 1024];                                int r = clientSocket.Receive(buffer);                                if(r == 0) break;                                Console.WriteLine(clientSocket.RemoteEndPoint +": "+Encoding.UTF8.GetString(buffer, 0, r));           }                                }        static void Send()        {            while(true)                 clientSocket.Send(Encoding.UTF8.GetBytes(Console.ReadLine()));        }                    }}为什么输入已经连接的客户端端口抛异常提示字典clientList的键不存在?

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!