一个最简单的c# Socket 服务器端实例 异步

2018-12-04 16:57

1.简单服务器端 /*

using System.Data;

using System.Net.Sockets; using System.Net;

using System.Threading;

private static int port = %%2;

private static Thread thThreadRead; private static TcpListener TcpListen; private static bool bListener = true; private static Socket stRead; */

private static void Listen() { try

{

TcpListen = new TcpListener(port); TcpListen.Start();

stRead = TcpListen.AcceptSocket();

EndPoint tempRemoteEP = stRead.RemoteEndPoint;

IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;

IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address); string sHostName = host.HostName; while (bListener)

{

stRead.Send(Encoding.ASCII.GetBytes(%%1));

string sTime = DateTime.Now.ToShortTimeString(); Byte[] byRead = new Byte[1024];

int iRead = stRead.ReceiveFrom(byRead, ref tempRemoteEP); Byte[] byText = new Byte[iRead];

Array.Copy(byRead, 0, byText, 0, iRead);

string line = System.Text.Encoding.Default.GetString(byRead); }

}

catch (System.Security.SecurityException) {

//监听失败 }

}

thThreadRead = new Thread(new ThreadStart(Listen)); thThreadRead.Start();

2.简单客户端

/*

using System.Data;

using System.Net.Sockets; using System.Net;

private static IPEndPoint dateTimeHost; */

string hostIPString=%%1;

string hostPortString=%%2;

IPAddress hostIP=IPAddress.Parse(hostIPString);

dateTimeHost=new IPEndPoint(hostIP,Int32.Parse(hostPortString));

Socket conn=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); conn.Connect(dateTimeHost); int bytes=0;

Byte[] RecvBytes=new Byte[256];

bytes=conn.Receive(RecvBytes,RecvBytes.Length,0);

string RecvString=Encoding.ASCII.GetString(RecvBytes,0,bytes); Console.WriteLine(RecvString);

conn.Shutdown(SocketShutdown.Both); conn.Close();

3.获得本机IP

//using System.Net;

IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; string %%1=null;

for (int i = 0; i < addressList.Length; i++) {

%%1 += addressList[i].ToString(); }

5.点对点通信 /*

using System.Data; using System.Net.Sockets; using System.Net; using System.Threading; */ Thread th;

TcpListener tpListen1; bool listenerRun=true; NetworkStream tcpStream; StreamWriter reqStreamW; TcpClient tcpc; Socket skSocket;

protected void Listen()

{

try{

tpListen1=new TcpListener(Int32.Parse(%%2)); tpListen1.Start();

skSocket=tpListen1.AcceptSocket();

EndPoint tempRemoteEP=skSocket.RemoteEndPoint; IPEndPoint tempRemoteIP=(IPEndPoint)tempRemoteEP;

IPHostEntry host=Dns.GetHostByAddress(tempRemoteIP.Address); string HostName=host.HostName; while(listenerRun) {

Byte[] stream=new Byte[1024];

string time=DateTime.Now.ToString();

int i=skSocket.ReceiveFrom(stream,ref tempRemoteEP); string %%5=Encoding.UTF8.GetString(stream); //指定编码,从缓冲区中解析出内容 //time+\ } }

catch(Security.SecurityException) {

//防火墙安全错误! } try{

string sMsg=%%4;

string MyName=Dns.GetHostName();

reqStreamW=new StreamWriter(tcpStream); reqStreamW.Write(sMsg); reqStreamW.Flush();

string time=DateTime.Now.ToString(); //显示传送的数据和时间 //time+\ //sMsg }

catch(Exception) {

//无法发送信息到目标计算机! }

protected override void Dispose(bool disposing) {

try{

listenerRun=false; th.Abort();

th=null; tpListen1.Stop(); skSocket.Close(); tcpc.Close(); }

catch{}

if(disposing && component!=null) {

components.Dispose(); } }

base.Dispose(disposing); }

16.聊天室服务器端逻辑 using System.Data;

using System.Net.Sockets; using System.Net;

using System.Threading; using System.Collections; class Server {

private static List li = new ArrayList(); private static TcpListener TcpListen; private static Socket stRead; public Server(int port) { try

{

TcpListen = new TcpListener(port); TcpListen.Start(); }

catch (Exception) { } }

public List Li() { get{ return li; }; set{ li=value; };

}

public void start() {

while (true) {

Socket s; try {

stRead = TcpListen.AcceptSocket(); li.Add(TcpListen); new Service(this, s).start(); }

catch (Exception e) { } }

}

static void main(string[] args) {

new Server(8888).start(); } }

class Service {

private static Socket stRead;

private static bool bListener = true; private static TcpListener TcpListen;

public Service(Server server, Socket client) {

TcpClient tcpc; Socket stSend = null; bool tcpConnect = false; try

{

stSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); EndPoint tempRemoteEP = stSend.RemoteEndPoint;

IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP; EndPoint epTemp = (EndPoint)tempRemoteIP; stSend.Connect(epTemp); tcpConnect = true; }

catch (Exception)

{

//MessageBox.Show(\目标计算机拒绝了连接请求!\ }

finally {


一个最简单的c# Socket 服务器端实例 异步.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:一、学生掌握知识情况:(略)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: