Simple socket communication instance (WCF calls socket)

Source: Internet
Author: User

Server:

Console program listener

1 /// <summary> 2 // server 3 // </Summary> 4 class program 5 {6 static socket serversocket; 7 static socket clientsocket; 8 Static thread; 9 10 static void main (string [] ARGs) 11 {12 ipendpoint ipep = new ipendpoint (IPaddress. any, 3001); 13 serversocket = new socket (ipep. addressfamily, sockettype. stream, protocoltype. TCP); 14 serversocket. BIND (ipep); 15 serversocket. listen (10); 16 while (true) 17 {18 clientsocket = serversocket. accept (); 19 thread = new thread (New threadstart (dowork); 20 thread. start (); 21} 22 23} 24 25 Private Static void dowork () 26 {27 socket S = clientsocket; // client information 28 ipendpoint = (ipendpoint) s. remoteendpoint; 29 string address = ipendpoint. address. tostring (); 30 string port = ipendpoint. port. tostring (); 31 console. writeline (address + ":" + port + "connected"); 3 2 byte [] inbuffer = new byte [1024]; 33 byte [] outbuffer = new byte [1024]; 34 string inbufferstr; 35 string outbufferstr; 36 try37 {38 While (true) 39 {40 s. receive (inbuffer, 1024, socketflags. none); 41 inbufferstr = encoding. ASCII. getstring (inbuffer); 42 console. writeline (address + ":" + port + "said:"); 43 console. writeline (inbufferstr); 44 outbufferstr = console. readline (); 45 outbuffer = encoding. ASCII. get Bytes (outbufferstr); 46 s. Send (outbuffer, outbuffer. length, socketflags. None); 47} 48} 49 catch50 {51 console. writeline ("the client has been disabled! "); 52} 53} 54}

 

Client

Business logic layer

1 namespace cloudtraplatwcf. business 2 {3 /// <summary> 4 /// 5 /// </Summary> 6 public class boutside_bond 7 {8 private string Path = @ "\ side_bond "; // log storage path 9 static socket _ clientsocket; 10 11 # region private Method 12 // <summary> 13 // 14 // </Summary> 15 // <Param name = "outbufferstr"> return information </param> 16 // <returns> return result </returns> 17 Public String helpsendandreceivemessage (string outbufferstr) 18 {19 string IP = confighelper. getreturnbondip (); 20 int Port = confighelper. getreturnbondport (); 21 ipendpoint _ ipendpoint = new ipendpoint (IPaddress. parse (IP), Port); 22 try23 {24 if (_ clientsocket = NULL) 25 {26 _ clientsocket = new socket (_ ipendpoint. addressfamily, sockettype. stream, protocoltype. TCP); 27 _ clientsocket. connect (_ ipendpoint); // connect the socket to the server 28} 29 byte [] outbuffer = new byte [1024]; 30 byte [] inbuffer = new byte [1024]; 31 // send message 32 outbuffer = encoding. ASCII. getbytes (outbufferstr); 33 _ clientsocket. send (outbuffer, outbuffer. length, socketflags. none); 34 // receive Server Information 35_clientsocket. receive (inbuffer, 1024, socketflags. none); 36 string logmsg = ""; 37 logger. write (logmsg, "", PATH); 38 return encoding. ASCII. getstring (inbuffer); 39} 40 catch (exception ex) 41 {42 throw ex; 43} 44} 45 # endregion46} 47}

 

WCF contract

1 namespace cloudtraplatwcf. contracts 2 {3 /// <summary> 4 // SMS interface 5 /// </Summary> 6 [servicecontract (name = "ioutside_smscontract")] 7 public interface ioutside_smscontract 8 {9 // <summary> 10 /// 11 /// </Summary> 12 /// <returns> </returns> 13 [operationcontract] 14 string test (); 15} 16}

 

WCF Service

# Region ioutside_smscontract member Public String test () {string res = new boutside_bond (). helpsendandreceivemessage ("hello"); Return res ;}# endregion

 

Simple socket communication instance (WCF calls socket)

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.