(转)WinForm实现在服务器端创建文件夹

来源:互联网 发布:最好的数据统计软件 编辑:程序博客网 时间:2024/05/16 13:00
//client端usingSystem;usingSystem.Text;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;namespacesocketsample{  classClass1  {   staticvoidMain()   {    try    {     intport=2000;     stringhost="127.0.0.1";     IPAddressip=IPAddress.Parse(host);     IPEndPointipe=newIPEndPoint(ip,port);//把ip和端口转化为IPEndPoint实例     Socketc=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建一个Socket     Console.WriteLine("Conneting...");     c.Connect(ipe);//连接到服务器     stringsendStr="hello!Thisisasockettest";     byte[]bs=Encoding.ASCII.GetBytes(sendStr);     Console.WriteLine("SendMessage");     c.Send(bs,bs.Length,0);//发送测试信息     stringrecvStr="";     byte[]recvBytes=newbyte[1024];     intbytes;     bytes=c.Receive(recvBytes,recvBytes.Length,0);//从服务器端接受返回信息     recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes);     Console.WriteLine("ClientGetMessage:{0}",recvStr);//显示服务器返回信息     c.Close();    }    catch(ArgumentNullExceptione)    {     Console.WriteLine("ArgumentNullException:{0}",e);    }    catch(SocketExceptione)    {     Console.WriteLine("SocketException:{0}",e);    }    Console.WriteLine("PressEntertoExit");    Console.ReadLine();   }  }}//server端usingSystem;usingSystem.Text;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;namespaceProject1{  classClass2  {   staticvoidMain()   {    try    {     intport=2000;     stringhost="127.0.0.1";     IPAddressip=IPAddress.Parse(host);     IPEndPointipe=newIPEndPoint(ip,port);     Sockets=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建一个Socket类     s.Bind(ipe);//绑定2000端口     s.Listen(0);//开始监听     Console.WriteLine("Waitforconnect");     Sockettemp=s.Accept();//为新建连接创建新的Socket。     Console.WriteLine("Getaconnect");     stringrecvStr="";     byte[]recvBytes=newbyte[1024];     intbytes;     bytes=temp.Receive(recvBytes,recvBytes.Length,0);//从客户端接受信息     recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes);     Console.WriteLine("ServerGetMessage:{0}",recvStr);//把客户端传来的信息显示出来     stringsendStr="Ok!ClientSendMessageSucessful!";     byte[]bs=Encoding.ASCII.GetBytes(sendStr);     temp.Send(bs,bs.Length,0);//返回客户端成功信息     temp.Close();     s.Close();    }    catch(ArgumentNullExceptione)    {     Console.WriteLine("ArgumentNullException:{0}",e);    }    catch(SocketExceptione)    {     Console.WriteLine("SocketException:{0}",e);    }    Console.WriteLine("PressEntertoExit");    Console.ReadLine();   }  }}
0 0