我想用C#写个局域网聊天的程序,来这里求些学习资料。 我想做成像QQ 聊天那样的程序,但对C#网络编程还不太懂,哪位朋友能提一些建议或提供一些学习资料?谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 带加密的Chatusing System;using System.Net;using System.Net.Sockets;using System.Threading;using System.Text;using System.IO;using System.Security.Cryptography;namespace Chat{ class ChatApp { private static IPAddress rempteIPAddress; private static int remotePort; private static int localPort; private static UTF8Encoding Utf8Encod = new UTF8Encoding(); private static string CryptoKey = "!~6oxli@]t2K'yd$"; private static string CryptoIV = "!~6oxli@]t2K'yd$"; static void Main(string[] args) { try { //Get necessary data for connection //Console.WriteLine("Enter Local Port"); //localPort = Convert.ToInt16(Console.ReadLine()); localPort = 10001; //Console.WriteLine("Enter Remote Port"); //remotePort = Convert.ToInt16(Console.ReadLine()); remotePort = 10005; //Console.WriteLine("Enter Remote IP Address"); //rempteIPAddress = IPAddress.Parse(Console.ReadLine()); rempteIPAddress = IPAddress.Parse("127.0.0.1"); //Create thread for listening Thread tRec = new Thread(new ThreadStart(Receive)); tRec.Start(); //byte[] bytes = encryptData("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); //Console.WriteLine("-----"+bytes.Length.ToString()); //bytes = null; //bytes = UTF8Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); //Console.WriteLine("======" + bytes.Length.ToString()); //Console.ReadLine(); while (true) { Send(Console.ReadLine()); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } private static void Send(string datagram) { //Create UdpClient UdpClient sender = new UdpClient(); //Create IPEndPoint with details of remote host IPEndPoint endPoint = new IPEndPoint(rempteIPAddress, remotePort); try { //byte[] bytes = encryptData(datagram); byte[] bytes = Encoding.ASCII.GetBytes(datagram); sender.Send(bytes, bytes.Length, endPoint); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { sender.Close(); } } private static void Receive() { //Create a UdpClient for reading incoming data. UdpClient receivingUdpClient = new UdpClient(localPort); IPEndPoint RemoteIpEndPoint = null; try { Console.WriteLine("------------------Ready for chat!!!------------------"); while (true) { //wait for datagram byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); //Convert and display data string returnData = Encoding.ASCII.GetString(receiveBytes); Console.WriteLine("-"+returnData.ToString()); //returnData = decryptData(receiveBytes); Console.WriteLine("-"+returnData.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } static byte[] encryptData(string theDataGram) { byte[] bCipherText = null; try { //Create the Rijndael Service Provider object and assign the key and vector to it RijndaelManaged RijndaelProvider = new RijndaelManaged(); //Console.WriteLine(RijndaelProvider.BlockSize.ToString() +"/"+ RijndaelProvider.Key.Length + "/" + RijndaelProvider.IV.Length); //Console.WriteLine(UTF8Encoding.ASCII.GetBytes(CryptoIV).Length.ToString()); RijndaelProvider.Key = Utf8Encod.GetBytes(CryptoKey); //Utf8Encod = new UTF8Encoding(); RijndaelProvider.IV = Utf8Encod.GetBytes(CryptoIV); ICryptoTransform RijndaelEncrypt = RijndaelProvider.CreateEncryptor(); byte[] bClearText = Utf8Encod.GetBytes(theDataGram); MemoryStream Mstm = new MemoryStream(); //Create Crypto Stram that transforms a stream using the encryption CryptoStream Cstm = new CryptoStream(Mstm, RijndaelEncrypt, CryptoStreamMode.Write); //Write out encrypted content into MemoryStream Cstm.Write(bClearText, 0, bClearText.Length); Cstm.FlushFinalBlock(); //Get the output bCipherText = Mstm.ToArray(); Cstm.Close(); Mstm.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return bCipherText; } static string decryptData(byte[] bCipherText) { string sEncoded = ""; try { //Create the RijndaelManaged Service Provider object and assign the key and vector to it RijndaelManaged RijndaelProvider = new RijndaelManaged(); RijndaelProvider.Key = Utf8Encod.GetBytes(CryptoKey); RijndaelProvider.IV = Utf8Encod.GetBytes(CryptoIV); ICryptoTransform RijndaelDecrypt = RijndaelProvider.CreateDecryptor(); MemoryStream Mstm = new MemoryStream(bCipherText, 0, bCipherText.Length); //Create Crypto Stram that transforms a stream using the encryption CryptoStream Cstm = new CryptoStream(Mstm, RijndaelDecrypt, CryptoStreamMode.Read); //Read out the result from the Crypto Stream StreamReader Sr = new StreamReader(Cstm); sEncoded = Sr.ReadToEnd(); Sr.Close(); Cstm.Close(); Mstm.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return sEncoded; } }} 三种方案1.dot net remoting;2.web severce3.socket 如何操作内嵌类的反射? 网络映射驱动器 正则表达式 急 求ASP.NTE 无刷新问题 AJAX 聊天室 正则表达式替换的问题。急,争取今天揭帖 正则表达式问题 求助:怎么使WEB页面总是置顶显示? 关于C#的集合类 为什么IMediaControl播放声音文件没有声音? C#中被引用的类如何知道当前引用自身的类? 再发一贴,为什么别人能实现的效果我实现不了?(解决立刻给分) dataGridView 控件真难用,第一个空白固定列去不掉,选择一行骤焦后当焦点移到 Textbox 文本时,那选中的一行还是有蓝色背景
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.IO;
using System.Security.Cryptography;namespace Chat
{
class ChatApp
{
private static IPAddress rempteIPAddress;
private static int remotePort;
private static int localPort;
private static UTF8Encoding Utf8Encod = new UTF8Encoding();
private static string CryptoKey = "!~6oxli@]t2K'yd$";
private static string CryptoIV = "!~6oxli@]t2K'yd$"; static void Main(string[] args)
{
try
{
//Get necessary data for connection
//Console.WriteLine("Enter Local Port");
//localPort = Convert.ToInt16(Console.ReadLine());
localPort = 10001; //Console.WriteLine("Enter Remote Port");
//remotePort = Convert.ToInt16(Console.ReadLine());
remotePort = 10005;
//Console.WriteLine("Enter Remote IP Address");
//rempteIPAddress = IPAddress.Parse(Console.ReadLine()); rempteIPAddress = IPAddress.Parse("127.0.0.1");
//Create thread for listening
Thread tRec = new Thread(new ThreadStart(Receive));
tRec.Start(); //byte[] bytes = encryptData("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
//Console.WriteLine("-----"+bytes.Length.ToString());
//bytes = null;
//bytes = UTF8Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
//Console.WriteLine("======" + bytes.Length.ToString()); //Console.ReadLine();
while (true)
{
Send(Console.ReadLine());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} private static void Send(string datagram)
{
//Create UdpClient
UdpClient sender = new UdpClient();
//Create IPEndPoint with details of remote host
IPEndPoint endPoint = new IPEndPoint(rempteIPAddress, remotePort); try
{
//byte[] bytes = encryptData(datagram); byte[] bytes = Encoding.ASCII.GetBytes(datagram);
sender.Send(bytes, bytes.Length, endPoint);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
sender.Close();
}
} private static void Receive()
{
//Create a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient(localPort);
IPEndPoint RemoteIpEndPoint = null;
try
{
Console.WriteLine("------------------Ready for chat!!!------------------");
while (true)
{
//wait for datagram
byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
//Convert and display data
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("-"+returnData.ToString());
//returnData = decryptData(receiveBytes);
Console.WriteLine("-"+returnData.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} static byte[] encryptData(string theDataGram)
{
byte[] bCipherText = null;
try
{
//Create the Rijndael Service Provider object and assign the key and vector to it
RijndaelManaged RijndaelProvider = new RijndaelManaged();
//Console.WriteLine(RijndaelProvider.BlockSize.ToString() +"/"+ RijndaelProvider.Key.Length + "/" + RijndaelProvider.IV.Length); //Console.WriteLine(UTF8Encoding.ASCII.GetBytes(CryptoIV).Length.ToString());
RijndaelProvider.Key = Utf8Encod.GetBytes(CryptoKey);
//Utf8Encod = new UTF8Encoding();
RijndaelProvider.IV = Utf8Encod.GetBytes(CryptoIV); ICryptoTransform RijndaelEncrypt = RijndaelProvider.CreateEncryptor();
byte[] bClearText = Utf8Encod.GetBytes(theDataGram);
MemoryStream Mstm = new MemoryStream();
//Create Crypto Stram that transforms a stream using the encryption
CryptoStream Cstm = new CryptoStream(Mstm, RijndaelEncrypt, CryptoStreamMode.Write); //Write out encrypted content into MemoryStream
Cstm.Write(bClearText, 0, bClearText.Length);
Cstm.FlushFinalBlock(); //Get the output
bCipherText = Mstm.ToArray();
Cstm.Close();
Mstm.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return bCipherText;
} static string decryptData(byte[] bCipherText)
{
string sEncoded = "";
try
{
//Create the RijndaelManaged Service Provider object and assign the key and vector to it
RijndaelManaged RijndaelProvider = new RijndaelManaged();
RijndaelProvider.Key = Utf8Encod.GetBytes(CryptoKey);
RijndaelProvider.IV = Utf8Encod.GetBytes(CryptoIV); ICryptoTransform RijndaelDecrypt = RijndaelProvider.CreateDecryptor();
MemoryStream Mstm = new MemoryStream(bCipherText, 0, bCipherText.Length);
//Create Crypto Stram that transforms a stream using the encryption
CryptoStream Cstm = new CryptoStream(Mstm, RijndaelDecrypt, CryptoStreamMode.Read); //Read out the result from the Crypto Stream
StreamReader Sr = new StreamReader(Cstm);
sEncoded = Sr.ReadToEnd(); Sr.Close();
Cstm.Close();
Mstm.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return sEncoded;
}
}
}
1.dot net remoting;
2.web severce
3.socket