0% found this document useful (0 votes)
27 views2 pages

!定义 socket1 为套接字设备; !创建套接字 socket1; !连接远程服务器 IP:192.168.0.1 端 口: 2000; !示教器上显示服务器连接成功; ! 往服务器上发送客户端连 接成功; !从服务器接收字符串数据放到 sdata 字符串变 量中 !在示教器上显示 sdata 里的数据 !关闭套接字

The document describes a client and server program that communicate over a socket. The client program connects to the server at IP 192.168.0.1 port 2000, sends a message, and receives a response in a loop. The server program binds to port 1025, listens for connections, receives and acknowledges messages, then closes the connection.

Uploaded by

heng yuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

!定义 socket1 为套接字设备; !创建套接字 socket1; !连接远程服务器 IP:192.168.0.1 端 口: 2000; !示教器上显示服务器连接成功; ! 往服务器上发送客户端连 接成功; !从服务器接收字符串数据放到 sdata 字符串变 量中 !在示教器上显示 sdata 里的数据 !关闭套接字

The document describes a client and server program that communicate over a socket. The client program connects to the server at IP 192.168.0.1 port 2000, sends a message, and receives a response in a loop. The server program binds to port 1025, listens for connections, receives and acknowledges messages, then closes the connection.

Uploaded by

heng yuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

MODULE Client_prog

VAR string sdata;


VAR socketdev socket1; !定义 socket1 为套接字设备;
PROC Client_prog()
socketcreate socket1; !创建套接字 socket1;
SocketConnect socket1,"192.168.0.1",2000; !连接远程服务器 IP:192.168.0.1 端
口:2000;
TPwrite "server connect ok"; !示教器上显示服务器连接成功;
WHILE TRUE do
socketsend socket1\str:="Client connect ok"+"\0D\0A" ! 往服务器上发送客户端连
接成功;
socketreceive socket1\str:=sdata; !从服务器接收字符串数据放到 sdata 字符串变
量中
tpwrite "sdata:=",sdata; !在示教器上显示 sdata 里的数据
ENDWHILE
socketclose socket1; !关闭套接字
ENDPROC
endmodule

MODULE server_prog
VAR socketdev temp_socket;
VAR socketdev client_socket;
VAR string received_string;
VAR bool keep_listening := TRUE;
PROC server_prog
SocketCreate temp_socket;
SocketBind temp_socket, "192.168.0.2", 1025;
SocketListen temp_socket;
WHILE keep_listening DO
! Waiting for a connection request
SocketAccept temp_socket, client_socket;
! Communication
SocketReceive client_socket \Str:=received_string;
TPWrite "Client wrote - " + received_string;
received_string := "";
SocketSend client_socket \Str:="Message acknowledged";
! Shutdown the connection
SocketReceive client_socket \Str:=received_string;
TPWrite "Client wrote - " + received_string;
SocketSend client_socket \Str:="Shutdown acknowledged";
SocketClose client_socket;
ENDWHILE
SocketClose temp_socket;
ENDPROC
ENDMODULE

You might also like