#ifndef unix
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int SocketLibStart();
void SocketLibEnd();
int TCPStartServer(const int port, const int queue);
int TCPWaitForConnection(const int sd);
int TCPStopServerTCP(const int sd);
int TCPStartClient(const char *dest, const int port);
int TCPStopClient(const int sd);
int TCPRecvAny(const int socket, char *buffer, const int maxsize);
int TCPSendAny(const int socket, const char *buffer, int size);
int TCPRecvLine(const int socket, char *line, const int maxsize);
int TCPSendLine(const int socket, const char *line);
int main()
{
int sd1, sd2;
SocketLibStart();
sd1=TCPStartServer(60000, 6);
if ( sd1<0) {
cerr << "Server cannot be started - aborting" << endl;
exit(1);
}
{
char buf[81];
char nam[81];
int visits = 0;
while (1) {
sd2=TCPWaitForConnection(sd1);
if ( sd2<0) {
cerr << "accept failed" << endl;
exit(1);
}
visits++;
TCPSendLine(sd2,"Hello, what is your name?");
TCPRecvLine(sd2, nam, 80);
sprintf(buf,"It is nice to meet you %s, you are connection number %d today.\n",
nam, visits);
TCPSendLine(sd2,buf);
closesocket(sd2);
}
}
SocketLibEnd();
return(0);
}
int SocketLibStart() {
#ifdef WIN32
WSADATA wsaData;
return WSAStartup(0x0101, &wsaData);
#else
return 0;
#endif
}
void SocketLibEnd() {
#ifdef WIN32
WSACleanup();
#endif
}
int TCPStartServer(const int port, const int queue) {
struct protoent *ptrp;
struct sockaddr_in sad;
int sd;
memset((char *)&sad,0,sizeof(sad));
sad.sin_family = AF_INET;
sad.sin_addr.s_addr = INADDR_ANY;
sad.sin_port = htons((u_short)port);
ptrp = getprotobyname("tcp");
if ( ptrp == 0) {
cerr << "cannot map \"tcp\" to protocol number" << endl;
return(-1);
}
sd = (int)socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (socket < 0) {
cerr << "socket creation failed" << endl;
return(-1);
}
if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
cerr << "bind failed" << endl;
return(-1);
}
if (listen(sd, queue) < 0) {
cerr << "listen failed" << endl;
return(-1);
}
return(sd);
}
int TCPWaitForConnection(const int sd) {
struct sockaddr_in cad;
int alen;
alen = sizeof(cad);
return( (int)accept(sd, (struct sockaddr *)&cad, &alen) );
}
int TCPStopServerTCP(const int sd) {
return closesocket(sd);
}
int TCPStartClient(const char *host, const int port) {
struct hostent *ptrh;
struct protoent *ptrp;
struct sockaddr_in sad;
int sd;
memset((char *)&sad,0,sizeof(sad));
sad.sin_family = AF_INET;
ptrh = gethostbyname(host);
if ( ((char *)ptrh) == NULL ) {
cerr << "invalid host: " << host << endl;
return(-1);
}
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
sad.sin_port = htons((u_short)port);
ptrp = getprotobyname("tcp");
if ( ptrp == 0) {
cerr << "cannot map \"tcp\" to protocol number" << endl;
return(-1);
}
sd = (int)socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
cerr << "soclet creation failed" << endl;
return(-1);
}
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
cerr << "connection failed" << endl;
return(-1);
}
return(sd);
}
int TCPStopClient(const int sd) {
return closesocket(sd);
}
int TCPRecvAny(const int socket, char *buffer, const int maxsize) {
return( recv(socket,buffer,maxsize,0) );
}
int TCPSendAny(const int socket, const char *buffer, int size) {
return( send(socket,buffer,size,0) );
}
int TCPRecvLine(const int socket, char *line, const int maxsize) {
int n;
int len=0;
char bch;
n = recv(socket,&bch,1,0);
while (n > 0) {
if (len>=maxsize) return(-1);
if (bch!='\n'&&bch!='\r')
{
line[len]=bch;
len++;
}
else
{
line[len]='\0';
if (len>0) return(len);
}
n = recv(socket,&bch,1,0);
}
return(n);
}
int TCPSendLine(const int socket, const char *line) {
return( (int)send(socket,line,strlen(line),0) );
}