BCB socket communication

Source: Internet
Author: User

Server: <br/> 1. port: 6767 <br/> 2. <SPAN class = 'wp _ keywordlink'> Code </span>: <br/> // optional <br/> # include <VCL. h> <br/> # pragma hdrstop <br/> # include "unit1.h" <br/> // restart <br/> # pragma package (smart_init) <br/> # pragma resource "*. DFM "<br/> tform1 * form1; <br/> ///---- --------------------------------------------------------------------- <Br/>__ fastcall tform1: tform1 (tcomponent * owner) <br/>: tform (owner) <br/>{< br/>}< br/> // response <br/> void _ fastcall tform1: button1click (tobject * sender) <br/>{< br/> // send information <br/> ansistring MSG = edit1-> text + ":" + edit2-> text; <br/> edit2-> text = ""; <br/> me Mo1-> lines-> Add (MSG); <br/> // send the information to all clients. <br/> for (INT I = 0; I <serversocket1-> socket-> activeconnections; I ++) <br/> serversocket1-> socket-> connections [I]-> sendtext (MSG ); <br/>}< br/> // configure <br/> void _ fastcall tform1: serversocket1accept (tobject * sender, <br/> tcustomwinsocket * socket) <br/> {<br/> // The accept event occurs when the server receives the client request. <br/> Sistring MSG = "server message:" + socket-> remotehost + "entering the chat room "; <br/> // send a message to all clients <br/> for (INT I = 0; I <serversocket1-> socket-> activeconnections; I ++) <br/> serversocket1-> socket-> connections [I]-> sendtext (MSG ); <br/> // Add this information to your information bar <br/> memo1-> lines-> Add (MSG ); <br/>}< br/> // configure <br/> void _ fastcall tform1: serversocket1clientread (tobject * Sender, <br/> tcustomwinsocket * socket) <br/>{< br/> // read data from the socket <br/> ansistring MSG = socket-> receivetext (); <br/> // send the information to all clients <br/> for (INT I = 0; I <serversocket1-> socket-> activeconnections; I ++) <br/> serversocket1-> socket-> connections [I]-> sendtext (MSG ); <br/> // Add this information to your information bar <br/> memo1-> lines-> Add (MSG ); <br/>}< br/> // ----------------------------------------------------------------------------- <br/> Void _ fastcall tform1: formcreate (tobject * sender) <br/>{< br/> edit1-> text = ""; <br/> edit2-> text = ""; <br/>}< br/> // signature <br/> void _ fastcall tform1 :: button2click (tobject * sender) <br/>{< br/> serversocket1-> Port = strtoint (edit3-> text); <br/> serversocket1-> active = true; <br/> shape1-> visible = true; <br/>}< br/> ///------------------ Callback <br/> void _ fastcall tform1: button3click (tobject * sender) <br/>{< br/> serversocket1-> active = false; <br/> shape1-> visible = false; <br/>}< br/> // clients <br/> client: <br/> 1. address: 127.0.0.1 (Server IP address) <br/> 2. port: 6767 (server port number) <br/> 3. code <br/> ///-------------------------------- ----------------------------------------- <Br/> # include <VCL. h> <br/> # pragma hdrstop <br/> # include "unit1.h" <br/> // restart <br/> # pragma package (smart_init) <br/> # pragma resource "*. DFM "<br/> tform1 * form1; <br/> // configure <br/>__ fastcall tform1: tform1 (tcompon Ent * owner) <br/>: tform (owner) <br/>{< br/>}< br/> // response <br/> void _ fastcall tform1: button1click (tobject * sender) <br/>{< br/> ansistring MSG = edit1-> text + ":" + edit2-> text; <br/> memo1-> lines-> Add (MSG); <br/> edit2-> text = ""; <br/> // send information to the server <br/> clientsocket1-> socket-> sendtext (MSG); <br/>}< br/> //-------------------------- --------------------------------------------- <Br/> void _ fastcall tform1: clientsocket1read (tobject * sender, <br/> tcustomwinsocket * socket) <br/>{< br/> // The client reads the Information <br/> ansistring MSG = socket-> receivetext (); <br/> // determine whether the message is sent by yourself <br/> If (MSG! = Memo1-> lines-> strings [memo1-> lines-> count-1]) <br/> memo1-> lines-> Add (MSG ); <br/>}< br/> // configure <br/> void _ fastcall tform1: formcreate (tobject * sender) <br/>{< br/> edit1-> text = ""; <br/> edit2-> text = ""; <br/>}< br/> // signature <br/> void _ fastcall tform1: button2click (tobject * sender) <br/>{< br/> clientsocket1-> address = edit3-> text; <br/> clientsocket1-> Port = strtoint (edit4-> text ); <br/> clientsocket1-> active = true; <br/>}< br/> // wait <br/> void _ fastcall tform1 :: button3click (tobject * sender) <br/>{< br/> clientsocket1-> active = false; <br/>}< br/> // configure <br/> void _ fastcall tform1: clientsocket1connect (tobject * sender, <br/> tcustomwinsocket * socket) <br/>{< br/> ansistring MSG = "connection successful, [host] "+ socket-> remotehost +" [host address] "+ socket-> remoteaddress +" [host listening port] "+ inttostr (socket-> remoteport ); <br/> showmessage (MSG); <br/>}< br/> // response <br/> void _ fastcall tform1: clientsocket1disconnect (tobject * sender, <br/> tcustomwinsocket * socket) <br/> {<br/> socket-> sendtext (edit1-> text + ""); <br/> // clientsocket1-> socket-> sendtext (edit1-> text + "offline"); <br/> showmessage ("disconnected "); <br/>}< br/> // ------------------------------------------------------------------------------- body

 

SocketSocket: Each socket is composedIPAddress+Service number (called PortPort. After data transmission, you must release and cancel the connection.

TclientsocketAndTserversocketTwo network components, located inInternetPage.

 

Tserversocket:

1.PortAttribute:IntType, set the port number, such6767

2.ActivateAttribute:BoolType, set the listener:True; Disable listening:False

3.OnlistenEvent: This event occurs before the listener connection is formed.

4.OnacceptEvent: This event occurs after a client connection request is accepted.

5.OnclientreadEvent: This event reads data from the socket (sent from the client)

6.OnclientwriteEvent: This event allows you to write data to a socket.

 

Tclientsocket:

1.AddressAttribute: ServerIPAddress

2.HostAttribute: name of the server host, whenHostAndAddressAll refer to timing,TclientsocketWill useHostAttribute work

3.PortAttribute:IntType, set the socket port number, as shown in figure6767

4.ActivateAttribute:True: Establish a connection with the server;False: Disconnect from the server

5.OnlookupEvent: This event occurs before the server socket is located.

6.OnconnectingEvent: This event occurs when the server socket is located.

7.OnconnectEvent: This event occurs after a connection is established with the server.

8.OnreadEvent: You can read data from the connection socket.

9.OnwriteEvent: This event allows you to write data to a socket.

10.OndisconnectEvent: This event occurs when you are disconnected from the server.

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.