I’m learning socket programming and i want when that clients are connected to my server. I can send data to them with their specific address..
e.g
Server —> client 1
╚—> client 2
╚—> client 3
Connection from: 192.168.5.3
Connection from: 192.168.5.10
Connection from: 192.168.5.15
For example say client 1 sends data to me and then client 2 also sends data to me i want to reply back only to client 1 how could i possibly to do that?..
How will i store my client so when i need to send data to them i know which client will i send data to?
Here’s my Code.
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#define TRUE 1
#define FALSE 0
typedef struct SERVER_FD{
int sPort;
int serverFD;
int smaxFD;
int newFD;
}sSD;
pid_t pid, sid;
int cFD,
dSize,
err,
start = 1,
state,
DescRead,
DCSERVER = FALSE;
int fd;
char buf[255];
int nbytes;
struct sockaddr_in addr, cli_addr;
unsigned long ip;
char strbuf[256];
socklen_t clilen;
fd_set fdin, fduse;
struct pollfd pfds[2];
int rc;
void process(int ServerFD, int Port, int sMax, int NewSFD);
void cleanUP(int i, int max);
void dlogs(unsigned long ip);
main (int argc, char *argv[])
{
sSD link;
sSD *sCon;
sCon = &link;
sCon->sPort = 53234;
fd = open("/tmp/myFIFO", O_RDWR);
if(fd == -1) {
printf("Could not open the pipe\n");
}
fcntl(fd, F_SETFL,
fcntl(fd, F_GETFL) |
O_NONBLOCK);
sCon->serverFD = socket(AF_INET, SOCK_STREAM, 0);
if (sCon->serverFD != -1)
{
err = setsockopt(sCon->serverFD, SOL_SOCKET, SO_REUSEADDR,(char *)&start, sizeof(start));
if (err != -1)
{
err = ioctl(sCon->serverFD, FIONBIO, (char *)&start);
if (err != -1){
process(sCon->serverFD,sCon->sPort,sCon->smaxFD,sCon->newFD);
}
else{
perror("ioctl() failed");
close(sCon->serverFD);
exit(EXIT_FAILURE);
}
}
else{
perror("setsockopt() failed");
close(sCon->serverFD);
exit(EXIT_FAILURE);
}
}
else{
perror("FAILED CONNECTING TO SOCKET");
exit(EXIT_FAILURE);
}
}
void process(int ServerFD, int Port, int sMax, int NewSFD){
bzero((char *) &addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = htons(Port);
err = bind(ServerFD,(struct sockaddr *)&addr, sizeof(addr));
if (err < 0)
{
perror("bind() failed");
close(ServerFD);
exit(EXIT_FAILURE);
}
err = listen(ServerFD, 32);
if (err < 0)
{
perror("listen() failed");
close(ServerFD);
exit(EXIT_FAILURE);
}
clilen = sizeof(cli_addr);
FD_ZERO(&fdin);
sMax = ServerFD;
FD_SET(ServerFD, &fdin);
do
{
fduse = fdin;
//printf("Waiting on select()...\n");
err = select(sMax + 1, &fduse, NULL, NULL, NULL);
if (err < 0)
{
perror(" select() failed");
break;
}
DescRead = err;
for (cFD=0; cFD <= sMax && DescRead > 0; ++cFD)
{
if (FD_ISSET(cFD, &fduse))
{
DescRead -= 1;
if (cFD == ServerFD)
{
//printf(" Listening socket is readable\n");
do
{
NewSFD = accept(ServerFD,(struct sockaddr *) &cli_addr, &clilen);
if (NewSFD < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" accept() failed");
DCSERVER = TRUE;
}
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
{
//printf(" Descriptor %d is readable\n", cFD);
pfds[0].fd = fd;
pfds[0].events = POLLIN;
pfds[1].fd = cFD;
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(cFD, 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)
{
err = send(cFD, buf, strlen(buf), 0);
if (err < 0)
{
perror(" send() failed");
state = TRUE;
break;
}
pfds[0].events = POLLIN;
pfds[1].events = POLLIN;
}
} while (TRUE);
fopen("/sockF.txt","w");
if (state)
{
close(cFD);
FD_CLR(cFD, &fdin);
if (cFD == sMax)
{
while (FD_ISSET(sMax, &fdin) == FALSE)
sMax -= 1;
}
}
}
}
}
} while (DCSERVER == FALSE);
cleanUP(cFD, sMax);
}
void cleanUP(int i, int max){
for (i=0; i <= max; ++i)
{
if (FD_ISSET(i, &fdin))
close(i);
}
}
void dlogs(unsigned long ip){
FILE* pFile = fopen("/sockF.txt", "w+");
fprintf(pFile,"Connection from: %d.%d.%d.%d",
(int)(ip>>24)&0xff,
(int)(ip>>16)&0xff,
(int)(ip>>8)&0xff,
(int)(ip>>0)&0xff);
fclose(pFile);
}
Clients can connect to me but i don’t know which of them to send data when both of them are connected… I can send data when only 1 client is connected to my server but how will i send data to another client when 2 of my client is connected at the same time?
Thanks,
First – i would say it is not well written. But since you are learning you need to understand which piece of code is doing what.
NewSFD is the resultant new connected descriptor/socket to only the client which sent you the message. If you do a write/send on this descriptor/socket, it will be received by the sending client.
If you want to do it at some point latter, you need to save this socket in some struct and access it to write to the specific client.
cli_addr would actually return the client info such as port and ip, which could be used to create a new socket latter. However, you are using tcp which requires a handshake through connect and accept calls. So using the saved connected socket is the appropriate choice.
You can use a struct like this
client_fds Clients[MAX];
and latter retrieve socket to write from it