How to Write a TCP/IP communication program

Source: Internet
Author: User
Tags socket error

Document title: How to Write TCP/IP communicationsProgramAuthor: anonymous name

Key words: TCP/IP, communication program, socket, tserversocket, tclientsocket

In this paper, the conversion from C ++ builder research-http://www.ccrun.com/article/go.asp? I = 395 & D = r0vn4p
Recently, I have been asking people on the internet how to use BCB tserversocket and tclientsocket for programming. So I decided to tell you some of my programming experience, this allows you to master their usage as soon as possible.

First, let's talk about some of their settings (attributes ):
Attributes of tserversocket
Active
Whether it is working or not. You can set it to true or false to start and stop listening.
Port
The port listened on by the local machine. It can be set to a value greater than 1024;
Servertype
The server has two options: stnonblocking and stthreadblocking. The default value is non-blocking. Programming in non-blocking mode is relatively simple (I personally think). In blocking mode, each connection must use its own thread to control sending and receiving.
Threadcachesize
The number of buffered threads is valid when servertype is stthreadblocking.
Attributes of tclientsocket
Active
Whether it is working or not. You can set it to true or false to establish or disconnect a connection with the server.
Host
Server (serversocket) computer name, is a string
Address
Server (serversocket) IP Address
Clienttype
One way the client works is ctnonblocking, and the other is ctblocking. The default method is non-blocking.
Port
The port connected to the server (serversocket). This port is the listener port of serversocket.
It is equivalent to setting the (host) host name and the (Address) IP address of clientsocket. However, to set the host name, the network must have a DNS (Domain Name Resolution Service) server, in addition, host name setting is slower than Host IP address setting. We recommend that you use address to connect to serversocket.

After all the attributes are set, the serversocket control can listen by setting its active attribute to true. Clientsocket can be connected to serversocket by setting its active attribute. After the connection is successful, you can communicate. Some events will occur in this process. Let's talk about their respective events.

Several tserversocket events
Onaccept
When a client successfully connects to the server, this event is generated and the connection becomes available. You can send and receive data to the client.
Onclientconnect
This event occurs when a client is establishing a connection with the server. You can decide whether to accept the connection.
Onclientdisconnect
This event is generated when a client is disconnected from the server. You need to perform some processing here, such as clearing connections from the connection list and releasing memory.
Onclienterror
This event is generated when an error occurs between the client and the server. In this event, you can set errorcode = 0 to block system error prompts. In this way, you can avoid annoying English errors. Based on the different values of errorevent, you can find out what is wrong. It has a few error types.
Eegeneral unknown error
An error occurred while sending eesend data.
An error occurred while receiving data from eereceive.
An error occurred while requesting the connection from the eeconnect client.
Eedisconnect client disconnection Error
An error occurred when eeaccept accepted a customer.
Generally, when an error occurs, the connection of this customer is no longer available and the connection failure must be handled.
Onclientread
This event occurs when the server receives data from the client. The data of the receiving client is carried out here.
Onclientwrite
This event is generated when the server sends data.

Tclientsocket events

Onconnect
This event is generated after the same server (serversocket) connection is successful. After this event is generated, the connection is available and data can be sent to serversocket.
Onconnecting
This event occurs when you are connecting to the server.
Ondisconnect
This event is generated after the connection is disconnected from the server. After this event is generated, the active attribute of clientsocket is false. In this case, the connection is unavailable and data can be sent to the server only after a new connection is established.
Onerror
This event is generated when a socket error occurs. This event has the same meaning as the serversocket error event, but it does not have the eeaccept error.
Onread
This event is generated when data sent from the server is received.
Onwrite
This event is generated when data is sent to the server.

The basic attributes and events of these two controls are described above. You can use these two controls to write communication programs in some ways. I am tired of writing them today, another day I was writing it and using these two controls to write a communication program. There were two options: a LAN chat program and a LAN control program, what do you like? Give me some suggestions.

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.