Linux network programming using C - c

I have write a programming about transfer files from server to client.But problem is that I can only transfer 2G files if file is bigger than 2G ,only 2G transfer successfully.
client
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAXLINE 1000
#define SA struct sockaddr
typedef struct
{
int oft;
int len;
char data[MAXLINE];
}Mem;
Mem Dat;
int main(int argc,char *argv[])
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
int complete;
struct sockaddr_in cli;
ssize_t recv_l,send_l;
size_t block_len;
socklen_t len;
char buf[MAXLINE];
char filename[MAXLINE];
FILE *fp;
bzero(&cli,sizeof(cli));
cli.sin_family = AF_INET;
if (argv[1] == NULL)
argv[1] = "172.16.42.22";
cli.sin_port = htons(5001);
inet_pton(AF_INET,argv[1],&cli.sin_addr.s_addr);
bind(sockfd,(struct sockaddr*)&cli,sizeof(cli));
if (connect(sockfd,(SA*)&cli,sizeof(cli)) == -1)
{
perror("connect");
close(sockfd);
exit(1);
}
//接收文件名列表
while((recv_l = recv(sockfd,buf,sizeof(buf),0)) > 0)
{
buf[recv_l] = '\0';
fputs(buf,stdout);
if(strcmp(buf+recv_l-5,"name\n") == 0)
break;
}
// exit(0);
bzero(filename,sizeof(filename));
while(fgets(filename,sizeof(filename),stdin) != NULL)
// while(gets(filename) != NULL)
{
int i = 0;
for(i = strlen(filename) - 1;i >= 0;i --)
if(filename[i] == '\n'){
filename[i] = '\0';
break;
}
send_l = send(sockfd,filename, strlen(filename),0);
recv_l = recv(sockfd,buf, sizeof(buf),0);
buf[recv_l] = '\0';
puts(buf);
if (strcmp(buf,"success") == 0)
break;
fputs(buf,stdout);
}
puts("filename send success");
//接收文件
if ((fp = fopen(filename,"wb")) == NULL)
{
perror("fopen");
exit(1);
}
while((recv_l = recv(sockfd,&Dat,sizeof(Dat),MSG_WAITALL)))
{
if (recv_l < 0){
perror("recv");
exit(1);
}
fwrite(Dat.data,sizeof(char),Dat.len,fp);
}
puts("file receive success");
fclose(fp);
close(sockfd);
return 0;
}
server
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAXLINE 1000
#define SA struct sockaddr
typedef struct
{
int oft;
int len;
char data[MAXLINE];
}Mem;
Mem Dat;
long long sum = 0;
int sendall(int sockfd, void *buf, int *len)
{
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while(total < *len) {
n = send(sockfd, buf+total, bytesleft, 0);
sum += n;
if(sum >= 1073741824){
puts("1G");
sum = 0;
}
if (n != bytesleft) return -1;
if (n == -1) {
break; }
total += n;
bytesleft -= n;
}
*len = total; // return number actually sent here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
int main(int argc,char *argv[])
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
socklen_t len;
size_t block_len;
int complete;
ssize_t send_l,recv_l;
struct sockaddr_in serv,cli;
char buf[MAXLINE];
char filename[100];
FILE *fp;
bzero(&serv,sizeof(serv));
serv.sin_family = AF_INET;
if (argv[1] == NULL)
argv[1] = "5001";
serv.sin_port = htons(atoi(argv[1]));
serv.sin_addr.s_addr = htonl(0);
bind(sockfd,(SA*)&serv,sizeof(serv));
listen(sockfd,5);
// for( ; ; )
{
len = sizeof(cli);
complete = accept(sockfd,(SA*)&cli,&len);
//发送文件名列表
fp = popen("ls","r");
while( fgets(buf,MAXLINE,fp) != NULL)
{
send(complete,buf,strlen(buf),0);
}
strcpy(buf,"input file name\n");
send(complete,buf, strlen(buf),0);
puts("send list success");
pclose(fp);
//exit(0);
//获取文件名
while( (recv_l = recv(complete,filename,sizeof(filename),0) ) > 0)
{
filename[recv_l] = '\0';
if ((fp = fopen(filename,"rb")) == NULL)
{
strcpy(buf,"filename error please input again");
puts("filename error");
send(complete,buf,strlen(buf),0);
}else{
puts("get filename success");
break;
}
}
//文件名对了
strcpy(buf,"success");
send(complete,buf,strlen(buf),0);
puts("get correct filename");
//传文件
int all = 0;
long long sa = 0;
int len = sizeof(Dat);
while((block_len = fread(Dat.data,sizeof(char),MAXLINE,fp)) > 0)
{
Dat.len = block_len;
if (sendall(complete,&Dat,&len) < 0){
puts("send error");
exit(1);
}
// if (send(complete,&Dat ,sizeof(Dat),0) < 0)
// {
// perror("send");
// exit(1);
// }
}
printf("%lld\n",sum);
printf("%s Tranfer finished\n",filename);
fclose(fp);
close(complete);
}
return 0;
}

