弄个简单的客户端将网络端点
软件学院表示为ip地址和端口号IPEndPointpoint;try{point=newIPEndPoint(HostIP,Int32.Parse("1571"));//AddressFamily指定Socket的寻址方式//指定Socket的协议类型ProtocolType.Tcpsocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);socket.Connect(point);//向服务器发送数据byte[]sendByte=newbyte[64];stringsendStr=this.textBox1.Text+"\r\n";sendByte=Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());socket.Send(sendByte,sendByte.Length,0);}catch(Exceptionex){MessageBox.Show(ex.Message);}}}}CodeusingSystem;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;usingSystem.Windows.Forms;namespaceWindowsApplication1{publicpartialclassnet_client:Form{publicnet_client(){InitializeComponent();}privatevoidbtn_send_Click(objectsender,EventArgse){Socketsocket;//提供网际协议的ip地址IPAddressHostIP=IPAddress.Parse("127.0.0.1");//
软件工程将网络端点表示为IP地址和端口号IPEndPointpoint;//定义两个套接字Socketsocket;SocketacceptedSocket;//开启接受套接字的服务privatevoidbutton1_Click(objectsender,EventArgse){try{point=newIPEndPoint(HostIP,Int32.Parse("1571"));socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);socket.Bind(point);socket.Listen(50);//acceptedSocket=socket.Accept();Threadthread=newThread(newThreadStart(Process));thread.Start();}catch(Exceptioney){MessageBox.Show(ey.Message);}}//要专门用一个线程来accept一个线程来receive数据privatevoidProcess(){acceptedSocket=socket.Accept();if(acceptedSocket.Connected){byte[]receiveByte=newbyte[64];acceptedSocket.Receive(receiveByte,receiveByte.Length,0);stringstrInfo=Encoding.BigEndianUnicode.GetString(receiveByte);//在
重庆足下软件教育控件的基础窗口句柄的线程上,用指定的参数列表执行指定的委托this.Invoke(newSetTextCallback(SetText),newobject[]{strInfo});}else{this.textBox1.Text="还没有信息";}}}}T服务器端的代码CodeusingSystem;usingSystem.Text;usingSystem.Net;usingSystem.Threading;usingSystem.Diagnostics;usingSystem.Net.Sockets;namespaceWindowsApplication1{publicpartialclassnet_server:Form{publicnet_server(){InitializeComponent();}//声明委托delegatevoidSetTextCallback(stringtext);privatevoidSetText(stringtext){textBox1.AppendText(text+"\r\n");}//提供网际协议(IP)地址IPAddressIPAddressHostIP=IPAddress.Parse("127.0.0.1");//
posted on 2010-05-25 17:09
王晓玉 阅读(84)
评论(0) 编辑 收藏 引用