为了账号安全,请及时绑定邮箱和手机立即绑定

socket网络编程问题

socket网络编程问题

C#
qq_杀意隆_0 2018-03-17 10:51:49
服务端: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<string, Socket> clientList = new Dictionary<string, Socket>();         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的键不存在?
查看完整描述

目前暂无任何回答

  • 0 回答
  • 1 关注
  • 1312 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信