求C#聊天室源代码
C#聊天室源代码,要求有后台源代码,急!!!不好意思,没多少分数,全给了邮箱cathykakaxi@163.com...
C#聊天室源代码,要求有后台源代码,急!!!不好意思,没多少分数,全给了
邮箱cathykakaxi@163.com 展开
邮箱cathykakaxi@163.com 展开
2个回答
展开全部
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace UdpServer
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Char
{
private static IPAddress remoteIPAddress;
private static int remotePort;
private static int localPort;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
//Get necessary data for connection
Console.WriteLine("Enter Local port");
localPort = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter remote port");
remotePort = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter remote IP Address");
remoteIPAddress = IPAddress.Parse(Console.ReadLine());
//Create thread for listening
Thread tRec = new Thread(new ThreadStart(Receiver));
tRec.Start();
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 detail of remote host
IPEndPoint endPoint = new IPEndPoint(remoteIPAddress,remotePort);
try
{
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 Receiver()
{
//Create a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient(localPort);
//IPEndPoint with remote host information
IPEndPoint RemoteIPEndPoint = null;
try
{
Console.WriteLine("---------------*********Ready for char!!!!*******------------");
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());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace UdpServer
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Char
{
private static IPAddress remoteIPAddress;
private static int remotePort;
private static int localPort;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
//Get necessary data for connection
Console.WriteLine("Enter Local port");
localPort = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter remote port");
remotePort = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter remote IP Address");
remoteIPAddress = IPAddress.Parse(Console.ReadLine());
//Create thread for listening
Thread tRec = new Thread(new ThreadStart(Receiver));
tRec.Start();
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 detail of remote host
IPEndPoint endPoint = new IPEndPoint(remoteIPAddress,remotePort);
try
{
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 Receiver()
{
//Create a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient(localPort);
//IPEndPoint with remote host information
IPEndPoint RemoteIPEndPoint = null;
try
{
Console.WriteLine("---------------*********Ready for char!!!!*******------------");
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());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询