附上源码:
[socket_def.inc]
const domain=AF_INET;
_type=SOCK_STREAM;
protocol=IPPROTO_TCP;
var sockfd:longint;
const backlog:longint=100;
const ports=9313;//服务器端口
portc=8795;//客户端端口
const ips=699574464;//服务器IP
ipc=749906112;//客户端IP
var addrs:TInetSockAddr=(family:domain;port:ports;addr:ips);
addrc:TInetSockAddr=(family:domain;port:portc;addr:ipc);
const addrlens:longint=sizeof(addrs);
addrlenc:longint=sizeof(addrc);
const flags=0;
var sockin,sockout:text;
var buff:string;
[socket_service.pas]
program socket_service;
uses sockets;
{$I socket_def.inc}
var addrlen:longint;
begin
sockfd:=socket(domain,_type,protocol);
bind(sockfd,addrs,addrlens);
listen(sockfd,backlog);
accept(sockfd,addrc,sockin,sockout);
reset(sockin);
rewrite(sockout);
writeln('Service online. Please type in message.');
repeat
write('Service send: ');
readln(buff);
writeln(sockout,buff);
write('Client recv: ');
readln(sockin,buff);
writeln(buff);
until false;
close(sockout);
close(sockin);
end.
[socket_client.pas]
program so_cket_client;
uses sockets;
{$I socket_def.inc}
begin
sockfd:=socket(domain,_type,protocol);
bind(sockfd,addrc,addrlenc);
connect(sockfd,addrs,sockin,sockout);
reset(sockin);
rewrite(sockout);
writeln('Client online. Please wait for message.');
repeat
write('Service recv: ');
readln(sockin,buff);
writeln(buff);
write('Client send: ');
readln(buff);
writeln(sockout,buff);
until false;
close(sockout);
close(sockin);
end.
[socket_def.inc]
const domain=AF_INET;
_type=SOCK_STREAM;
protocol=IPPROTO_TCP;
var sockfd:longint;
const backlog:longint=100;
const ports=9313;//服务器端口
portc=8795;//客户端端口
const ips=699574464;//服务器IP
ipc=749906112;//客户端IP
var addrs:TInetSockAddr=(family:domain;port:ports;addr:ips);
addrc:TInetSockAddr=(family:domain;port:portc;addr:ipc);
const addrlens:longint=sizeof(addrs);
addrlenc:longint=sizeof(addrc);
const flags=0;
var sockin,sockout:text;
var buff:string;
[socket_service.pas]
program socket_service;
uses sockets;
{$I socket_def.inc}
var addrlen:longint;
begin
sockfd:=socket(domain,_type,protocol);
bind(sockfd,addrs,addrlens);
listen(sockfd,backlog);
accept(sockfd,addrc,sockin,sockout);
reset(sockin);
rewrite(sockout);
writeln('Service online. Please type in message.');
repeat
write('Service send: ');
readln(buff);
writeln(sockout,buff);
write('Client recv: ');
readln(sockin,buff);
writeln(buff);
until false;
close(sockout);
close(sockin);
end.
[socket_client.pas]
program so_cket_client;
uses sockets;
{$I socket_def.inc}
begin
sockfd:=socket(domain,_type,protocol);
bind(sockfd,addrc,addrlenc);
connect(sockfd,addrs,sockin,sockout);
reset(sockin);
rewrite(sockout);
writeln('Client online. Please wait for message.');
repeat
write('Service recv: ');
readln(sockin,buff);
writeln(buff);
write('Client send: ');
readln(buff);
writeln(sockout,buff);
until false;
close(sockout);
close(sockin);
end.