Communication between the B/S mode and the C/S Mode in C #-socket communication

Source: Internet
Author: User

I) Background:

In many systems, the B/S architecture and the C/S architecture are included. This involves the communication between B/S and C/S, I have also discussed this issue in csdn forums.

And you have discussed the master, the specific post: http://topic.csdn.net/u/20100326/09/dd9ee3ce-3d90-4583-8eaf-d8542d7eec72.html

In general, two solutions are provided:

1) communicate through WebService. 2) communication through WCF

However, I do not know much about WebService and WCF, So I considered another method and used socket for communication.

Ii) server-side test code:

Private bool islistening = false; <br/> private void button#click (Object sender, eventargs e) <br/>{< br/> If (! Islistening) <br/>{< br/> // create a TCP/IP socket for listening to client connections <br/> socket listensocket = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. <br/> int Port = 8000; <br/> string host = "192.168.1.222 "; // ip address used for network connection <br/> // string host = "127.0.0.1"; // used when the network is not connected <br/> iphostentry entry = DNS. gethostentry (host); <br/> IPaddress IP = entry. addresslist [0]; <br/> /// first bind the host and port to be listened on, so that The socket is associated with a local endpoint. <Br/> listensocket. BIND (New ipendpoint (IP, Port); <br/> // start listening and specify the maximum length of the listening queue <br/> listensocket. listen (10); <br/> islistening = true; <br/> // The thread is blocked until a new client connection exists. <br/> listensocket. beginaccept (New asynccallback (this. onacceptsocket), null); <br/> addmsg ("Start listening... "); <br/> // generate the main thread and start the main thread <br/> thread tmain = new thread (New threadstart (testmainrepart); <br/> tmain. start (); <br/>}< br/> else <br/> {<B R/> MessageBox. Show ("the service is enabled! "); <Br/>}</P> <p>}

 

Private void onacceptsocket (iasyncresult AR) <br/>{< br/> If (! Islistening) <br/> return; <br/> socket S = listensocket. endaccept (AR); <br/> mysocket socket = new mysocket (); <br/> socket. socket = s; <br/> socket. isconnected = true; <br/> socket. socket. beginreceive (socket. buffer, 0, mysocket. bufsize, socketflags. none, new asynccallback (this. onreceivedata), socket); <br/> listensocket. beginaccept (New asynccallback (this. onacceptsocket), null); <br/>}

 

 

Private void onreceivedata (iasyncresult AR) <br/>{< br/> mysocket socket = ar. asyncstate as mysocket; <br/> If (socket = NULL |! Socket. isconnected) <br/> return; <br/> int length = socket. socket. endreceive (AR); <br/> // MSG indicates the accepted data. <br/> string MSG = encoding. utf8.getstring (socket. buffer, 0, length); <br/> // send the received data to the client. <br/> socket. send (MSG); <br/>}

 

Public class mysocket <br/>{< br/> private Socket socket; <br/> private bool isconnected; <br/> private byte [] Buf; <br/> private string clientname; <br/> Public const int bufsize = 8192; <br/> Public const string exitcode = "exit "; <br/> Public mysocket (string clientname, Socket socket, byte [] BUF) <br/>{< br/> This. clientname = clientname; <br/> This. socket = socket; <br/> This. buf = Buf; <br/> This. isconnected = false; <br/>}< br/> Public mysocket (string clientname, Socket socket) <br/>: This (clientname, socket, new byte [bufsize]) <br/>{< br/>}< br/> Public mysocket (string clientname) <br/>: This (clientname, new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP) <br/>{< br/>}< br/> Public mysocket () <br/>{< br/> This. buf = new byte [bufsize]; <br/>}< br/> Public Socket socket <br/>{< br/> get {return socket ;} <br/> set {socket = value ;}< br/>}< br/> Public bool isconnected <br/>{< br/> get {return isconnected ;} <br/> set {isconnected = value ;}< br/>}< br/> Public String clientname <br/>{< br/> get {return clientname ;} <br/> set {clientname = value ;}< br/>}< br/> Public byte [] buffer <br/>{< br/> get {return Buf ;} <br/>}< br/> Public void sendmessage (string MSG) <br/>{< br/> byte [] Buf = encoding. utf8.getbytes (MSG); <br/> socket. beginsend (BUF, 0, Buf. length, socketflags. none, new asynccallback (sendcallback), socket); <br/>}< br/> Public void sendcallback (iasyncresult IR) <br/>{< br/> try <br/> {<br/> Socket socket = (socket) IR. asyncstate; <br/> socket. endsend (IR); <br/>}< br/> catch <br/>{< br/>}< br/>}

 

 

 

Iii) client code:

 

Protected void button1_click (Object sender, eventargs e) <br/>{< br/> int Port = 8000; <br/> string host = "192.168.1.222 "; <br/> // string host = "127.0.0.1"; <br/> IPaddress IP = IPaddress. parse (host); <br/> ipendpoint IPE = new ipendpoint (IP, Port); <br/> socket c = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); <br/> C. connect (IPE); <br/> string STR = "data to be sent"; <br/> byte [] BS = encoding. utf8.getbytes (STR); <br/> // send data <br/> C. send (BS, BS. length, 0); <br/> string recvstr = ""; <br/> byte [] recvbytes = new byte [1024]; <br/> int bytes; <br/> bytes = C. receive (recvbytes, recvbytes. length, 0); <br/> // recvstr indicates the received data. <br/> recvstr + = encoding. utf8.getstring (recvbytes, 0, bytes); <br/> C. close (); <br/>}< br/>

 

Iv) Example:

Http://blog.csdn.net/zhoufoxcn/archive/2009/03/18/4000301.aspx

Http://blog.csdn.net/lyd_253261362/archive/2009/07/30/4394370.aspx

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.