on 32 bit systems, the maximum file size is 2 GB by default.
you can compile your program with the switch -D_FILE_OFFSET_BITS=64 to create files larger than 2 GB or use 64 bit.

Related

How can I program a file transfer system using UDP so that files are transferred instead of messages?

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
int pid;
int sock;
void interrupt(int sig) {
close(sock);
if (pid) {
kill(pid, sig);
printf("Exiting...\n");
}
exit(0);
}
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("Usage: %s portnumber\n", argv[0]);
return 1;
}
signal(SIGINT, &interrupt);
signal(SIGTERM, &interrupt);
int sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr = {
AF_INET,
htons(atoi(argv[1])),
INADDR_ANY
};
int res = bind( sock,
(struct sockaddr*)&addr,
sizeof(addr));
if (res < 0) {
perror("Bind");
return -1;
}
int pid = fork();
if (pid) {
while(1) {
char buf[100];
int chars = 0;
char c;
int portnumber;
scanf("%d", &portnumber);
struct sockaddr_in target = {
AF_INET,
htons(portnumber),
inet_addr("127.0.01")
};
while(read(0, &c, 1) > 0) {
if(c == '\n') {
// TODO send buf
sendto(sock, buf, chars, 0,
(struct sockaddr*)&target,
sizeof(target));
chars = 0;
break;
} else {
if (chars < 100)
buf[chars++] = c;
else
printf("\n->Maximum character is reached, press enter to send\n");
}
}
}
} else {
struct sockaddr_in incoming;
int lenincoming = sizeof(incoming);
char buf[101];
int chars = 0;
while( (chars = recvfrom(sock, &buf, 100, 0,
(struct sockaddr*)&incoming,
&lenincoming)) >= 0)
{
write(1, buf, chars);
write(1, "\n", 1);
buf[chars] = 0;
if (strcmp(buf, "ok") != 0) {
sendto( sock, "ok", 2, 0,
(struct sockaddr*)&incoming,
lenincoming);
}
}
}
return 0;
}
I want to modify this program so that a file can be sent. It should ask a port number and filename and then send the file in 100 byte packages to the other side. Every package should have starting point of the data. When I get data, I should seek to the location that is specified at the start of the data. Save to fixed filename or if you want transfer filename first. You can assume that you are working on the same system.
I read about "lseek(fd, start, SEEK_SET)". I think it should help. But how to combine it all?

file transfer using UDP protocol

