Communication of two programs using sockets

Source: Internet
Author: User

It's very easy to write, and I think it's very interesting.

Program

Main code

 Public classMessage {Form1 mainfrom=NULL;  PublicMessage () {} PublicMessage (Form1 form) {Mainfrom=form; }         Public BOOLStartreceive (intPort) {            Try{IPEndPoint IEP=NewIPEndPoint (Ipaddress.loopback, Port); Socket TCPServer=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);                Tcpserver.bind (IEP); Tcpserver.listen ( -); Tcpserver.beginaccept (NewAsyncCallback (Accept), tcpserver); return true; }            Catch(Exception ex) {return false; } }        Private voidAccept (IAsyncResult ia) {socket Socket= ia. AsyncState asSocket; varClient =socket.            Endaccept (IA); byte[] buf =New byte[1024x768]; Socket. BeginAccept (NewAsyncCallback (Accept), socket); StateObject State=NewStateObject (); State.worksocket=client; Try{client. BeginReceive (State.buffer,0, Stateobject.buffersize, Socketflags.none,NewAsyncCallback (Receive), state); }            Catch(Exception ex) {//Console.WriteLine ("Error listening on request: \ r \ n" + ex.) ToString ());            }        }        Private voidReceive (IAsyncResult ia) {stateobject State= ia. AsyncState asStateObject; if(state = =NULL)            {                return; } Socket Client=State.worksocket; if(Client = =NULL)            {                return; }            Try            {                intCount =client.                EndReceive (IA); if(Count >0)                {                    Try{client. BeginReceive (State.buffer,0, Stateobject.buffersize, Socketflags.none,NewAsyncCallback (Receive), client); stringContext = Encoding.GetEncoding ("gb2312"). GetString (State.buffer,0, Count); //Show Receive MessageMainfrom.                    Loginformtextchange (context); }                    Catch(Exception ex) {//Console.WriteLine ("Error receiving data: \r\n{0}", ex. ToString ());                    }                }            }            Catch(Exception err) {}} Public voidSendMessage (intPortstringm) {System.Text.Encoding CharSet= System.Text.Encoding.GetEncoding ("gb2312"); Socket tcpClient=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Try{tcpclient.connect (ipaddress.loopback, Port); stringsendmsg =m; byte[] Buff =charset.getbytes (sendmsg); Tcpclient.send (buff, buff. Length,0); }            Catch(SocketException e) {}}}  Public classStateObject {//Client socket.         PublicSocket Worksocket =NULL; //Size of receive buffer.         Public Const intBufferSize =1024x768; //Receive buffer.         Public byte[] buffer =New byte[buffersize]; //Received data string.         PublicStringBuilder SB =NewStringBuilder (); }

SOURCE Http://yun.baidu.com/s/1i39XtjR

Communication of two programs using sockets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.