ACE_SOCK_Acceptor 服务器端socket
ACE_SOCK_Connector 客户机端socket
ACE_SOCK_Stream 用来连接服务器端和客户机端, 在这儿有send和recv的接口.
/#:> cat server.C
#include "SOCK_Acceptor.h"
#include <ace/INET_Addr.h>
#include <ace/Log_Msg.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
ACE_INET_Addr port_to_listen (1234);
ACE_SOCK_Acceptor acceptor;
acceptor.open (port_to_listen, 1);
cout << "Listening ..." << endl;
while (1)
{
ACE_SOCK_Stream peer;
ACE_INET_Addr peer_addr;
ACE_Time_Value timeout (10, 0);
if( 0 == acceptor.accept (peer, &peer_addr, &timeout, 0))
{
cout << "Incoming ..." << endl;
char buffer[BUFSIZ];
ssize_t bytes_received;
while ((bytes_received = peer.recv (buffer, sizeof(buffer))) > 0)
{
buffer[bytes_received]='/0';
cout << "Receiving " << buffer << endl;
peer.send_n (buffer, bytes_received);
}
peer.close ();
}
}
return 0;
}
/#:> cat client.C
#include <ace/SOCK_Connector.h>
#include <ace/INET_Addr.h>
#include <ace/Log_Msg.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
//ACE_INET_Addr remote_addr(atoi("1234"), "your_ip_address");
ACE_INET_Addr remote_addr("0.0.0.0:1234");
ACE_SOCK_Connector connector;
ACE_SOCK_Stream stream;
char buf[BUFSIZ];
if (connector.connect(stream, remote_addr) == -1)
cerr << "Connect to server failed ... Exit" << endl;
sprintf(buf,"Hello World!");
cout << "Send message ..." << endl;
stream.send_n(buf,strlen(buf));
stream.recv(buf, sizeof(buf));
cout << "Receive message: " << buf << endl;
stream.close();
}
/#:> cat Makefile
ACE_ROOT =.../vendor/Corba/ACE_TAO
CCFLAGS += -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__ -DACE_HAS_IPV
LDFLAGS =
ACE_LIBS = -lACE
all: server client
server: server.o
CC $(CCFLAGS) -L$(ACE_ROOT)/lib/NR $(ACE_LIBS) -o $@ $^
client: client.o
CC $(CCFLAGS) -L$(ACE_ROOT)/lib/NR $(ACE_LIBS) -o $@ $^
%.o: %.C
CC -c $(CCFLAGS) -I$(ACE_ROOT)/include -I$(ACE_ROOT)/include/ace -o $@ $^