Socket Learning (iii). 1

Source: Internet
Author: User

Implemented, LAN client-to-client communication. This is actually a client-side service.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespaceAnalog customer service end 01{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Textbox.checkforillegalcrossthreadcalls=false;//Prohibit multithreading check} Socket socket_my; //actively establish a connection socketSocket Socket_listen;//Listening socketsSocket socket_accept;//Receive socketsThread thread_receive;//Receive ThreadThread thread_accept;//Send Thread         Public voidAccept () { while(true) {socket_accept= Socket_listen.accept ();//Listen socket receive request return receive socketTextboxmag.appendtext ("Client Connection succeeded! "+ Socket_accept.remoteendpoint +"\ r \ n"); Thread_accept=NewThread (Receive); Thread_accept.isbackground=true;            Thread_accept.start (); }        }         Public voidReceive () { while(true)            {                Try                {                    byte[] B =New byte[1024x768]; Socket_accept.receive (B);//receive socket receive information                    stringMessage = Encoding.UTF8.GetString (B,0, b.length); if(!string. IsNullOrEmpty (message)) {Textboxmag.appendtext ( message+"\ r \ n"); }                }                Catch(Exception) {Textboxmag.appendtext ("the connection is off."+"\ r \ n"); Btn_conncet.enabled=false;  Break; }            }        }        Private voidStartlisten_click (Objectsender, EventArgs e) {Socket_listen=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket_listen.bind (NewIPEndPoint (Ipaddress.any,int.            Parse (Textboxmyport.text)); Socket_listen.listen (Ten); Textboxmag.appendtext ("Start listening ."+"\ r \ n"); Thread_receive=NewThread (Accept); Thread_receive.isbackground=true;            Thread_receive.start (); Startlisten.enabled=false; }        Private voidBtn_conncet_click (Objectsender, EventArgs e) {            Try{socket_my=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket_my.connect (NewIPEndPoint (Ipaddress.parse (Textboxip.text),int.                Parse (Textboxport.text)); Btn_conncet.enabled=false; }            Catch(Exception ex) {Textboxmag.appendtext ("Connection not on"+"\ r \ n"); }        }        Private voidButton_send_click (Objectsender, EventArgs e) {            Try{socket_my.send (Encoding.UTF8.GetBytes (Textboxsend.text+"\ r \ n")); }            Catch(Exception) {Textboxmag.appendtext ("Send failed"+"\ r \ n"); }        }    }}


Here is the port number, and the IP, next, do a service-side record it said that there is a client's IP communication port is good.

Socket Learning (iii). 1

Related Article

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.