求一个有服务端与客户端的c#应用程序源码
//server。csusing System;using System。Text;using System。Net;using System。Net。Sockets;using System。 Threading;using System。IO;namespace servers{ class servers { static void Main(string[] args) { try { //把ip地址转换为IPAddress实例 IPAd...全部
//server。csusing System;using System。Text;using System。Net;using System。Net。Sockets;using System。
Threading;using System。IO;namespace servers{ class servers { static void Main(string[] args) { try { //把ip地址转换为IPAddress实例 IPAddress ipAd = IPAddress。
Parse("192。168。1。107"); //初始化监听,端口为8001 TcpListener myList = new TcpListener(ipAd, 8002); //开始监听服务器端口 myList。
Start(); Console。WriteLine("启动端口服务。。。"); Console。WriteLine("本地节点为:" myList。
LocalEndpoint); Console。WriteLine("等待连接。。。"); //等待处理接入连接请求 Socket s = myList。
AcceptSocket(); NetworkStream ns = new NetworkStream(s); StreamReader sr = new StreamReader(ns); StreamWriter sw = new StreamWriter(ns); //新建立的连接用套接字s表示 Console。
WriteLine("连接来自 " s。RemoteEndPoint " 已上线。"); //Console。WriteLine("输入###结束"); while (true) { Console。
Write("对方说:"); string sentence = sr。ReadLine(); if (sentence == "###") break; Console。
WriteLine(sentence); //////接收客户信息 ////byte[] b = new byte[100]; ////int k = s。
Receive(b); ////Console。Write("对方说:"); ////for (int i = 0; i ////{ //// Console。
Write(Convert。ToChar(b[i])); ////} Console。WriteLine(); Thread。
Sleep(1000);//停1秒 Console。Write("你说:"); //处理客户端请求,给客户端回应 //ASCIIEncoding asen = new ASCIIEncoding(); string str = Console。
ReadLine(); sw。WriteLine(str); sw。Flush(); } //s。
Send(asen。GetBytes(str)); //Console。WriteLine("
已发送回应信息"); //释放资源,结束监听 sr。
Close(); sw。Close(); ns。Close(); s。Close(); myList。
Stop(); } catch (Exception e) { Console。WriteLine("错误。
。。" e。StackTrace); } } }}//客户端using System;using System。Text;using System。
Net;using System。Net。Sockets;using System。IO;namespace client{ class client { static void Main(string[] args) { try { //建立客户端套接字 TcpClient tcpclnt = new TcpClient(); Console。
WriteLine("连接。。。"); //连接服务器 tcpclnt。Connect("192。168。1。107", 8002); //得到客户端的流 NetworkStream ns = tcpclnt。
GetStream(); StreamReader sr = new StreamReader(ns); StreamWriter sw = new StreamWriter(ns); //Stream stm = tcpclnt。
GetStream(); Console。WriteLine("已连接"); //Console。WriteLine("输入###结束"); while(true) { Console。
Write("你说:"); string str = Console。ReadLine();//输入说话内容,如义乌网站制作 if (str == "###") break; //发送字符串 //ASCIIEncoding ascen = new ASCIIEncoding(); //byte[] ba = ascen。
GetBytes(str); Console。WriteLine("传输中。。。"); //stm。Write(ba, 0, ba。
Length); sw。WriteLine(str); sw。Flush(); Console。
Write("对方说"); str=sr。ReadLine(); Console。WriteLine(str); } ////接收从服务器返回的信息 //byte[] bb = new byte[100]; //int k = stm。
Read(bb, 0, 100); ////输出服务器端返回的信息 //Console。Write("对方说"); //for (int i = 0; i //{ // Console。
Write(Convert。ToChar(bb[i])); //} sr。Close(); sw。Close(); ns。
Close(); tcpclnt。Close(); } catch (Exception e) { Console。
WriteLine("错误。。。" e。StackTrace); } } }}。收起