I have a server and client code which I am using to transfer file from server to client. After compiling, instead of copying my whole file from server directory to client directory, it shows the content of file on the terminal. I am new to c and this my assignment. I know UDP is not the best possible way to implement file transfer application. But this is how I have to do it.
This is server side:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_PROTOCOL 0
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define cipherKey 'S'
#define sendrecvflag 0
#define nofile "File Not Found!"
// funtion to clear buffer
void clearBuf(char* b)
{
int i;
for (i = 0; i < NET_BUF_SIZE; i++)
b[i] = '\0';
}
// funtion to encrypt
char Cipher(char ch)
{
return ch ^ cipherKey;
}
// funtion sending file
int sendFile(FILE* fp, char* buf, int s)
{
int i, len;
if (fp == NULL) {
strcpy(buf, nofile);
len = strlen(nofile);
buf[len] = EOF;
for (i = 0; i <= len; i++)
buf[i] = Cipher(buf[i]);
return 1;
}
char ch, ch2;
for (i = 0; i < s; i++) {
ch = fgetc(fp);
ch2 = Cipher(ch);
buf[i] = ch2;
if (ch == EOF)
return 1;
}
return 0;
}
// driver code
int main()
{
int sockfd, nBytes;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(PORT_NO);
addr_con.sin_addr.s_addr = INADDR_ANY;
char net_buf[NET_BUF_SIZE];
FILE* fp;
// socket()
sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
// bind()
if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
printf("\nSuccessfully binded!\n");
else
printf("\nBinding Failed!\n");
while (1) {
printf("\nWaiting for file name...\n");
// receive file name
clearBuf(net_buf);
nBytes = recvfrom(sockfd, net_buf,
NET_BUF_SIZE, sendrecvflag,
(struct sockaddr*)&addr_con, &addrlen);
fp = fopen(net_buf, "r");
printf("\nFile Name Received: %s\n", net_buf);
if (fp == NULL)
printf("\nFile open failed!\n");
else
printf("\nFile Successfully opened!\n");
while (1) {
if (sendFile(fp, net_buf, NET_BUF_SIZE)) {
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag,
(struct sockaddr*)&addr_con, addrlen);
break;
}
// send
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag,
(struct sockaddr*)&addr_con, addrlen);
clearBuf(net_buf);
}
if (fp != NULL)
fclose(fp);
}
return 0;
}
This is client side:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_PROTOCOL 0
#define IP_ADDRESS "127.0.0.1" // localhost
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define cipherKey 'S'
#define sendrecvflag 0
// funtion to clear buffer
void clearBuf(char* b)
{
int i;
for (i = 0; i < NET_BUF_SIZE; i++)
b[i] = '\0';
}
// function for decryption
char Cipher(char ch)
{
return ch ^ cipherKey;
}
// function to receive file
int recvFile(char* buf, int s)
{
int i;
char ch;
for (i = 0; i < s; i++) {
ch = buf[i];
ch = Cipher(ch);
if (ch == EOF)
return 1;
else
printf("%c", ch);
}
return 0;
}
// driver code
int main()
{
int sockfd, nBytes;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(PORT_NO);
addr_con.sin_addr.s_addr = inet_addr(IP_ADDRESS);
char net_buf[NET_BUF_SIZE];
FILE* fp;
// socket()
sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
while (1) {
printf("\nPlease enter file name to receive:\n");
scanf("%s", net_buf);
sendto(sockfd, net_buf, NET_BUF_SIZE, sendrecvflag, (struct sockaddr*)&addr_con, addrlen);
printf("\n---------Data Received---------\n");
while (1) {
// receive
clearBuf(net_buf);
nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE, sendrecvflag, (struct sockaddr*)&addr_con, &addrlen);
// process
if (recvFile(net_buf, NET_BUF_SIZE)) {
break;
}
}
printf("\n-------------------------------\n");
}
return 0;
}
I need to copy file from Server to Client

C socket: recv jams the program if used twice

