. The following example is a TCP/IP windows socket client written in C language. This example corresponds to the example. To use the code snippet, set the #define IPADDRESS '10.4.20.201' to the correct IP address and compile with MinGW using the following command: C: mingw32-gcc.exe tcpclient2.c -lws232 First start the server in MC by running the script exhibited in and then run the TCP/IP client.
The example framework has two main menu items: 'Start Winsock Handler' (Ctrl-W), and a generic 'Action' item (Ctrl-A). When you give the Start command,. From all of this you can see the Microsoft Winsock programming is almost identical to Unix/Linux. Client.cpp // // Extremely simple, stream client example.
The client will receive the data sent from the server, send data to set the MC variables x and y, query the variables' values and exit. This example is based on the 'connect' example from MSDN.
TCP/IP is a specification that defines a series of protocols used to standardize how computers exchange information with each other. TCP/IP provides communication across interconnected networks that use diverse hardware architectures and various operating systems.
The protocols in TCP/IP are arranged in a series of layers known as a protocol stack. Each layer has its own functionality. Winsock is a standard that is maintained by Microsoft. This standard is basically a set of routines that describe communications to and from the TCP/IP stack.
These routines reside in a dynamic link library (DLL) that runs under Windows. The Winsock DLL is interfaced with TCP/IP and from there through the Internet. This article will show how to use the Winsock in a client server environment. We will create two separate applications, one of which will be a server and the other will be a client. Both client and server will interact with each other to exchange data. Client will send a request to the server, and the server, which will be connected to a database, will retrieve the information requested by the client from the database and will return the requested information back to the client.
You will find a database with this article. The database contains item numbers and their prices. In real-life situations, the database might be located on a machine different from the one that hosts the client application. Ports Let's talk about the ports before we proceed any further. A port is a special memory location that exists when two computers are in communication via TCP/IP.
Applications use a port number as an identifier to other computers. Both the sending and receiving computers use this port to exchange data.
To make the job of communication easier, some port numbers have been standardized. These standard port numbers have no inherent value other than that users have agreed to use them with certain applications.
The table below lists a number of popular and publicly accepted port numbers and their corresponding applications. Service Port HTTP 80 FTP 20, 21 Gopher 70 SMTP 25 POP3 110 Telnet 23 Finger 79 Local loops/callbacks 0 Using the Winsock Control Winsock is above the TCP/IP protocol stack in the ISO/OSI model. TCP/IP is an industry standard communication protocol that defines methods for packaging data into packets for transmission between computing devices on a heterogeneous network. TCP/IP is the standard for data transmission over networks, including the Internet. TCP establishes a connection for data transmission and IP defines the method for sending data packets.
The Microsoft Winsock control makes using the TCP/IP a breeze. Microsoft has wrapped up the Winsock and NETAPI API calls into a neat package that you can easily incorporate into your Visual Basic applications. Winsock Operating Modes The Transport layer (also known as the Host-to-Host Transport layer) is responsible for providing the Application layer with session and datagram communication services. The core protocols of the Transport layer are TCP and User Datagram Protocol (UDP). The Winsock control supports the following two operating modes:. sckTCPProtocol.
sckUDPProtocol Winsock Properties Winsock enables you to create clients and servers using the same control. This dual functionality enables you to specify through property setting the type of application you will be building.
The Winsock control uses a number of the same properties, whether you are creating a client or a server, thereby all but eliminating the learning curve needed to create applications. Some of the important properties of the control are as follows: BytesReceived Property This property returns the number of bytes currently in the receive buffer.
This is a read-only property and is unavailable at design time. The value returned is a long integer. LocalHostName Property The LocalHostName property returns the name of the local host system. This is a read-only property and is unavailable at design time.
The value returned is a string. LocalIP Property The LocalIP property returns the local host system IP address in the form of a string, such as 11.0.0.127. This property is read-only and is unavailable at design time. LocalPort Property This property returns or sets the local port number. This can be both 'read from' and 'written to' and is available at both design time and runtime. The value returned is a long integer. Protocol Property Returns or sets the protocol, either TCP or UDP, used by the Winsock control.
RemoteHost Property The RemoteHost property returns or sets the remote host. This can be both 'read from' and 'written to' and is available both in design time and runtime. The value returned is a string and can be specified either as an IP address or as a DNS name. RemotePort Property This property returns or sets the remote port number. State Property This returns the state of the control as expressed by an enumerated list.
This is a read-only property and is unavailable at design time. Winsock Methods Some of the important methods of Winsock control are as follows: Accept Method It accepts the request for connection from the client system. For this method to be used, the control must be in the listening state. Close Method The Close method terminates a TCP connection from either the client or server applications.
GetData Method GetData is the method that retrieves the current block of data from the buffer and then stores it in a variable of the variant type. PeekData Method The PeekData method operates in a fashion similar to the GetData method. However, it does not remove data from the input queue. Listen Method This is invoked on the server application to have the server application wait for a TCP request for connection from a client system. SendData Method This method dispatches data to the remote computer. It is used for both the client and server systems. Connect Method The Connect method requests a connection to a remote computer.
I am not going to discuss events here. You can find more Winsock details at the following address: As stated earlier, this article describes the creation of two applications - one server and one client. The database used in the sample is also provided with the code.
The database - Prices.mdb -. Contains a single table with the fields item number and price. The client sends the item number to the server and the server retrieves the price against that item number from the database and sends it back to the client. One of the current trends in software development is the issue of thick clients versus thin clients. A thick client is basically an application that performs the bulk of the processing on the individual client PC, whereas a thin client performs the processing on the server.
Creating the Client Follow the steps below:. Start a new EXE project.
Add a Winsock control to your application. Add all the controls to the form (see the application for details).
Comments. server closes then how client is known of server status connection Posted by bhavik on 10:29am how do client know when server is disconnected or how it should be disconnected when server is closed due to some error.
server closes then how client is known of server status connection Posted by Gopal on 05:10am Bhavik, You can use a timer control which checks the connection regularly in a fixed time duration and whenever finds the connection off then popup the message to confirm the disconnection. Private Sub Timer1Timer If sockMain.State sckConnected Then If A = 1 Then txtStatus.Text = txtStatus.Text & 'CONNECTION WITH REMOTE SERVER IS LOST' & vbNewLine & 'APPLICATION IS NOT RUNNING ON REMOTE COMPUTER' & vbRed Timer1.Interval = 0 End If End If End Sub.
You must have javascript enabled in order to post comments. Leave a Comment. Your email address will not be published.