I'm connected to certain client then we're communicating. Another client connects to my server which will instantly output that there's a new connection.. e.g Accepting while listening to other clients?.. How can i do that?..
I have already a working one but it only does one thing at a time. When i'm already communicating with my client i can't accept incoming connection or data to other clients. I can only accept new connection or data from other clients when the client i'm communicating is disconnected. how can i do so that it can process both accept and listen to client at the same time. I don't want to use threads.
Here's a part of my code.
do
{
fduse = fdin;
printf("Waiting for Connection\n");
err = select(sMax + 1, &fduse, NULL, NULL, NULL);
if (err < 0)
{
perror(" select() failed");
break;
}
DescRead = err;
for (SockStorage = 0; SockStorage <= sMax && DescRead > 0; ++SockStorage)
{
if (FD_ISSET(SockStorage, &fduse))
{
DescRead -= 1;
if (SockStorage == socketFd)
{
printf(" Listening socket is readable\n");
do
{
NewSFD =
accept(socketFd, (struct sockaddr *)&cli_addr, &clilen);
if (NewSFD < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" accept() failed");
DCSERVER = TRUE;
}
break;
}
if (ClientCount < MAX_CLIENTS)
{
for (loop = 0; loop < MAX_CLIENTS; loop++)
{
if (Clients[loop].connected_sock < 0)
{
Clients[loop].connected_sock = NewSFD;
break;
}
}
ClientCount++;
}
else
{
printf("Maximum Client Reached.\n");
char *sendtoclient = "Server full. ";
send(NewSFD, sendtoclient, strlen(sendtoclient), 0);
close(NewSFD);
break;
}
ip = ntohl(cli_addr.sin_addr.s_addr);
printf(" Connection from %d.%d.%d.%d\n",
(int)(ip >> 24) & 0xff,
(int)(ip >> 16) & 0xff,
(int)(ip >> 8) & 0xff, (int)(ip >> 0) & 0xff);
dlogs(ip);
FD_SET(NewSFD, &fdin);
if (NewSFD > sMax)
sMax = NewSFD;
}
while (NewSFD != -1);
}
else
{
int d;
for (d = 0; d < MAX_CLIENTS; d++)
{
printf("Descriptor ID: %d\n", Clients[d].connected_sock);
}
pfds[0].fd = fd;
pfds[0].events = POLLIN;
pfds[1].fd = SockStorage;
pfds[1].events = POLLIN;
state = FALSE;
do
{
rc = poll(pfds, 2, -1);
if (pfds[0].revents & POLLIN)
{
while ((nbytes = read(fd, buf, sizeof (buf) - 1)) > 0)
{
buf[nbytes] = '\0';
printf("%s\n", buf);
}
pfds[0].events = 0;
pfds[1].events = POLLIN | POLLOUT;
}
if (pfds[1].revents & POLLIN)
{
err = recv(SockStorage, strbuf, sizeof (strbuf), 0);
if (err < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" recv() failed");
state = TRUE;
}
break;
}
if (err == 0)
{
printf(" Connection closed\n");
state = TRUE;
break;
}
dSize = err;
printf(" %d bytes received\n", dSize);
}
if (pfds[1].revents & POLLOUT)
{
int s;
for (s = 0; s < MAX_CLIENTS; s++)
{
if (Clients[s].connected_sock > 0)
{
err =
send(Clients[s].connected_sock, buf,
strlen(buf), 0);
if (err < 0)
{
perror(" send() failed");
track = s;
state = TRUE;
break;
}
}
}
pfds[0].events = POLLIN;
pfds[1].events = POLLIN;
}
}
while (TRUE);
fopen("/sockF.txt", "w");
if (state)
{
ClientCount--;
close(SockStorage);
FD_CLR(SockStorage, &fdin);
if (SockStorage == sMax)
{
while (FD_ISSET(sMax, &fdin) == FALSE)
sMax -= 1;
}
}
}
}
}
} while (DCSERVER == FALSE);
cleanUP(SockStorage, sMax);
}
I've been working on it for two days and still i can't get it. thanks..
Well, to give you an example of a working multi-connection daemon, have a look at this example
If you need a further explanation to go with it, shoot.
Note, the code in that example has been stripped of things like daemon_init(), etc, so just copy-pasting blindly and trying to compile it won't work.
Added setnonblocking() as per request.
void setnonblocking(sock)
int sock;
{
int opts;
opts = fcntl(sock, F_GETFL);
if (opts < 0)
{
syslog(LOG_ERR, "fcntl(F_GETFL) failed");
exit(EXIT_FAILURE);
}
opts = (opts | O_NONBLOCK);
if (fcntl(sock, F_SETFL, opts) < 0)
{
syslog(LOG_ERR, "fcntl(F_SETFL) failed");
exit(EXIT_FAILURE);
}
return;
}
And daemon_init()
int daemon_init(void)
{
pid_t pid;
int i;
if ((pid = fork()) < 0)
{
return -1;
}
else if (pid != 0)
{
exit(0);
}
for (i=getdtablesize();i>=0;--i) close(i);
i = open("/dev/null", O_RDWR); /* open stdin */
dup(i); /* stdout */
dup(i); /* stderr */
setsid();
return 0;
}
Related
I wrote this C code that uses the libssh2 library to connect to a ssh server and get the output of a command executed on the server.
Currently the code works fine, it's connecting to the ssh server and obtains the output of executed command from custom_command variable.
The problem is that I want to make it obtain the output of two commands, for example to obtain the output of custom_command and custom_command2 variables in the same libssh2 channel.
I tried with this but it doesen't work
while((rc = libssh2_channel_exec(channel, custom_command)) == LIBSSH2_ERROR_EAGAIN)
{
waitsocket(s_sock, session);
}
while((rc = libssh2_channel_exec(channel, custom_command2)) == LIBSSH2_ERROR_EAGAIN)
{
waitsocket(s_sock, session);
}
Does anyone know how I can modify this code to work as I said above?
//COMPILE COMMAND: gcc program.c -o program -lssh2
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include "libssh2_config.h"
#include <libssh2.h>
static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
{
struct timeval timeout;
int rc, dir;
fd_set fd;
fd_set *writefd = NULL;
fd_set *readfd = NULL;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
FD_ZERO(&fd);
FD_SET(socket_fd, &fd);
dir = libssh2_session_block_directions(session);
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
{
readfd = &fd;
}
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
{
writefd = &fd;
}
rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
return rc;
}
int main()
{
//LOGIN CREDENTIALS
char *hostname = "127.0.0.1";
char *username = "vboxuser";
char *password = "changeme";
char *custom_command = "uptime";
char *custom_command2 = "hostname"
int port = 22;
int s_sock;
struct sockaddr_in addr;
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel;
int rc, exitcode;
rc = libssh2_init(0);
if(rc != 0)
{
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}
s_sock = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr = inet_addr(hostname);
if(setsockopt(s_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
perror("setsockopt failed\n");
}
if(setsockopt(s_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
perror("setsockopt failed\n");
}
if(connect(s_sock, (struct sockaddr *)(&addr), sizeof(struct sockaddr_in)) != 0)
{
return -1;
}
session = libssh2_session_init();
if(!session)
{
return -1;
}
while((rc = libssh2_session_handshake(session, s_sock)) == LIBSSH2_ERROR_EAGAIN);
if(rc)
{
return -1;
}
while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN);
if(rc)
{
goto shutdown;
}
while((channel = libssh2_channel_open_session(session)) == NULL && libssh2_session_last_error(session, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN )
{
waitsocket(s_sock, session);
}
if(channel == NULL)
{
goto shutdown;
}
while((rc = libssh2_channel_exec(channel, custom_command)) == LIBSSH2_ERROR_EAGAIN)
{
waitsocket(s_sock, session);
}
if(rc != 0)
{
goto shutdown;
}
for(;;)
{
int rc;
do
{
char command_output[65500];
rc = libssh2_channel_read(channel, command_output, sizeof(command_output));
if(rc > 0)
{
fprintf(stderr, "\nSuccessfully Login!\n \nuser:%s \npassword:%s \nhost:%s \nport:%d\n", username, password, hostname, port);
fprintf(stderr, "\nCommand output: %s\n", command_output);
goto shutdown;
}
else
{
if(rc != LIBSSH2_ERROR_EAGAIN)
{
goto shutdown;
}
}
}
while(rc > 0);
if(rc == LIBSSH2_ERROR_EAGAIN)
{
waitsocket(s_sock, session);
}
else
{
break;
}
}
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
{
waitsocket(s_sock, session);
}
exitcode = 127;
if(rc == 0)
{
exitcode = libssh2_channel_get_exit_status(channel);
}
libssh2_channel_free(channel);
close(s_sock);
channel = NULL;
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
libssh2_exit();
exit(0);
shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
close(s_sock);
libssh2_exit();
return 0;
}
I can't seem to print what I enter in my client. I have compiled both without any error. Can't figure out why it isn't printing the desired output.
Q. What is wrong with my code ? How can I print a string with whitespaces through the client in the server.
Here is the code for the server.
/* Server Side program */
int sid,nid;
struct sockaddr_in q;
char x[100];
int len=sizeof(struct sockaddr_in);
sid=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
q.sin_family=PF_INET;
q.sin_port=1600;
q.sin_addr.s_addr=0;
bind(sid,&q,len);
listen(sid,20);
while(1)
{
memset(x,0,sizeof(x));
nid=accept(sid,&q,&len);
read(nid,x,100);
printf("%s\n",x);
}
Here is the code for the client.
/*client side program*/
int sid,status;
struct sockaddr_in q;
int len=sizeof(struct sockaddr_in);
char x[100];
sid=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
q.sin_family=PF_INET;
q.sin_port=1600;
q.sin_addr.s_addr=0;
status=connect(sid,&q,len);
if(status==-1)
{
printf("Connection failure");
exit(0);
}
while(1)
{
printf("Enter string to send : ");
scanf("%[^\n]s",x);
write(sid,x,strlen(x));
if(strcmp(x,"bye")==0)
break;
}
There is all kinds of problems with your code. Lack of error handling. Incorrect use of printf() and scanf(). Resource leaks.
Try something more like this instead:
Server:
int main()
{
int sid, nid;
struct sockaddr_in q;
char x[100];
socklen_t qlen;
ssize_t xlen;
sid = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sid < 0)
{
perror("socket failure");
return -1;
}
q.sin_family = AF_INET;
q.sin_port = htons(1600);
q.sin_addr.s_addr = INADDR_ANY;
if (bind(sid, &q, sizeof(q)) < 0)
{
perror("bind failure");
return -1;
}
if (listen(sid, 20) < 0)
{
perror("listen failure");
return -1;
}
while(1)
{
qlen = sizeof(q);
nid = accept(sid, &q, &qlen);
if (nid < 0)
{
perror("accept failure");
return -1;
}
while ((xlen = read(nid, x, sizeof(x))) > 0)
{
printf("Received: '%.*s'\n", xlen, x);
}
if (xlen < 0)
perror("read failure");
else
printf("client disconnected\n");
close(nid);
}
return 0;
}
Client:
int main()
{
int sid, xlen;
struct sockaddr_in q;
char x[100];
sid = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sid < 0)
{
perror("socket failure");
return -1;
}
q.sin_family = AF_INET;
q.sin_port = htons(1600);
q.sin_addr.s_addr = inet_addr("server ip here"); // or INADDR_LOOPBACK (127.0.0.1)
if (connect(sid, &q, sizeof(q)) < 0)
{
printf("connect failure");
return -1;
}
while(1)
{
printf("Enter string to send : ");
if (!fgets(x, sizeof(x), stdin))
break;
xlen = strlen(x);
if ((xlen > 0) && (x[xlen-1] == '\n'))
{
x[xlen-1] = '\0';
--xlen;
}
if (write(sid, x, xlen) < 0)
{
perror("write failure");
break;
}
if (strcmp(x, "bye") == 0)
break;
}
close(sid);
return 0;
}
i don't know why the select function don't return any result.
I put the server in execution with this command:
./server 5555
and i try to send any character from another terminal, using:
nc 127.0.0.1 5555
and
any string to send
This is the source code of the server.c
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#define MAXSIZE 1000
int main(int argc, char **argv) {
int socketfd, connectedfd, maxfd;
int ris, i, len, ris1, sent, opt;
struct sockaddr_in serverS, clientS;
short int localServerPort;
fd_set rdfs, wrfs;
char buffer[MAXSIZE];
if (argc != 2) {
printf("necessario un parametro PORTNUMBER");
exit(1);
} else {
localServerPort = atoi(argv[1]);
}
socketfd = socket(AF_INET, SOCK_STREAM, 0);
if (socketfd < 0) {
printf("socket() error\n");
exit(1);
}
opt = 1;
ris = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
if (ris < 0) {
printf ("setsockopt() SO_REUSEADDR failed, Err: %d \"%s\"\n", errno,strerror(errno));
exit(1);
}
memset(&serverS, 0, sizeof(serverS));
serverS.sin_family = AF_INET;
serverS.sin_addr.s_addr = htonl(INADDR_ANY);
serverS.sin_port = htons(localServerPort);
ris = bind(socketfd, (struct sockaddr*)&serverS, sizeof(serverS));
if (ris < 0) {
printf("bind() error");
exit(1);
}
ris = listen(socketfd, 10);
if (ris < 0) {
printf("listen() error");
exit(1);
}
len = sizeof(clientS);
connectedfd = accept(socketfd, (struct sockaddr*)&clientS, &len);
if (connectedfd < 0) {
printf("accept() error");
exit(1);
} else {
printf("Connesso con: %s:%d - descrittore: %d\n", inet_ntoa(clientS.sin_addr), ntohs(clientS.sin_port), connectedfd);
}
fd_set tempset;
FD_ZERO(&rdfs);
//FD_ZERO(&wrfs);
FD_SET(connectedfd, &rdfs);
maxfd = connectedfd + 1;
while (1) {
printf("Select...\n");
ris = select(connectedfd, &rdfs, NULL, NULL, NULL);
printf("test\n");
if (ris == 0) {
printf("select() timeout error\n");
} else if (ris < 0 && errno != EINTR) {
printf("select() error\n");
} else if (ris > 0) {
printf("test ris > 0\n");
for (i = 0; i < maxfd+1; i++) {
if (FD_ISSET(i, &rdfs)) {
do {
ris = recv(i, buffer, MAXSIZE, 0);
} while (ris == -1 && errno == EINTR);
if (ris > 0) {
buffer[ris] = 0;
printf("Echoing: %s\n", buffer);
sent = 0;
do {
ris1 = send(i, buffer+sent, ris-sent, MSG_NOSIGNAL);
if (ris1 > 0)
sent += ris1;
else if (ris1 < 0 && errno != EINTR);
break;
} while (ris > sent);
}
else if (ris == 0) {
close(i);
FD_CLR(i, &rdfs);
}
else {
printf("Error in recv(): %s\n", strerror(errno));
}
}
}
}
}
return(0);
}
Why the execution remain locked at "Select...\n"?
Thanks to all!
From http://linux.die.net/man/2/select, the first param passed should be the highest-numbered file descriptor in any of the three sets (read/write/exception), plus 1. I would try the following:
ris = select(connectedfd+1, &rdfs, NULL, NULL, NULL);
int CreateSocket()
{
//pthread_attr_t attr;
// Socket creation for UDP
acceptSocket = socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket == -1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
fcntl(acceptSocket, O_NONBLOCK);
if(rc== -1)
{
printf("Oh dear, something went wrong with bind()! %s\n", strerror(errno));
return -1;
}
else
{
printf("Socket an port %d \n",port);
}
return acceptSocket;
}
int create_timer (void)
{
timer_t timer_id;
int timerfd = timer_create (CLOCK_REALTIME, 0, &timer_id);
if (timerfd < 0) {
perror ("timerfd_create:");
return -1;
}
return timerfd;
}
int start_timer_msec (int fd, int msec)
{
struct itimerspec timspec;
memset (&timspec, 0, sizeof(timspec));
timspec.it_value.tv_sec = 0;
timspec.it_value.tv_nsec = msec * 1000 * 1000;
if (timer_settime(fd, 0, &timspec, NULL) < 0) {
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
Xcp_Initialize();
int fd_2ms = create_timer();
int fd_10ms = create_timer();
int fd_100ms = create_timer();
int rval = 0;
if ((fd_2ms < 0) || (fd_10ms < 0) || (fd_100ms < 0)) {
exit (1);
}
if ((sock = CreateSocket()) < 0) {
perror ("Create_socket");
// fclose (fp);
exit (1);
}
start_timer_msec (fd_2ms, 2);
start_timer_msec (fd_10ms, 10);
start_timer_msec (fd_100ms, 100);
do
{
socklen_t len;
int max_fd = 0;
fd_set rdfds;
FD_ZERO (&rdfds);
GET_MAX_FD (max_fd, sock);
FD_SET (sock, &rdfds);
GET_MAX_FD (max_fd, fd_2ms);
FD_SET (fd_2ms, &rdfds);
GET_MAX_FD (max_fd, fd_10ms);
FD_SET (fd_10ms, &rdfds);
GET_MAX_FD (max_fd, fd_100ms);
FD_SET (fd_100ms, &rdfds);
rval = select (max_fd, &rdfds, NULL, NULL, NULL);
if (rval < 0) {
if (errno == EINTR)
continue;
} else if (rval > 0) {
/*Process CLI*/
if ((FD_ISSET (sock, &rdfds)))
{
FD_CLR(sock, &rdfds);
len = sizeof(client);
printf("NEW DATA ARRIVED\n");
//non blocking mode : MSG_DONTWAIT
rc=recvfrom(sock, buf, 256, 0, (struct sockaddr*) &client, &len);
//I am calculating the time here
//InterruptTime = GetTimeStamp();
//measurements[17] = InterruptTime;
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
if (errno == SIGINT)
continue;
printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
break;
}
ConfigureISR( );
XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
}
if ((FD_ISSET (fd_2ms, &rdfds))) {
FD_CLR(fd_2ms, &rdfds);
TASK1(Task2ms_Raster);
start_timer_msec (fd_2ms, 2);
}
if ((FD_ISSET (fd_10ms, &rdfds))) {
FD_CLR(fd_10ms, &rdfds);
TASK2(Task10ms_Raster);
start_timer_msec (fd_10ms, 10);
}
if ((FD_ISSET (fd_100ms, &rdfds))) {
FD_CLR(fd_100ms, &rdfds);
TASK3(Task100ms_Raster);
start_timer_msec (fd_100ms, 100);
}
}
} while (1);
close(sock);
//fclose (fp);
return 0;
}
I created a socket to receive data from client via the ip address and port number. I created a timer to call the task for every 2ms, 10ms and 100ms. The above code is working fine for linux. But how to convert the above code for QNX RTOS. What are the changes should I make for QNX rtos. could some one please help me ??
IF I use the same code in qnx then it is crashing at int fd_2ms = create_timer(); !!
I have this server app where clients can connect to. Now i want that when the clients are connected i can send data to all of them. I manage to do it when i connect my two clients.. what i send is received by my two clients. but my problem is when i connect client1 then send data to server, client1 can receive data then i connect client2 and i send data to the server. now when i send data from server to my clients only client1 can receive data but when i disconnect client 1 then client2 can receive data from the server.
How can i make them work at the same time?.. Also how can i make my server accept messages at the same time from my client?..
Here's the part of the code im having trouble.
for(j=0;j<MAX_CLIENTS; j++)
Clients[j].connected_sock = -1;
do
{
fduse = fdin;
printf("Waiting for Connection\n");
err = select(sMax + 1, &fduse, NULL, NULL, NULL);
if (err < 0)
{
perror(" select() failed");
break;
}
DescRead = err;
for (SockStorage=0; SockStorage <= sMax && DescRead > 0; ++SockStorage)
{
if (FD_ISSET(SockStorage, &fduse))
{
DescRead -= 1;
if (SockStorage == socketFd)
{
printf(" Listening socket is readable\n");
do
{
NewSFD = accept(socketFd,(struct sockaddr *) &cli_addr, &clilen);
if (NewSFD < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" accept() failed");
DCSERVER = TRUE;
}
break;
}
if(ClientCount < MAX_CLIENTS){
for(loop = 0; loop <MAX_CLIENTS; loop++){
if(Clients[loop].connected_sock<0){
Clients[loop].connected_sock = NewSFD;
break;
}
}
ClientCount++;
}
else
{
printf("Maximum Client Reached.\n");
char *sendtoclient = "Server full. ";
send(NewSFD, sendtoclient, strlen(sendtoclient),0);
close(NewSFD);
break;
}
ip = ntohl(cli_addr.sin_addr.s_addr);
printf(" Connection from %d.%d.%d.%d\n",
(int)(ip>>24)&0xff,
(int)(ip>>16)&0xff,
(int)(ip>>8)&0xff,
(int)(ip>>0)&0xff);
dlogs(ip);
FD_SET(NewSFD, &fdin);
if (NewSFD > sMax)
sMax = NewSFD;
} while (NewSFD != -1);
}
else
{
int d;
for(d=0; d<MAX_CLIENTS; d++){
printf("Descriptor ID: %d\n", Clients[d].connected_sock);
}
pfds[0].fd = fd;
pfds[0].events = POLLIN;
pfds[1].fd = SockStorage;
pfds[1].events = POLLIN;
state = FALSE;
do
{
rc = poll(pfds, 2, -1);
if (pfds[0].revents & POLLIN)
{
while ((nbytes = read(fd, buf, sizeof(buf)-1)) > 0)
{
buf[nbytes] = '\0';
printf("%s\n", buf);
}
pfds[0].events = 0;
pfds[1].events = POLLIN | POLLOUT;
}
if (pfds[1].revents & POLLIN)
{
err = recv(SockStorage, strbuf, sizeof(strbuf), 0);
if (err < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" recv() failed");
state = TRUE;
}
break;
}
if (err == 0)
{
printf(" Connection closed\n");
state = TRUE;
break;
}
dSize = err;
printf(" %d bytes received\n", dSize);
}
if (pfds[1].revents & POLLOUT)
{
int s;
for(s=0; s<MAX_CLIENTS; s++){
if(Clients[s].connected_sock>0){
err = send(Clients[s].connected_sock, buf, strlen(buf), 0);
if (err < 0)
{
perror(" send() failed");
state = TRUE;
break;
}
}
}
pfds[0].events = POLLIN;
pfds[1].events = POLLIN;
}
} while (TRUE);
Here's how im sending data to my clients.
int s;
for(s=0; s<MAX_CLIENTS; s++){
if(Clients[s].connected_sock>0){
err = send(Clients[s].connected_sock, buf, strlen(buf), 0);
if (err < 0)
{
perror(" send() failed");
state = TRUE;
break;
}
}
}
Thanks,
There are generally couple of possible solutions:
You can use separate threads for each client connection;
You can use select;
You can use poll;
You can use epoll;
In Windows environment there are also some other possibilities like IOCP for example.
Each of mentioned solutions have some pros and cons (you need generally distinguish between simplicity and performance).
You can read some network programming tutorial for more details, e.g. this.