I'm trying to implement C code on a UNIX platform which sends all files data to the client, and uploading a file from the client to the server.
The first part is working but for the second part I've encountered an issue.
After some debugging I've found out that the server side jams the client when I'm using recv the second time in the code [I've marked it for your convenience].
I've tried to look up on guides or what I'm doing wrong but I couldn't find the issue for what causing the program to get stuck.
For de-bugging purposes I'm now trying to send an int from client to the server
server side
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <dirent.h>
#include <fcntl.h>
#define PORT 0x0da2
#define IP_ADDR 0x7f000001
#define QUEUE_LEN 20
int main(void)
{
//char directory[1024];
//chdir("server");
//getcwd(directory, sizeof(directory));
//printf("%s",directory);
DIR* directory;
struct dirent* ent;
int fd;
char buffer[1000] = {0};
char convert[100] = {0};
size_t array_size = 0;
struct stat fileStat;
if ((directory = opendir ("server")) == NULL)
{
perror ("Cannot open .");
return 1;
}
while ((ent = readdir(directory)) != NULL)
{
if (!( !strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) )
{
fd = openat(dirfd(directory), ent->d_name, 0);
if (fd == -1)
{
perror ("Can't get stats.");
return 1;
}
if(fstat(fd, &fileStat)== -1)
{
perror ("Can't get stats.");
return 1;
}
strcat(buffer,ent->d_name);
sprintf(convert,"%lld", (long long) fileStat.st_size);
strcat(buffer, " ");
strcat(buffer,convert);
strcat(buffer,"\n");
}
}
int listenS = socket(AF_INET, SOCK_STREAM, 0);
if (listenS < 0)
{
perror("socket");
return 1;
}
struct sockaddr_in s = {0};
s.sin_family = AF_INET;
s.sin_port = htons(PORT);
s.sin_addr.s_addr = htonl(IP_ADDR);
if (bind(listenS, (struct sockaddr*)&s, sizeof(s)) < 0)
{
perror("bind");
return 1;
}
if (listen(listenS, QUEUE_LEN) < 0)
{
perror("listen");
return 1;
}
struct sockaddr_in clientIn;
int clientInSize = sizeof clientIn;
while (1)
{
int newfd = accept(listenS, (struct sockaddr*)&clientIn, (socklen_t*)&clientInSize);
if (newfd < 0)
{
perror("accept");
return 1;
}
if (send(newfd, &buffer, strlen(buffer), 0) < 0)
{
perror("send");
return 1;
}
char namebuff[100] = {0};
char sizebuff[256] = {0};
//int size;
ssize_t nrecv;
int error = 0;
int fsize;
//int dataleft;
if ((nrecv = recv(newfd, &namebuff, sizeof(namebuff), 0)) < 0)
{
perror("recv");
return 1;
}
if((strstr(buffer,namebuff) != NULL))
{
error = -1;
if (send(newfd, &error, sizeof(int), 0) < 0)
{
perror("send");
return 1;
}
}
/*if ((nrecv = recv(newfd, &fsize, sizeof(int), 0)) < 0) //this line is what jams the program
{
perror("recv");
return 1;
}*/
if(error != -1) //
{
if ((nrecv = recv(newfd, &sizebuff, sizeof(sizebuff), 0)) < 0)
{
perror("recv");
return 1;
}
fsize = atoi(sizebuff);
}
printf("%s\n",namebuff);
printf("%d\n",fsize);
close(newfd);
}
close(listenS);
return 0;
}
client side
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#define PORT 0x0da2
#define IP_ADDR 0x7f000001
int ctoi(char c) {
return c-'0';
}
int main(void)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
ssize_t nrecv;
struct sockaddr_in s = {0};
s.sin_family = AF_INET;
s.sin_port = htons(PORT);
s.sin_addr.s_addr = htonl(IP_ADDR);
if (connect(sock, (struct sockaddr*)&s, sizeof(s)) < 0)
{
perror("connect");
return 1;
}
printf("Successfully connected.\n");
char buffer[1000] = {0};
if ((nrecv = recv(sock, &buffer, sizeof(buffer), 0)) < 0)
{
perror("recv");
return 1;
}
printf("./client list-files\n%s\n", buffer); //nrcev gives the size of the data is recived
int fd;
//int i;
//ssize_t nwrite, nread;
char sizebuff[256];
struct stat file_stat;
int numcheck = 0;
//char buffer2[4096];
char namebuff[100] = "coolfile.txt";
fd=open("coolfile.txt",O_RDONLY);
if (send(sock, &namebuff, strlen(namebuff), 0) < 0)
{
perror("send");
return 1;
}
/*if ((nrecv = recv(sock, &numcheck, sizeof(int), 0)) < 0)
{
perror("recv");
return 1;
}*/
if (numcheck == -1)
{
printf("file already exists in the server\n");
return 1;
}
if (fstat(fd, &file_stat) < 0)
{
printf("error");
return 1;
}
sprintf(sizebuff, "%ld", file_stat.st_size);
//printf("%s\n",sizebuff);
/*int number = 5;
if(send(sock, &number, sizeof(int), 0) < 0)
{
printf("error");
return 1;
}*/
if(send(sock, &sizebuff, strlen(sizebuff), 0) < 0);
{
printf("error");
return 1;
}
/*
nread = read(fd, buffer2, 4096);
for (i = 0; i < nread; i += nwrite)
{
nwrite = write(sock, buffer2 + i, nread - i);
}
} while (nread != 0);*/
return 0;
}
Thanks for your help!

