C # network programming (1) -- C # simple lan Communication

Source: Internet
Author: User

Requirements:

1. Server:

2. Client:

3. The client sends a message to the server. After receiving the message, the server displays the message and sends the confirmation message.

 

The server code is as follows:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. net; <br/> using system. net. sockets; </P> <p> namespace lan_server <br/> {<br/> public class server <br/>{< br/> Public static void main () <br/> {<br/> try <br/> {<br/> // instance that converts an IP address to IPaddress <br/> IPaddress iPad = IPaddress. parse ("127.0.0.1"); </P> <p> // initialize the listener. Port: 8001 <br/> tcplistener mylist = new tcplistener (IPAD, 8001 ); </P> <p> // start listening to the server port <br/> mylist. start (); </P> <p> // output server startup information <br/> console. writeline ("Start service on port 8001... "); <br/> console. writeline ("local node:" + mylist. localendpoint); <br/> console. writeline ("waiting for connection ..... "); </P> <p> // wait for the access connection request to be processed <br/> // the newly established connection is represented by socket S. <br/> socket S = mylist. acceptsocket (); <br/> console. writeline ("connection from" + S. remoteendpoint); </P> <p> // receives client information <br/> byte [] B = new byte [100]; <br/> int K = S. receive (B); <br/> console. writeline ("received... "); <br/> for (INT I = 0; I <K; I ++) <br/> {<br/> console. write (convert. tochar (B [I]); <br/>}</P> <p> // process client requests, respond to the client <br/> asciiencoding ASEN = new asciiencoding (); <br/> S. send (ASEN. getbytes ("the string was recieved by the server. "); <br/> console. writeline ("/n Response Message sent"); <br/> console. read (); </P> <p> // release resources in the aftermath <br/> S. close (); <br/> mylist. stop (); <br/>}< br/> catch (exception e) <br/>{< br/> console. writeline ("error ..... "+ E. stacktrace); <br/>}</P> <p>

The client code is as follows:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. io; <br/> using system. net; <br/> using system. text; <br/> using system. net. sockets; </P> <p> namespace lan_client <br/> {<br/> public class client <br/>{< br/> Public static void main () <br/>{< br/> try <br/> {<br/> // create a client socket <br/> tcpclient tcpclnt = new tcpclient (); <br/> console. writeline ("connection ..... "); </P> <p> // connect to the server <br/> tcpclnt. connect ("maid", 8001); <br/> console. writeline ("connected"); <br/> console. write ("Enter the string to be transmitted:"); </P> <p> // read the string <br/> string STR = console. readline (); </P> <p> // obtain the stream of the client <br/> stream STM = tcpclnt. getstream (); </P> <p> // sending string <br/> asciiencoding ASEN = new asciiencoding (); <br/> byte [] BA = Asen. getbytes (STR); <br/> console. writeline ("transmission ..... "); <br/> STM. write (BA, 0, ba. length); </P> <p> // receives the information returned from the server <br/> byte [] BB = new byte [100]; <br/> int K = STM. read (BB, 0,100); </P> <p> // information returned by the output server <br/> for (INT I = 0; I <K; I ++) <br/> {<br/> console. write (convert. tochar (BB [I]); <br/>}< br/> console. read (); </P> <p> // close the client connection <br/> tcpclnt. close (); <br/>}< br/> catch (exception e) <br/>{< br/> console. writeline ("error ..... "+ E. stacktrace); <br/>}< br/>

The running result is as follows:

 

Defects:

(1) Real-time dialogs are not allowed.

(2) The connection cannot be closed manually.

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.