I am trying to wrap unix socket api calls in a C++ class to make it easier to work with. I have taken a minimal working example written in C and duplicated the code in a class. As you can see from below the code is identical whether I am calling the function version or method versions of the wrapper code. See ugly temporary code below:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
static int sockfd;
static int rec_sock;
static int len;
typedef struct sockaddr_in sockaddr_in;
static sockaddr_in addr;
static sockaddr_in recaddr;
int ServerSocket_socket(int family, int type, int protocol)
{
sockfd = socket(AF_INET, SOCK_STREAM, 0)
return sockfd;
}
void ServerSocket_bind(int port)
{
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = port;
bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
}
void ServerSocket_printInfo(void)
{
len = sizeof(addr);
getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), (addr.sin_port));
}
void ServerSocket_listen(int backlog)
{
listen(sockfd, backlog);
}
void ServerSocket_accept(void)
{
rec_sock = accept(sockfd, (struct sockaddr *)(&recaddr), (socklen_t *)&len);
}
void ServerSocket_printPeerInfo(void)
{
printf("remote machine = %s, port = %x, %x.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
memset(&recaddr, 0, sizeof(recaddr));
len = sizeof(addr);
getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
}
void ServerSocket_write(void)
{
write(rec_sock, "hi, there", 10);
sleep(20);
exit(1);
}
struct ServerSocket
{
int socket(int family, int type, int protocol)
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
return sockfd;
}
void bind(int port)
{
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = port;
::bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
}
void printInfo(void)
{
len = sizeof(addr);
getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), (addr.sin_port));
}
void listen(int backlog)
{
::listen(sockfd, backlog);
}
void accept(void)
{
rec_sock = ::accept(sockfd, (struct sockaddr *)(&recaddr), (socklen_t *)&len);
}
void printPeerInfo(void)
{
printf("remote machine = %s, port = %x, %x.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
memset(&recaddr, 0, sizeof(recaddr));
len = sizeof(addr);
getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
printf("remote machine = %s, port = %d, %d.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
}
void write(void)
{
::write(rec_sock, "hi, there", 10);
sleep(20);
exit(1);
}
};
When I call the functions the code works like a champ. However when I run the almost identical code wrapped in methods I get a connection refused. Any idea what this minimal code change is doing to cause the example not to work?
ServerSocket_socket(AF_INET, SOCK_STREAM, 0);
ServerSocket_bind(1031);
ServerSocket_printInfo();
ServerSocket_listen(5);
ServerSocket_accept();
ServerSocket_printPeerInfo();
ServerSocket_write();
/*
ServerSocket sock;
sock.socket(AF_INET, SOCK_STREAM, 0);
sock.bind(1031);
sock.printInfo();
sock.listen(5);
sock.accept();
sock.printPeerInfo();
sock.write();
*/
In ServerSocket_bind change:
addr.sin_port = port;
to
addr.sin_port = htons(port);
And it should work. Also, use strace to debug syscalls (or ltrace for libc) like:
strace -f ./a.out
ltrace ./a.out
Try this:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
typedef struct sockaddr_in sockaddr_in;
struct ServerSocket
{
int sockfd;
int rec_sock;
ServerSocket()
{
sockfd = -1;
rec_sock = -1;
}
~ServerSocket()
{
if (rec_sock != -1) closesocket(rec_sock);
if (sockfd != -1) closesocket(sockfd);
}
void socket(int family, int type, int protocol)
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
void bind(int port)
{
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
::bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
}
void printInfo(void)
{
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
int len = sizeof(addr);
getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
}
void listen(int backlog)
{
::listen(sockfd, backlog);
}
void accept(void)
{
rec_sock = ::accept(sockfd, (struct sockaddr *)(&recaddr), (socklen_t *)&len);
}
void printPeerInfo(void)
{
sockaddr_in recaddr;
memset(&recaddr, 0, sizeof(recaddr));
int len = sizeof(addr);
getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
printf("remote machine = %s, port = %d.\n", inet_ntoa(recaddr.sin_addr), ntohs(recaddr.sin_port));
}
void write(void *data, int len)
{
::write(rec_sock, (char*)data, len);
}
};
.
ServerSocket sock;
sock.socket(AF_INET, SOCK_STREAM, 0);
sock.bind(1031);
sock.printInfo();
sock.listen(5);
sock.accept();
sock.printPeerInfo();
sock.write("hi, there", 10);
sleep(20);
exit(1);
Related
Am trying to develop a reader for multicast UDP broadcast.
Written a sample code to test on server.
In server I can see 2 NIC interfaces.
While executing the code, I can see the same packet being received twice.
What other changes do I need to do to eliminate duplicate packets?
Server is CentOS 7.9
gcc version 9.1.0
I hope this shouldn't matter
sample code
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#define HELLO_PORT 12345
#define HELLO_GROUP "227.0.0.376"
#define INTRF "10.0.21.1"
#define MSGBUFSIZE 256
int main(int argc, char *argv[])
{
string source_iface(INTRF);
string group(HELLO_GROUP);
int port(HELLO_PORT);
int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
exit(1);
}
u_int yes = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
{
perror("Reusing ADDR failed");
exit(1);
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(group.c_str());
if (bind(fd,(struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(1);
}
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(group.c_str());
mreq.imr_interface.s_addr = inet_addr(source_iface.c_str());
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
perror("setsockopt");
exit(1);
}
socklen_t addrlen;
int nbytes;
char msgbuf[MSGBUFSIZE];
while (1)
{
memset(&msgbuf, 0, MSGBUFSIZE);
addrlen = sizeof(addr);
if ((nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *)&addr, &addrlen)) < 0)
{
perror("recvfrom");
exit(1);
}
//msgbuf will be a structure, inside that there is a unique ID
}
return 0;
}
I am trying to get a very basic hello world UDP sender and UDP multicast listener to work. I have a PC but have a virtual machine with the Linux OS CentOS. It has no problems connecting to the internet. The sender and listener are two separate programs, Eclipse is my environment.
The Sender...
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1" // 127.0.0.1
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd;
struct ip_mreq mreq;
char *message="Hello, World!";
int message_size = strlen(message) + 1;
// Create a UDP socket
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
perror("socket(...) ");
return -1;
}
// allow multiple sockets to use the same PORT number
u_int reuse_port = 1;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &reuse_port, sizeof(reuse_port)) < 0)
{
perror("setsockopt(...) ");
return -1;
}
// set up destination address
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(UDP_GROUP);
addr.sin_port = htons(UDP_PORT);
printf("Begin sendto(...) infinite loop\n");
while (true)
{
printf("Sending message: %s, of size: %d\n", message, message_size);
if (sendto(fd, message, message_size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("sendto(...): ");
return -1;
}
// printf("message sent: %s\n", message);
sleep(1);
}
return 1;
}
The Listener...
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1"
#define MAX_BUFFER_SIZE 256
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd, nbytes;
socklen_t addrlen;
struct ip_mreq mreq;
char msgbuf[MAX_BUFFER_SIZE];
u_int reuse_port = 1;
// Create a socket
fd = socket(AF_INET,SOCK_DGRAM,0);
if (fd < 0)
{
perror("create socket failed");
return -1;
}
// allow multiple sockets to use the same PORT number
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_port, sizeof(reuse_port)) < 0)
{
perror("Reusing port number failed");
return -1;
}
// set up destination address
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(UDP_PORT);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
return -1;
}
// Set the recvfrom timeout after 1 s
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
{
perror("Error setting recvfrom timeout\n");
return -1;
}
// use setsockopt() to request that the kernel join a multicast group
mreq.imr_multiaddr.s_addr = inet_addr(UDP_GROUP);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
perror("setsockopt");
return -1;
}
addrlen = sizeof(addr);
printf("Begin recvfrom(...) infinite loop\n");
while (true)
{
nbytes = recvfrom(fd, msgbuf, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if (nbytes < 0)
{
printf("recvfrom timeout\n");
}
else
{
printf("message received: %s\n", msgbuf);
}
}
return 1;
}
Every second, the sender program printf's "Sending message: Hello, World!, of size: 14" and every two seconds the receiver printf's "recvfrom timeout". I have set Wireshark to look at UDP traffic and I definitely see the sento data. The recvfrom is not getting any data. I have tried using many different Group IP's from 255.0.0.0 to 239.255.255.255, no change. I have tried many different ports, no change. Is their a special setup I need to do on my network card? I'm not sure what else to do. Small edit, the recvfrom and sendto message should not have "&".
I got trouble on using sendto() function in socket
int main(int argc, const char * argv[]) {
int acceptFd;
struct sockaddr_in servaddr, cliaddr;
char recieveBuf[512];
char sendBuf[512];
socklen_t cliLen;
if(-1 == (acceptFd = socket(AF_INET,SOCK_DGRAM,0))) perror("socket() failed.\n");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6789);
if(-1==bind(acceptFd,(struct sockaddr*)&servaddr,sizeof(servaddr))) perror("bind() failed.\n");
bzero(&cliaddr, sizeof(cliaddr));
while(1){
if(recvfrom(acceptFd, recieveBuf, 512, 0, (struct sockaddr*) &cliaddr, &cliLen) == -1) {
perror("recvfrom() failed");
continue;
}
strcpy(sendBuf,"recieved\n");
printf("%s\n",sendBuf);
if(-1 == sendto(acceptFd,sendBuf, 512,0,(struct sockaddr*)&cliaddr,cliLen)){
perror("sendto() failed");
continue;
}
}
}
the recvfrom() works fine, but every time sendto() was called, the error handling print out this: sendto() failed: Invalid argument
the send program is here:
#include "test.AcceptMessage.pb.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, const char * argv[]) {
int sendSocket;
struct sockaddr_in cliaddr;
char buf[512];
if(-1 == (sendSocket = socket(AF_INET,SOCK_DGRAM,0))) perror("socket() failed.\n");
bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
cliaddr.sin_port = htons(6789);
sendto(sendSocket,buf, 512,0,(
struct sockaddr*)&cliaddr, sizeof(cliaddr));
recvfrom(sendSocket,buf,512,0, nullptr, nullptr);
printf("%s\n",buf);
return 0;
}
So what's wrong with this code?
From man recvfrom:
Before the call, it [addrlen] should be initialized to the size of the
buffer associated with src_addr.
Therefore, initialize your cliLen variable with:
socklen_t cliLen = sizeof(cliaddr);
You don't have any reason to use recvfrom() and sendto() at all here. It's a connected socket. Just use send() and recv().
I started making a C++ server, but I can't bind to socket.
#pragma once
#include <WinSock2.h>
#include <thread>
#include "Logging.h"
namespace network
{
static SOCKET sock;
static VOID startAccept()
{
while (true)
{
struct sockaddr_in serv_addr, cli_addr;
int clilen = sizeof(cli_addr);
SOCKET accepted;
if (accepted == NULL)
{
accepted = accept(sock, (struct sockaddr *) &cli_addr, &clilen);
if (accepted < 0)
{
core::writeln("Error accept: " + WSAGetLastError());
}
else
{
core::writeln("New connection from " + cli_addr.sin_addr.S_un.S_addr);
}
}
}
}
static VOID connect(const char* ipAddress, u_short port)
{
struct sockaddr_in serv_addr, cli_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0)
{
core::writeln("Error creating socket: " );
perror("error:");
return;
}
else if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
core::writeln("Error binding to socket: " );
perror("error:");
return;
}
else if (listen(sock, 10) < 0)
{
core::writeln("Error listening socket: ");
return;
}
else
{
core::writeln("Bound WinSock to " + serv_addr.sin_addr.S_un.S_addr);
std::thread accepting(startAccept);
accepting.join();
}
}
};
In my main int:
int _tmain(int argc, _TCHAR* argv[])
{
connect("127.0.0.1", 500);
}
But everytime I try to bind, I get the following output:
Error binding to socket:
error:No Error
But the socket isn't bound on. What am I doing wrong?
FIXED, had to use WSAStartup.
When I'm trying to connect to the server with only 1 client, the recv() function on the server does not delay.
But when I'm starting the client console more then 1 time (something like 7 times), there is a delay of something like 2000ms after you send to the server packet with the function send() until the server will print the packet in is console.
Is there any solution without starting a thread for each client? (Windows limits the number of threads for each process).
The code is compiled with Visual Studio 2008, and this is the full server code:
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#include <Windows.h>
#include <stdio.h>
struct sslv3
{
#define max_clients 1024
private:
int cClient;
public:
SOCKET fd;
int CurrentClient()
{
return cClient;
}
struct client
{
client()
{
Valid = false;
}
bool Valid;
DWORD ip;
WORD port;
char ipstr[33];
char portstr[33];
SOCKET fd;
void StrGen()
{
wsprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip & 0xFF00)/0x100, (ip & 0xFF0000)/0x10000, (ip & 0xFF000000)/0x1000000);
wsprintf(portstr, "%d", port);
}
} clients[max_clients];
//
sslv3(bool server_client)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
cClient = 0;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//
DWORD timeout = 1;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
}
int Bind(WORD port)
{
int ret = 0;
sockaddr_in local;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(port);
if((ret = bind(fd, (struct sockaddr *)&local, sizeof(local)))
!= SOCKET_ERROR)
listen(fd, SOMAXCONN);
return ret;
}
int Accept()
{
SOCKET clientfd;
sockaddr_in client;
int addrlen = sizeof(client);
clientfd = accept(fd, (struct sockaddr *)&client, &addrlen);
if(clientfd == -1)
return -1;
clients[cClient].ip = client.sin_addr.S_un.S_addr;
clients[cClient].port = client.sin_port;
clients[cClient].StrGen();
clients[cClient].fd = clientfd;
clients[cClient].Valid = true;
//
DWORD timeout = 1;
setsockopt(clients[cClient].fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
cClient++;
if(cClient >= max_clients)
{
cClient = 0;
return max_clients - 1;
}
return cClient - 1;
}
int Connect(char ip[], WORD port)
{
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
return connect(fd, (const struct sockaddr*)&addr, sizeof(addr));
}
int Send(SOCKET sfd, void* buffer, int length)
{
return send(sfd, (char*)buffer, length, 0);
}
int Read(SOCKET sfd, void* buffer, int length)
{
return recv(sfd, (char*)buffer, length, 0);
}
};
sslv3 cssl(true);
DWORD WINAPI ReadThread(void* args)
{
while(true)
{
for(int j = 0; j <= cssl.CurrentClient(); j++)
{
if(cssl.clients[j].Valid)
{
char rpack[1024];
for(int i = 0; i < sizeof(rpack); i++)
rpack[i] = 0;
if(cssl.Read(cssl.clients[j].fd, rpack, sizeof(rpack)) > 0){
printf("%s:%s says: %s\n", cssl.clients[j].ipstr, cssl.clients[j].portstr, rpack);
}
}
}
Sleep(1);
}
return TRUE;
}
int main()
{
cssl.Bind(1234);
CreateThread(0,0,ReadThread,0,0,0);
while(true)
{
Sleep(1);
int cid = cssl.Accept();
if(cid != -1){
printf("%s:%s connected!\n", cssl.clients[cid].ipstr, cssl.clients[cid].portstr);
}
}
return 0;
}
The following is a full client code:
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
struct sslv3
{
#define max_clients 1024
private:
int cClient;
public:
SOCKET fd;
int CurrentClient()
{
return cClient;
}
struct client
{
client()
{
Valid = false;
}
bool Valid;
DWORD ip;
WORD port;
char ipstr[33];
char portstr[33];
SOCKET fd;
void StrGen()
{
wsprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip & 0xFF00)/0x100, (ip & 0xFF0000)/0x10000, (ip & 0xFF000000)/0x1000000);
wsprintf(portstr, "%d", port);
}
} clients[max_clients];
//
sslv3(bool server_client)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
cClient = 0;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//
DWORD timeout = 1;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
}
int Bind(WORD port)
{
int ret = 0;
sockaddr_in local;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(port);
if((ret = bind(fd, (struct sockaddr *)&local, sizeof(local)))
!= SOCKET_ERROR)
listen(fd, SOMAXCONN);
return ret;
}
int Accept()
{
SOCKET clientfd;
sockaddr_in client;
int addrlen = sizeof(client);
clientfd = accept(fd, (struct sockaddr *)&client, &addrlen);
if(clientfd == -1)
return -1;
clients[cClient].ip = client.sin_addr.S_un.S_addr;
clients[cClient].port = client.sin_port;
clients[cClient].StrGen();
clients[cClient].fd = clientfd;
clients[cClient].Valid = true;
//
DWORD timeout = 1;
setsockopt(clients[cClient].fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
cClient++;
if(cClient >= max_clients)
{
cClient = 0;
return max_clients - 1;
}
return cClient - 1;
}
int Connect(char ip[], WORD port)
{
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
return connect(fd, (const struct sockaddr*)&addr, sizeof(addr));
}
int Send(SOCKET sfd, void* buffer, int length)
{
return send(sfd, (char*)buffer, length, 0);
}
int Read(SOCKET sfd, void* buffer, int length)
{
return recv(sfd, (char*)buffer, length, 0);
}
};
sslv3 cssl(false);
int main()
{
cssl.Connect("127.0.0.1", 1234);
while(true)
{
printf("say: ");
char buf[1024];
for(int i = 0; i < sizeof(buf); i++)
buf[i] = 0;
cin >> buf;
int len = strlen(buf);
cssl.Send(cssl.fd, buf, len);
}
return 0;
}
The server seems 'idle' for 2 seconds, because some clients are handled after 2 sleeps, 1 second each.
This is clearly not the right way to handle more than one client on a server. You may want to check on select() - reference.
A very good tutorial for socket programming is Beej's