Encapsulation and auxiliary functions for using UDP sockets on a client and server

I am using the UDP auxiliary functions encapsulated below replacing the calls the functions of the sockets libraries to simulate and allow some testing on the UDP client and server. But I am not able to make the connection between them and pass the arguments correctly.
/********auxiliary functions for using UDP sockets*********/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <strings.h>
#include "tp_socket.h"
#define MTU 1024
int tp_mtu(void)
{
fprintf(stderr,"tp_mtu called\n");
return MTU;
}
int tp_sendto(int so, char* buff, int buff_len, so_addr* to_addr)
{
int count;
fprintf(stderr,"tp_sendto called (%d bytes)\n", buff_len);
count = sendto(so, (void*)buff, buff_len, 0,
(struct sockaddr*) to_addr, sizeof(struct sockaddr_in));
fprintf(stderr,"tp_sendto returning (sent %d bytes)\n", count);
return count;
}
int tp_recvfrom(int so, char* buff, int buff_len, so_addr* from_addr)
{
int count;
unsigned int sockaddr_len = sizeof(struct sockaddr_in);
fprintf(stderr,"tp_recvfrom called (%d bytes)\n",buff_len);
count = recvfrom(so,(void*)buff,(size_t)buff_len,0,
(struct sockaddr*) from_addr, &sockaddr_len);
fprintf(stderr,"tp_recvfrom returning (received %d bytes)\n",count);
return count;
}
int tp_init(void)
{
fprintf(stderr,"tp_init called\n");
return 0;
}
int tp_socket(unsigned short port)
{
int so;
struct sockaddr_in local_addr;
int addr_len =sizeof(local_addr);
fprintf(stderr,"tp_socket called\n");
if ((so=socket(PF_INET,SOCK_DGRAM,0))<0) {
return -1;
}
if (tp_build_addr(&local_addr,INADDR_ANY,port)<0) {
return -2;
}
if (bind(so, (struct sockaddr*)&local_addr, sizeof(local_addr))<0) {
return -3;
}
return so;
}
int tp_build_addr(so_addr* addr, char* hostname, int port)
{
struct hostent* he;
fprintf(stderr,"tp_build_addr called\n");
addr->sin_family = PF_INET;
addr->sin_port = htons(port);
if (hostname==NULL) {
addr->sin_addr.s_addr = INADDR_ANY;
} else {
if ((he=gethostbyname(hostname))==NULL) {
return -1;
}
bcopy(he->h_addr,&(addr->sin_addr.s_addr),sizeof(in_addr_t));
}
return 0;
}
My code at the moment of the UDP client:
/********clientUDP*********/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include "tp_socket.h"
int main(int argc, char *argv[])
{
so_addr to_addr;
char *ip_server;
char my_buffer[10];
int port_servidor, tam_buffer;
if (argc != 5)
{
perror("Error");
exit(-1);
}
*ip_server = argv[1];
port_server = atoi(argv[2]);
char arquivo = argv[3];
tam_buffer = atoi(argv[4]);
tp_init();
tp_socket(port);
tp_build_addr(&to_addr, ip_server, port_server);
exit(0);
}
My code at the moment of the UDP server:
/********serverUDP*********/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include "tp_socket.h"
int main(int argc, char *argv[])
{
so_addr my_addr;
int aux, so, mtu, port_server,tam_buffer,n;
char *archive;
char buffer[10];
FILE *file;
if (argc != 3)
{
perror("Error");
exit(-1);
}
port_server = atoi(argv[1]);
tam_buffer = atoi(argv[2]);
aux = tp_init();
if (aux < 0)
{
perror("error");
exit(aux);
}
mtu = tp_mtu();
so = tp_socket(port_server);
tp_recvfrom(so, buffer, 10 , &my_addr);
n = read( so,tam_buffer,1);
file = fopen(archive,"r");
if ( file != NULL )
{
n=fread(buffer,1,atoi(argv[2]),file);
while ( n > 0)
{
write(so,buffer,n);
n=fread(buffer,1, atoi(argv[2]),file);
}
} else
{
printf("error\n");
exit(0);
}
fclose(file);
shutdown(so,2);
close();
return 0;
}
Could someone please explain how I use the auxiliary functions to connect the client to the server and transfer files? I've be I've been locked up a long time at this stage.

