tcpOpen()
Opens the connection to a TCP/IP server socket.
Synopsis
int tcpOpen(string host, unsigned port [, time timeout]);
Parameters
Parameter | Description |
---|---|
host | Name of the host, to which the function establishes a connection, for example, eiwnt094 |
port | Number of the port, for example, 8080 |
timeout | Timeout for establishing the connection. This parameter is optional. Default is 5 seconds. |
Return value
The socket number of the opened TCP/IP connection.
tcpOpen() returns the socket number.
The return value of tcpOpen() must be used for the socket parameter of tcpWrite().
Use the function getLastError() to test if the function was executed successfully.
In case of an error, -1 will be returned.
Errors
Missing or wrong arguments.
The TCP/IP server socket has already been opened (Close with tcpClose()).
Details
Opens the connection to a TCP/IP Server socket.
You can open up to 255 connections at the same time.
main()
{
string computerNam= "abc9GC5223TU7", data;
unsigned Portnumb= 1025;
int socket, modWrite, modClose, modRead;
time t = 10;
//Open-> Write-> Close
socket = tcpOpen(computerNam, Portnumb);
dyn_errClass err=getLastError();
DebugN(err);
DebugN("Open: " + socket + computerNam+ Portnumb);
DebugN("Delay");
delay(5);
//Write
modWrite= tcpWrite(socket, "TEST");
DebugN("Write: " + modWrite);
DebugN("Delay");
delay(5);
modRead = tcpRead(socket, data, t);
DebugN("Read: " + modRead);
DebugN("Read data: " + data);
DebugN("Finish");
//Close
modClose= tcpClose(socket);
DebugN("TcpClose: " + modClose);
DebugN("Delay");
delay(5);
//Try to write again. Writing does not work anymore
modWrite= tcpWrite(socket, "WRITING DOES NOT WORK ANYMORE");
DebugN("Write does not work anymore: " + modWrite);
DebugN("Delay");
delay(5);
}