good evening, I am a newbie in using linux sockets under the c programming language.
for now I'm trying to make a client communicate with a server. basically the thing i'm stuck at is making the server accept a connection from the client and print the numbers after it receives them. here is the source code for the server so far:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0, n = 0;
struct sockaddr_in serv_addr;
char sendBuff[1024], recvBuff[1024];
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
printf("in first while loop\n");
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
while( (n = read(listenfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
printf("in second while loop\n");
recvBuff[n] = 0;
fputs(recvBuff, stdout);
}
}
}
I didn't post the client code since I don't think it's relevant. I have made a server which sends something to a client and it worked just fine. but I'm seriously confused by how I'm supposed to make the server accept input from the client. thank you in advance.
while( (n = read(connfd, recvBuff, sizeof(recvBuff)-1)) > 0)