How to return pointer to memory of a chunk of data in a circular buffer in C?

So i have this program where it receives data in a client/server model in linux. I have a circular buffer. I want to store 16384 pieces of data and set a flag after it's filled 16384.
Then return the pointer to that array after it's filled. then moves on to the next chunk and so on. and when my array is full. it starts over. but I don't know how to return the first pointer everytime it changes. I feel like my code logic is wrong.
Any help would be appreciated:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <stdbool.h>
#define MAX_ITEMS 524288 //nearest power of 2 to half million (2^19)
typedef struct circularQueue_s
{
int first;
int last;
int validItems;
int data[MAX_ITEMS];
} circularQueue_t;
void initializeQueue(circularQueue_t *theQueue);
int isEmpty(circularQueue_t *theQueue);
int putItem(circularQueue_t *theQueue, int theItemValue);
int getItem(circularQueue_t *theQueue, int *theItemValue);
void printQueue(circularQueue_t *theQueue);
void initializeQueue(circularQuete_t *theQueue)
{
int i;
theQueue->validItems = 0;
theQueue->first = 0;
theQueue->last = 0;
for (i = 0; i<MAX_ITEMS; i++)
{
theQueue->data[i] = 0;
}
return;
}
int isEmpty(circularQueue_t *theQueue)
{
if (theQueue->validItems == 0)
return(1);
else
return(0);
}
//puts new item at the end of the queue, moves rear pointer ahead by 1
int putItem(circularQueue_t *theQueue, int theItemValue)
{
if (theQueue->validItems >= MAX_ITEMS)
{
printf("The queue is full\n");
printf("You cannot add items\n");
return(-1);
}
else
{
theQueue->validItems++;
theQueue->data[theQueue->last] = theItemValue;
theQueue->last = (theQueue->last + 1) % MAX_ITEMS;
}
}
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
int recvBuff[16384];
struct sockaddr_in serv_addr;
circularQueue_t myQueue;
initializeQueue(&myQueue);
bool messageReceivedFlag = false;
memset(recvBuff, '0', sizeof(recvBuff));
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_addr.s_addr = inet_addr("192.168.100.200");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
int count;
while ((n = recv(sockfd, recvBuff, sizeof(recvBuff), 0)) > 0)
{
count = 0;
while (count < 16385)
{
putItem(&myQueue, recvBuff[count]);
count++;
if (count == 16384) {
messageReceivedFlag = true;
}
}
}
printf("then: %d\n", n);
if (n < 0)
{
printf("\n Read error \n");
}
return &myQueue;
}

Resources